dbobjectset.class.php 20 KB

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