dbobjectset.class.php 21 KB

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