objectManager: Fix lower-case letter in 'email Text'
[NewAppDB.git] / include / category.php
blobe6dae386c5593642258102dde711e403e1c7c963
1 <?php
2 /***************************************************/
3 /* this class represents a category + its children */
4 /***************************************************/
6 /**
7 * Category class for handling categories.
8 */
9 class Category {
10 var $iCatId;
11 var $iParentId;
12 var $sName;
13 var $sDescription;
14 var $aApplicationsIds; // an array that contains the appId of every application linked to this category
15 var $aSubcatsIds; // an array that contains the appId of every application linked to this category
18 /**
19 * constructor, fetches the data.
21 function Category($iCatId = null)
23 // we are working on an existing category
24 if($iCatId=="0" || $iCatId)
27 * We fetch the data related to this vendor.
29 $sQuery = "SELECT *
30 FROM appCategory
31 WHERE catId = '?' ORDER BY catName;";
32 if($hResult = query_parameters($sQuery, $iCatId))
34 $oRow = query_fetch_object($hResult);
35 if($oRow)
37 $this->iCatId = $iCatId;
38 $this->iParentId = $oRow->catParent;
39 $this->sName = $oRow->catName;
40 $this->sDescription = $oRow->catDescription;
45 * We fetch applicationsIds.
47 $sQuery = "SELECT appId
48 FROM appFamily
49 WHERE catId = '?'
50 AND state = 'accepted' ORDER BY appName";
51 if($hResult = query_parameters($sQuery, $iCatId))
53 while($oRow = query_fetch_object($hResult))
55 $this->aApplicationsIds[] = $oRow->appId;
60 * We fetch subcatIds.
62 $sQuery = "SELECT catId
63 FROM appCategory
64 WHERE catParent = '?' ORDER BY catName;";
65 if($hResult = query_parameters($sQuery, $iCatId))
67 while($oRow = query_fetch_object($hResult))
69 $this->aSubcatsIds[] = $oRow->catId;
76 /**
77 * Creates a new category.
79 function create()
81 $hResult = query_parameters("INSERT INTO appCategory (catName, catDescription, catParent) ".
82 "VALUES('?', '?', '?')",
83 $this->sName, $this->sDescription, $this->iParentId);
84 if($hResult)
86 $this->iCatId = query_appdb_insert_id();
87 $this->category($this->iCatId);
88 return true;
91 return false;
94 /**
95 * Update category.
96 * Returns true on success and false on failure.
98 function update()
100 if(!query_parameters("UPDATE appCategory SET catName = '?', catDescription = '?', catParent = '?' WHERE catId = '?'",
101 $this->sName, $this->sDescription, $this->iParentId, $this->iCatId))
102 return false;
104 return true;
108 /**
109 * Deletes the category from the database.
111 function delete()
113 if(!$this->canEdit())
114 return false;
116 if(sizeof($this->aApplicationsIds)>0)
117 return FALSE;
119 $sQuery = "DELETE FROM appCategory
120 WHERE catId = '?'
121 LIMIT 1";
122 query_parameters($sQuery, $this->iCatId);
124 return true;
127 function objectGetMailOptions($sAction, $bMailSubmitter, $bParentAction)
129 return new mailOptions();
132 function objectGetChildren()
134 /* We don't have any (or we do, sort of, but we don't use them for anything at the moment) */
135 return array();
138 /* Get a category's subcategory objects. Names are indented according
139 to subcategory level */
140 function getSubCatList($iLevel)
142 $aOut = array();
143 $iId = $this->iCatId ? $this->iCatId : 0;
145 $sIndent = '';
146 for($i = 0; $i < $iLevel; $i++)
147 $sIndent .= '&nbsp; &nbsp;';
149 $hResult = query_parameters("SELECT * FROM appCategory WHERE catParent = '?'
150 ORDER BY catName", $iId);
152 while($oRow = mysql_fetch_object($hResult))
154 $oCat = new category($oRow->catId);
155 $oCat->sName = $sIndent.$oCat->sName;
156 $aOut[] = $oCat;
157 $aOut = array_merge($aOut, $oCat->getSubCatList($iLevel + 1));
159 return $aOut;
162 /* Get all category objects, ordered and with category names indented
163 according to subcategory level */
164 static function getOrderedList()
166 $oCat = new category();
167 return $oCat->getSubCatList(0);
170 function objectGetMail($sAction, $bMailSubmitter, $bParentAction)
172 /* We don't send notification mails */
173 return array(null, null, null);
177 * returns a path like:
179 * { ROOT, Games, Simulation }
181 function getCategoryPath()
183 $aPath = array();
184 $iCatId = $this->iCatId;
186 /* loop, working up through categories until we have no parent */
187 while($iCatId != 0)
189 $hResult = query_parameters("SELECT catName, catId, catParent FROM appCategory WHERE catId = '?'",
190 $iCatId);
191 if(!$hResult || query_num_rows($hResult) != 1)
192 break;
193 $oCatRow = query_fetch_object($hResult);
194 $aPath[] = array($oCatRow->catId, $oCatRow->catName);
195 $iCatId = $oCatRow->catParent;
197 $aPath[] = array(0, "ROOT");
198 return array_reverse($aPath);
201 /* return the total number of applications in this category */
202 function getApplicationCount($depth = null)
204 $MAX_DEPTH = 5;
206 if($depth)
207 $depth++;
208 else
209 $depth = 0;
211 /* if we've reached our max depth, just return 0 and stop recursing */
212 if($depth >= $MAX_DEPTH)
213 return 0;
215 $totalApps = 0;
217 /* add on all apps in each category this category includes */
218 if($this->aSubcatsIds)
220 while(list($i, $iSubcatId) = each($this->aSubcatsIds))
222 $subCat = new Category($iSubcatId);
223 $totalApps += $subCat->getApplicationCount($depth);
227 $totalApps += sizeof($this->aApplicationsIds); /* add on the apps at this category level */
229 return $totalApps;
233 * create the Category: line at the top of appdb pages$
235 function make_cat_path($path, $appId = '', $versionId = '')
237 $str = "";
238 $catCount = 0;
239 while(list($iCatIdx, list($iCatId, $name)) = each($path))
241 if($name == "ROOT")
242 $catname = "Main";
243 else
244 $catname = $name;
246 if ($catCount > 0) $str .= " &gt; ";
247 $str .= html_ahref($catname,"appbrowse.php?catId=$iCatId");
248 $catCount++;
251 if(!empty($appId))
253 $oApp = new Application($appId);
254 if(!empty($versionId))
256 $oVersion = new Version($versionId);
257 $str .= " &gt; ".$oApp->objectMakeLink();
258 $str .= " &gt; ".$oVersion->sName;
259 } else
261 $str .= " &gt; ".$oApp->sName;
265 return $str;
268 function objectGetId()
270 return $this->iCatId;
273 function objectGetSubmitterId()
275 /* We don't log that */
276 return 0;
279 function outputEditor()
281 $sQuery = "SELECT catId, catName FROM appCategory WHERE catId!='?'";
282 $hResult = query_parameters($sQuery, $this->iCatId);
284 /* Add the virtual 'Main' category */
285 $aCatIds = array(0);
286 $aCatNames = array('Main');
288 /* Add the rest from the database */
289 while($oRow = query_fetch_object($hResult))
291 $aCatIds[] = $oRow->catId;
292 $aCatNames[] = $oRow->catName;
295 echo "<table border=\"0\" width=\"100%\" cellspacing=\"0\" cellpadding=\"2\">
296 <tr>
297 <td width=\"15%\" class=\"box-label\"><b>Category name</b></td>
298 <td class=\"box-body\">
299 <input type=\"text\" size=\"50\" name=\"sName\" value=\"".$this->sName."\">
300 </td>
301 </tr>
302 <tr>
303 <td width=\"15%\" class=\"box-label\"><b>Description</b></td>
304 <td class=\"box-body\">
305 <input type=\"text\" size=\"50\" name=\"sDescription\" value=\"".$this->sDescription."\">
306 </td>
307 </tr>
308 <tr>
309 <td width=\"15%\" class=\"box-label\"><b>Parent</b></td>
310 <td class=\"box-body\">
311 ".html_select("iParentId",$aCatIds,$this->iParentId, $aCatNames)."
312 </td>
313 </tr>
314 </table>";
317 function allowAnonymousSubmissions()
319 return FALSE;
322 function getOutputEditorValues($aClean)
324 $this->sName = $aClean['sName'];
325 $this->iParentId = $aClean['iParentId'];
326 $this->sDescription = $aClean['sDescription'];
329 function mustBeQueued()
331 return $_SESSION['current']->hasPriv('admin');
334 function canEdit()
336 return $_SESSION['current']->hasPriv('admin');
340 * display the full path of the Category we are looking at
342 function display($appId, $versionId = '')
344 $sCatFullPath = Category::make_cat_path($this->getCategoryPath(), $appId, $versionId);
345 echo html_frame_start("",'98%','',2);
346 echo "<p><b>Category: ". $sCatFullPath ."</b><br>\n";
347 echo html_frame_end();