dbobjectset.class.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  1. <?php
  2. // Copyright (C) 2010 Combodo SARL
  3. //
  4. // This program is free software; you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation; version 3 of the License.
  7. //
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. /**
  17. * Object set management
  18. *
  19. * @author Erwan Taloc <erwan.taloc@combodo.com>
  20. * @author Romain Quetiez <romain.quetiez@combodo.com>
  21. * @author Denis Flaven <denis.flaven@combodo.com>
  22. * @license http://www.opensource.org/licenses/gpl-3.0.html LGPL
  23. */
  24. /**
  25. * A set of persistent objects, could be heterogeneous
  26. *
  27. * @package iTopORM
  28. */
  29. class DBObjectSet
  30. {
  31. private $m_oFilter;
  32. private $m_aOrderBy;
  33. public $m_bLoaded;
  34. private $m_aData;
  35. private $m_aId2Row;
  36. private $m_iCurrRow;
  37. public function __construct(DBObjectSearch $oFilter, $aOrderBy = array(), $aArgs = array(), $aExtendedDataSpec = null, $iLimitCount = 0, $iLimitStart = 0)
  38. {
  39. $this->m_oFilter = $oFilter;
  40. $this->m_aOrderBy = $aOrderBy;
  41. $this->m_aArgs = $aArgs;
  42. $this->m_aAttToLoad = null;
  43. $this->m_aExtendedDataSpec = $aExtendedDataSpec;
  44. $this->m_iLimitCount = $iLimitCount;
  45. $this->m_iLimitStart = $iLimitStart;
  46. $this->m_iCount = null; // null if unknown yet
  47. $this->m_bLoaded = false; // true when the filter has been used OR the set is built step by step (AddObject...)
  48. $this->m_aData = array(); // array of (row => array of (classalias) => object/null)
  49. $this->m_aId2Row = array(); // array of (pkey => index in m_aData)
  50. $this->m_iCurrRow = 0;
  51. }
  52. public function __destruct()
  53. {
  54. }
  55. public function __toString()
  56. {
  57. $sRet = '';
  58. $this->Rewind();
  59. $sRet .= "Set (".$this->m_oFilter->ToOQL().")<br/>\n";
  60. $sRet .= "Query: <pre style=\"font-size: smaller; display:inline;\">".MetaModel::MakeSelectQuery($this->m_oFilter, array()).")</pre>\n";
  61. $sRet .= $this->Count()." records<br/>\n";
  62. if ($this->Count() > 0)
  63. {
  64. $sRet .= "<ul class=\"treeview\">\n";
  65. while ($oObj = $this->Fetch())
  66. {
  67. $sRet .= "<li>".$oObj->__toString()."</li>\n";
  68. }
  69. $sRet .= "</ul>\n";
  70. }
  71. return $sRet;
  72. }
  73. public function OptimizeColumnLoad($aAttToLoad)
  74. {
  75. if (is_null($aAttToLoad))
  76. {
  77. $this->m_aAttToLoad = null;
  78. }
  79. else
  80. {
  81. // Complete the attribute list with the attribute codes
  82. $aAttToLoadWithAttDef = array();
  83. foreach($aAttToLoad as $sClassAlias => $aAttList)
  84. {
  85. $aSelectedClasses = $this->m_oFilter->GetSelectedClasses();
  86. $sClass = $aSelectedClasses[$sClassAlias];
  87. foreach($aAttList as $sAttToLoad)
  88. {
  89. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttToLoad);
  90. $aAttToLoadWithAttDef[$sClassAlias][$sAttToLoad] = $oAttDef;
  91. if ($oAttDef->IsExternalKey())
  92. {
  93. // Add the external key friendly name anytime
  94. $oFriendlyNameAttDef = MetaModel::GetAttributeDef($sClass, $sAttToLoad.'_friendlyname');
  95. $aAttToLoadWithAttDef[$sClassAlias][$sAttToLoad.'_friendlyname'] = $oFriendlyNameAttDef;
  96. }
  97. }
  98. // Add the friendly name anytime
  99. $oFriendlyNameAttDef = MetaModel::GetAttributeDef($sClass, 'friendlyname');
  100. $aAttToLoadWithAttDef[$sClassAlias]['friendlyname'] = $oFriendlyNameAttDef;
  101. // Make sure that the final class is requested anytime, whatever the specification (needed for object construction!)
  102. if (!MetaModel::IsStandaloneClass($sClass) && !array_key_exists('finalclass', $aAttList))
  103. {
  104. $aAttToLoadWithAttDef[$sClassAlias]['finalclass'] = MetaModel::GetAttributeDef($sClass, 'finalclass');
  105. }
  106. }
  107. $this->m_aAttToLoad = $aAttToLoadWithAttDef;
  108. }
  109. }
  110. static public function FromObject($oObject)
  111. {
  112. $oRetSet = self::FromScratch(get_class($oObject));
  113. $oRetSet->AddObject($oObject);
  114. return $oRetSet;
  115. }
  116. static public function FromScratch($sClass)
  117. {
  118. $oFilter = new CMDBSearchFilter($sClass);
  119. $oRetSet = new self($oFilter);
  120. $oRetSet->m_bLoaded = true; // no DB load
  121. return $oRetSet;
  122. }
  123. // create an object set ex nihilo
  124. // input = array of objects
  125. static public function FromArray($sClass, $aObjects)
  126. {
  127. $oFilter = new CMDBSearchFilter($sClass);
  128. $oRetSet = new self($oFilter);
  129. $oRetSet->m_bLoaded = true; // no DB load
  130. $oRetSet->AddObjectArray($aObjects, $sClass);
  131. return $oRetSet;
  132. }
  133. // create an object set ex nihilo
  134. // aClasses = array of (alias => class)
  135. // input = array of (array of (classalias => object))
  136. static public function FromArrayAssoc($aClasses, $aObjects)
  137. {
  138. // In a perfect world, we should create a complete tree of DBObjectSearch,
  139. // but as we lack most of the information related to the objects,
  140. // let's create one search definition
  141. $sClass = reset($aClasses);
  142. $sAlias = key($aClasses);
  143. $oFilter = new CMDBSearchFilter($sClass, $sAlias);
  144. $oRetSet = new self($oFilter);
  145. $oRetSet->m_bLoaded = true; // no DB load
  146. foreach($aObjects as $rowIndex => $aObjectsByClassAlias)
  147. {
  148. $oRetSet->AddObjectExtended($aObjectsByClassAlias);
  149. }
  150. return $oRetSet;
  151. }
  152. static public function FromLinkSet($oObject, $sLinkSetAttCode, $sExtKeyToRemote)
  153. {
  154. $oLinkAttCode = MetaModel::GetAttributeDef(get_class($oObject), $sLinkSetAttCode);
  155. $oExtKeyAttDef = MetaModel::GetAttributeDef($oLinkAttCode->GetLinkedClass(), $sExtKeyToRemote);
  156. $sTargetClass = $oExtKeyAttDef->GetTargetClass();
  157. $oLinkSet = $oObject->Get($sLinkSetAttCode);
  158. $aTargets = array();
  159. while ($oLink = $oLinkSet->Fetch())
  160. {
  161. $aTargets[] = MetaModel::GetObject($sTargetClass, $oLink->Get($sExtKeyToRemote));
  162. }
  163. return self::FromArray($sTargetClass, $aTargets);
  164. }
  165. public function ToArray($bWithId = true)
  166. {
  167. $aRet = array();
  168. $this->Rewind();
  169. while ($oObject = $this->Fetch())
  170. {
  171. if ($bWithId)
  172. {
  173. $aRet[$oObject->GetKey()] = $oObject;
  174. }
  175. else
  176. {
  177. $aRet[] = $oObject;
  178. }
  179. }
  180. return $aRet;
  181. }
  182. public function ToArrayOfValues()
  183. {
  184. if (!$this->m_bLoaded) $this->Load();
  185. $aSelectedClasses = $this->m_oFilter->GetSelectedClasses();
  186. $aRet = array();
  187. foreach($this->m_aData as $iRow => $aObjects)
  188. {
  189. foreach($aObjects as $sClassAlias => $oObject)
  190. {
  191. if (is_null($oObject))
  192. {
  193. $aRet[$iRow][$sClassAlias.'.'.'id'] = null;
  194. }
  195. else
  196. {
  197. $aRet[$iRow][$sClassAlias.'.'.'id'] = $oObject->GetKey();
  198. }
  199. if (is_null($oObject))
  200. {
  201. $sClass = $aSelectedClasses[$sClassAlias];
  202. }
  203. else
  204. {
  205. $sClass = get_class($oObject);
  206. }
  207. foreach(MetaModel::ListAttributeDefs($sClass) as $sAttCode => $oAttDef)
  208. {
  209. if ($oAttDef->IsScalar())
  210. {
  211. $sAttName = $sClassAlias.'.'.$sAttCode;
  212. if (is_null($oObject))
  213. {
  214. $aRet[$iRow][$sAttName] = null;
  215. }
  216. else
  217. {
  218. $aRet[$iRow][$sAttName] = $oObject->Get($sAttCode);
  219. }
  220. }
  221. }
  222. }
  223. }
  224. return $aRet;
  225. }
  226. public function GetColumnAsArray($sAttCode, $bWithId = true)
  227. {
  228. $aRet = array();
  229. $this->Rewind();
  230. while ($oObject = $this->Fetch())
  231. {
  232. if ($bWithId)
  233. {
  234. $aRet[$oObject->GetKey()] = $oObject->Get($sAttCode);
  235. }
  236. else
  237. {
  238. $aRet[] = $oObject->Get($sAttCode);
  239. }
  240. }
  241. return $aRet;
  242. }
  243. public function GetFilter()
  244. {
  245. // #@# This is false as soon as the set has been manipulated (AddObject...)
  246. return $this->m_oFilter;
  247. }
  248. public function GetClass()
  249. {
  250. return $this->m_oFilter->GetClass();
  251. }
  252. public function GetSelectedClasses()
  253. {
  254. return $this->m_oFilter->GetSelectedClasses();
  255. }
  256. public function GetRootClass()
  257. {
  258. return MetaModel::GetRootClass($this->GetClass());
  259. }
  260. public function SetLimit($iLimitCount, $iLimitStart = 0)
  261. {
  262. $this->m_iLimitCount = $iLimitCount;
  263. $this->m_iLimitStart = $iLimitStart;
  264. }
  265. public function GetLimitCount()
  266. {
  267. return $this->m_iLimitCount;
  268. }
  269. public function GetLimitStart()
  270. {
  271. return $this->m_iLimitStart;
  272. }
  273. public function Load()
  274. {
  275. if ($this->m_bLoaded) return;
  276. // Note: it is mandatory to set this value now, to protect against reentrance
  277. $this->m_bLoaded = true;
  278. if ($this->m_iLimitCount > 0)
  279. {
  280. $sSQL = MetaModel::MakeSelectQuery($this->m_oFilter, $this->m_aOrderBy, $this->m_aArgs, $this->m_aAttToLoad, $this->m_aExtendedDataSpec, $this->m_iLimitCount, $this->m_iLimitStart);
  281. }
  282. else
  283. {
  284. $sSQL = MetaModel::MakeSelectQuery($this->m_oFilter, $this->m_aOrderBy, $this->m_aArgs, $this->m_aAttToLoad, $this->m_aExtendedDataSpec);
  285. }
  286. $resQuery = CMDBSource::Query($sSQL);
  287. if (!$resQuery) return;
  288. $sClass = $this->m_oFilter->GetClass();
  289. while ($aRow = CMDBSource::FetchArray($resQuery))
  290. {
  291. $aObjects = array();
  292. foreach ($this->m_oFilter->GetSelectedClasses() as $sClassAlias => $sClass)
  293. {
  294. if (is_null($aRow[$sClassAlias.'id']))
  295. {
  296. $oObject = null;
  297. }
  298. else
  299. {
  300. $oObject = MetaModel::GetObjectByRow($sClass, $aRow, $sClassAlias, $this->m_aAttToLoad, $this->m_aExtendedDataSpec);
  301. }
  302. $aObjects[$sClassAlias] = $oObject;
  303. }
  304. $this->AddObjectExtended($aObjects);
  305. }
  306. CMDBSource::FreeResult($resQuery);
  307. }
  308. public function Count()
  309. {
  310. if ($this->m_bLoaded && ($this->m_iLimitCount == 0) && ($this->m_iLimitStart == 0))
  311. {
  312. return count($this->m_aData);
  313. }
  314. else
  315. {
  316. if (is_null($this->m_iCount))
  317. {
  318. $sSQL = MetaModel::MakeSelectQuery($this->m_oFilter, $this->m_aOrderBy, $this->m_aArgs, null, null, 0, 0, true);
  319. $resQuery = CMDBSource::Query($sSQL);
  320. if (!$resQuery) return 0;
  321. $aRow = CMDBSource::FetchArray($resQuery);
  322. CMDBSource::FreeResult($resQuery);
  323. $this->m_iCount = $aRow['COUNT'];
  324. }
  325. return $this->m_iCount;
  326. }
  327. }
  328. public function Fetch($sClassAlias = '')
  329. {
  330. if (!$this->m_bLoaded) $this->Load();
  331. if ($this->m_iCurrRow >= count($this->m_aData))
  332. {
  333. return null;
  334. }
  335. if (strlen($sClassAlias) == 0)
  336. {
  337. $sClassAlias = $this->m_oFilter->GetClassAlias();
  338. }
  339. $oRetObj = $this->m_aData[$this->m_iCurrRow][$sClassAlias];
  340. $this->m_iCurrRow++;
  341. return $oRetObj;
  342. }
  343. // Return the whole line if several classes have been specified in the query
  344. //
  345. public function FetchAssoc()
  346. {
  347. if (!$this->m_bLoaded) $this->Load();
  348. if ($this->m_iCurrRow >= count($this->m_aData))
  349. {
  350. return null;
  351. }
  352. $aRetObjects = $this->m_aData[$this->m_iCurrRow];
  353. $this->m_iCurrRow++;
  354. return $aRetObjects;
  355. }
  356. public function Rewind()
  357. {
  358. if ($this->m_bLoaded)
  359. {
  360. $this->Seek(0);
  361. }
  362. }
  363. public function Seek($iRow)
  364. {
  365. if (!$this->m_bLoaded) $this->Load();
  366. $this->m_iCurrRow = min($iRow, count($this->m_aData));
  367. return $this->m_iCurrRow;
  368. }
  369. public function AddObject($oObject, $sClassAlias = '')
  370. {
  371. if (!$this->m_bLoaded) $this->Load();
  372. if (strlen($sClassAlias) == 0)
  373. {
  374. $sClassAlias = $this->m_oFilter->GetClassAlias();
  375. }
  376. $iNextPos = count($this->m_aData);
  377. $this->m_aData[$iNextPos][$sClassAlias] = $oObject;
  378. if (!is_null($oObject))
  379. {
  380. $this->m_aId2Row[$sClassAlias][$oObject->GetKey()] = $iNextPos;
  381. }
  382. }
  383. protected function AddObjectExtended($aObjectArray)
  384. {
  385. if (!$this->m_bLoaded) $this->Load();
  386. $iNextPos = count($this->m_aData);
  387. foreach ($aObjectArray as $sClassAlias => $oObject)
  388. {
  389. $this->m_aData[$iNextPos][$sClassAlias] = $oObject;
  390. if (!is_null($oObject))
  391. {
  392. $this->m_aId2Row[$sClassAlias][$oObject->GetKey()] = $iNextPos;
  393. }
  394. }
  395. }
  396. public function AddObjectArray($aObjects, $sClassAlias = '')
  397. {
  398. if (!$this->m_bLoaded) $this->Load();
  399. // #@# todo - add a check on the object class ?
  400. foreach ($aObjects as $oObj)
  401. {
  402. $this->AddObject($oObj, $sClassAlias);
  403. }
  404. }
  405. public function Merge($oObjectSet)
  406. {
  407. if ($this->GetRootClass() != $oObjectSet->GetRootClass())
  408. {
  409. throw new CoreException("Could not merge two objects sets if they don't have the same root class");
  410. }
  411. if (!$this->m_bLoaded) $this->Load();
  412. $oObjectSet->Seek(0);
  413. while ($oObject = $oObjectSet->Fetch())
  414. {
  415. $this->AddObject($oObject);
  416. }
  417. }
  418. public function CreateIntersect($oObjectSet)
  419. {
  420. if ($this->GetRootClass() != $oObjectSet->GetRootClass())
  421. {
  422. throw new CoreException("Could not 'intersect' two objects sets if they don't have the same root class");
  423. }
  424. if (!$this->m_bLoaded) $this->Load();
  425. $oNewSet = DBObjectSet::FromScratch($this->GetClass());
  426. $sClassAlias = $this->m_oFilter->GetClassAlias();
  427. $oObjectSet->Seek(0);
  428. while ($oObject = $oObjectSet->Fetch())
  429. {
  430. if (array_key_exists($oObject->GetKey(), $this->m_aId2Row[$sClassAlias]))
  431. {
  432. $oNewSet->AddObject($oObject);
  433. }
  434. }
  435. return $oNewSet;
  436. }
  437. // Note: This verb works only with objects existing in the database
  438. //
  439. public function HasSameContents($oObjectSet)
  440. {
  441. if ($this->GetRootClass() != $oObjectSet->GetRootClass())
  442. {
  443. return false;
  444. }
  445. if (!$this->m_bLoaded) $this->Load();
  446. if ($this->Count() != $oObjectSet->Count())
  447. {
  448. return false;
  449. }
  450. $sClassAlias = $this->m_oFilter->GetClassAlias();
  451. $oObjectSet->Rewind();
  452. while ($oObject = $oObjectSet->Fetch())
  453. {
  454. $iObjectKey = $oObject->GetKey();
  455. if ($iObjectKey < 0)
  456. {
  457. return false;
  458. }
  459. if (!array_key_exists($iObjectKey, $this->m_aId2Row[$sClassAlias]))
  460. {
  461. return false;
  462. }
  463. $iRow = $this->m_aId2Row[$sClassAlias][$iObjectKey];
  464. $oSibling = $this->m_aData[$iRow][$sClassAlias];
  465. if (!$oObject->Equals($oSibling))
  466. {
  467. return false;
  468. }
  469. }
  470. return true;
  471. }
  472. public function CreateDelta($oObjectSet)
  473. {
  474. if ($this->GetRootClass() != $oObjectSet->GetRootClass())
  475. {
  476. throw new CoreException("Could not 'delta' two objects sets if they don't have the same root class");
  477. }
  478. if (!$this->m_bLoaded) $this->Load();
  479. $oNewSet = DBObjectSet::FromScratch($this->GetClass());
  480. $sClassAlias = $this->m_oFilter->GetClassAlias();
  481. $oObjectSet->Seek(0);
  482. while ($oObject = $oObjectSet->Fetch())
  483. {
  484. if (!array_key_exists($oObject->GetKey(), $this->m_aId2Row[$sClassAlias]))
  485. {
  486. $oNewSet->AddObject($oObject);
  487. }
  488. }
  489. return $oNewSet;
  490. }
  491. public function GetRelatedObjects($sRelCode, $iMaxDepth = 99)
  492. {
  493. $aRelatedObjs = array();
  494. $aVisited = array(); // optimization for consecutive calls of MetaModel::GetRelatedObjects
  495. $this->Seek(0);
  496. while ($oObject = $this->Fetch())
  497. {
  498. $aMore = $oObject->GetRelatedObjects($sRelCode, $iMaxDepth, $aVisited);
  499. foreach ($aMore as $sClass => $aRelated)
  500. {
  501. foreach ($aRelated as $iObj => $oObj)
  502. {
  503. if (!isset($aRelatedObjs[$sClass][$iObj]))
  504. {
  505. $aRelatedObjs[$sClass][$iObj] = $oObj;
  506. }
  507. }
  508. }
  509. }
  510. return $aRelatedObjs;
  511. }
  512. /**
  513. * Builds an object that contains the values that are common to all the objects
  514. * in the set. If for a given attribute, objects in the set have various values
  515. * then the resulting object will contain null for this value.
  516. * @param $aValues Hash Output: the distribution of the values, in the set, for each attribute
  517. * @return Object
  518. */
  519. public function ComputeCommonObject(&$aValues)
  520. {
  521. $sClass = $this->GetClass();
  522. $aList = MetaModel::ListAttributeDefs($sClass);
  523. $aValues = array();
  524. foreach($aList as $sAttCode => $oAttDef)
  525. {
  526. if ($oAttDef->IsScalar())
  527. {
  528. $aValues[$sAttCode] = array();
  529. }
  530. }
  531. $this->Rewind();
  532. while($oObj = $this->Fetch())
  533. {
  534. foreach($aList as $sAttCode => $oAttDef)
  535. {
  536. if ($oAttDef->IsScalar() && $oAttDef->IsWritable())
  537. {
  538. $currValue = $oObj->Get($sAttCode);
  539. if (is_object($currValue)) continue; // Skip non scalar values...
  540. if(!array_key_exists($currValue, $aValues[$sAttCode]))
  541. {
  542. $aValues[$sAttCode][$currValue] = array('count' => 1, 'display' => $oObj->GetAsHTML($sAttCode));
  543. }
  544. else
  545. {
  546. $aValues[$sAttCode][$currValue]['count']++;
  547. }
  548. }
  549. }
  550. }
  551. foreach($aValues as $sAttCode => $aMultiValues)
  552. {
  553. if (count($aMultiValues) > 1)
  554. {
  555. uasort($aValues[$sAttCode], 'HashCountComparison');
  556. }
  557. }
  558. // Now create an object that has values for the homogenous values only
  559. $oCommonObj = new $sClass(); // @@ What if the class is abstract ?
  560. $aComments = array();
  561. $iFormId = cmdbAbstractObject::GetNextFormId(); // Identifier that prefixes all the form fields
  562. $sReadyScript = '';
  563. $aDependsOn = array();
  564. $sFormPrefix = '2_';
  565. foreach($aList as $sAttCode => $oAttDef)
  566. {
  567. if ($oAttDef->IsScalar() && $oAttDef->IsWritable())
  568. {
  569. if ($oAttDef->GetEditClass() == 'One Way Password')
  570. {
  571. $oCommonObj->Set($sAttCode, null);
  572. }
  573. else
  574. {
  575. $iCount = count($aValues[$sAttCode]);
  576. if ($iCount == 1)
  577. {
  578. // Homogenous value
  579. reset($aValues[$sAttCode]);
  580. $aKeys = array_keys($aValues[$sAttCode]);
  581. $currValue = $aKeys[0]; // The only value is the first key
  582. $oCommonObj->Set($sAttCode, $currValue);
  583. }
  584. else
  585. {
  586. // Non-homogenous value
  587. $oCommonObj->Set($sAttCode, null);
  588. }
  589. }
  590. }
  591. }
  592. $this->Rewind();
  593. return $oCommonObj;
  594. }
  595. }
  596. /**
  597. * Helper function to perform a custom sort of a hash array
  598. */
  599. function HashCountComparison($a, $b) // Sort descending on 'count'
  600. {
  601. if ($a['count'] == $b['count'])
  602. {
  603. return 0;
  604. }
  605. return ($a['count'] > $b['count']) ? -1 : 1;
  606. }
  607. ?>