cmdbabstract.class.inc.php 77 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100
  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. * Abstract class that implements some common and useful methods for displaying
  18. * the objects
  19. *
  20. * @author Erwan Taloc <erwan.taloc@combodo.com>
  21. * @author Romain Quetiez <romain.quetiez@combodo.com>
  22. * @author Denis Flaven <denis.flaven@combodo.com>
  23. * @license http://www.opensource.org/licenses/gpl-3.0.html LGPL
  24. */
  25. define('OBJECT_PROPERTIES_TAB', 'ObjectProperties');
  26. define('HILIGHT_CLASS_CRITICAL', 'red');
  27. define('HILIGHT_CLASS_WARNING', 'orange');
  28. define('HILIGHT_CLASS_OK', 'green');
  29. define('HILIGHT_CLASS_NONE', '');
  30. require_once(APPROOT.'/core/cmdbobject.class.inc.php');
  31. require_once(APPROOT.'/application/applicationextension.inc.php');
  32. require_once(APPROOT.'/application/utils.inc.php');
  33. require_once(APPROOT.'/application/applicationcontext.class.inc.php');
  34. require_once(APPROOT.'/application/ui.linkswidget.class.inc.php');
  35. require_once(APPROOT.'/application/ui.passwordwidget.class.inc.php');
  36. require_once(APPROOT.'/application/ui.extkeywidget.class.inc.php');
  37. require_once(APPROOT.'/application/ui.htmleditorwidget.class.inc.php');
  38. /**
  39. * All objects to be displayed in the application (either as a list or as details)
  40. * must implement this interface.
  41. */
  42. interface iDisplay
  43. {
  44. /**
  45. * Maps the given context parameter name to the appropriate filter/search code for this class
  46. * @param string $sContextParam Name of the context parameter, i.e. 'org_id'
  47. * @return string Filter code, i.e. 'customer_id'
  48. */
  49. public static function MapContextParam($sContextParam);
  50. /**
  51. * This function returns a 'hilight' CSS class, used to hilight a given row in a table
  52. * There are currently (i.e defined in the CSS) 4 possible values HILIGHT_CLASS_CRITICAL,
  53. * HILIGHT_CLASS_WARNING, HILIGHT_CLASS_OK, HILIGHT_CLASS_NONE
  54. * To Be overridden by derived classes
  55. * @param void
  56. * @return String The desired higlight class for the object/row
  57. */
  58. public function GetHilightClass();
  59. /**
  60. * Returns the relative path to the page that handles the display of the object
  61. * @return string
  62. */
  63. public static function GetUIPage();
  64. /**
  65. * Displays the details of the object
  66. */
  67. public function DisplayDetails(WebPage $oPage, $bEditMode = false);
  68. }
  69. abstract class cmdbAbstractObject extends CMDBObject implements iDisplay
  70. {
  71. protected $m_iFormId; // The ID of the form used to edit the object (when in edition mode !)
  72. public static function GetUIPage()
  73. {
  74. return '../pages/UI.php';
  75. }
  76. function DisplayBareHeader(WebPage $oPage, $bEditMode = false)
  77. {
  78. // Standard Header with name, actions menu and history block
  79. //
  80. // action menu
  81. $oSingletonFilter = new DBObjectSearch(get_class($this));
  82. $oSingletonFilter->AddCondition('id', $this->GetKey(), '=');
  83. $oBlock = new MenuBlock($oSingletonFilter, 'popup', false);
  84. $oBlock->Display($oPage, -1);
  85. // Master data sources
  86. $oReplicaSet = $this->GetMasterReplica();
  87. $bSynchronized = false;
  88. $bCreated = false;
  89. $bCanBeDeleted = false;
  90. $aMasterSources = array();
  91. if ($oReplicaSet->Count() > 0)
  92. {
  93. $bSynchronized = true;
  94. $sTip = "<p>The object is synchronized with an external data source</p>";
  95. while($aData = $oReplicaSet->FetchAssoc())
  96. {
  97. // Assumption: $aData['datasource'] will not be null because the data source id is always set...
  98. $sApplicationURL = $aData['datasource']->GetApplicationUrl($this, $aData['replica']);
  99. $sLink = '';
  100. if (!empty($sApplicationURL))
  101. {
  102. $sLink = "<a href=\"$sApplicationURL\" target=\"_blank\">".$aData['datasource']->GetName()."</a>";
  103. }
  104. if ($aData['replica']->Get('status_dest_creator') == 1)
  105. {
  106. $sTip .= "<p>The object was <b>created</b> by the external data source $sLink</p>";
  107. $bCreated = true;
  108. }
  109. if ($bCreated)
  110. {
  111. $sDeletePolicy = $aData['datasource']->Get('delete_policy');
  112. if (($sDeletePolicy == 'delete') || ($sDeletePolicy == 'update_then_delete'))
  113. {
  114. $bCanBeDeleted = true;
  115. $sTip .= "<p>The object <b>can be deleted</b> by the external data source $sLink</p>";
  116. }
  117. }
  118. $aMasterSources[$aData['datasource']->GetKey()]['datasource'] = $aData['datasource'];
  119. $aMasterSources[$aData['datasource']->GetKey()]['url'] = $sLink;
  120. }
  121. }
  122. $sSynchroIcon = '';
  123. if ($bSynchronized)
  124. {
  125. $sTip .= "<p><b>List of data sources:</b></p>";
  126. foreach($aMasterSources as $aStruct)
  127. {
  128. $oDataSource = $aStruct['datasource'];
  129. $sLink = $aStruct['url'];
  130. $sTip .= "<p style=\"white-space:nowrap\">".$oDataSource->GetIcon(true, 'style="vertical-align:middle"')."&nbsp;$sLink</p>";
  131. }
  132. $sSynchroIcon = '&nbsp;<img style="vertical-align:middle;" id="synchro_icon" src="../images/locked.png"/>';
  133. $oPage->add_ready_script("$('#synchro_icon').qtip( { content: '$sTip', show: 'mouseover', hide: 'unfocus', style: { name: 'dark', tip: 'leftTop' }, position: { corner: { target: 'rightMiddle', tooltip: 'leftTop' }} } );");
  134. }
  135. $oPage->add("<div class=\"page_header\"><h1>".$this->GetIcon()."&nbsp;\n");
  136. $oPage->add(MetaModel::GetName(get_class($this)).": <span class=\"hilite\">".$this->GetName()."</span>$sSynchroIcon</h1>\n");
  137. $oPage->add("</div>\n");
  138. }
  139. function DisplayBareHistory(WebPage $oPage, $bEditMode = false)
  140. {
  141. // history block (with as a tab)
  142. $oHistoryFilter = new DBObjectSearch('CMDBChangeOp');
  143. $oHistoryFilter->AddCondition('objkey', $this->GetKey(), '=');
  144. $oHistoryFilter->AddCondition('objclass', get_class($this), '=');
  145. $oBlock = new HistoryBlock($oHistoryFilter, 'table', false);
  146. $oBlock->Display($oPage, -1);
  147. }
  148. function DisplayBareProperties(WebPage $oPage, $bEditMode = false)
  149. {
  150. $oPage->add($this->GetBareProperties($oPage, $bEditMode));
  151. foreach (MetaModel::EnumPlugins('iApplicationUIExtension') as $oExtensionInstance)
  152. {
  153. $oExtensionInstance->OnDisplayProperties($this, $oPage, $bEditMode);
  154. }
  155. }
  156. function DisplayBareRelations(WebPage $oPage, $bEditMode = false)
  157. {
  158. // Related objects: display all the linkset attributes, each as a separate tab
  159. // In the order described by the 'display' ZList
  160. $aList = $this->FlattenZList(MetaModel::GetZListItems(get_class($this), 'details'));
  161. if (count($aList) == 0)
  162. {
  163. // Empty ZList defined, display all the linkedset attributes defined
  164. $aList = array_keys(MetaModel::ListAttributeDefs(get_class($this)));
  165. }
  166. $sClass = get_class($this);
  167. foreach($aList as $sAttCode)
  168. {
  169. $oAttDef = MetaModel::GetAttributeDef(get_class($this), $sAttCode);
  170. // Display mode
  171. if (!$oAttDef->IsLinkset()) continue; // Process only linkset attributes...
  172. $oPage->SetCurrentTab($oAttDef->GetLabel());
  173. if ($bEditMode)
  174. {
  175. $iFlags = $this->GetAttributeFlags($sAttCode);
  176. $sInputId = $this->m_iFormId.'_'.$sAttCode;
  177. if (get_class($oAttDef) == 'AttributeLinkedSet')
  178. {
  179. // 1:n links
  180. $sTargetClass = $oAttDef->GetLinkedClass();
  181. if ($this->IsNew())
  182. {
  183. $oPage->p(Dict::Format('UI:BeforeAdding_Class_ObjectsSaveThisObject', MetaModel::GetName($sTargetClass)));
  184. }
  185. else
  186. {
  187. $oPage->p(MetaModel::GetClassIcon($sTargetClass)."&nbsp;".$oAttDef->GetDescription());
  188. $oFilter = new DBObjectSearch($sTargetClass);
  189. $oFilter->AddCondition($oAttDef->GetExtKeyToMe(), $this->GetKey(),'=');
  190. $aDefaults = array($oAttDef->GetExtKeyToMe() => $this->GetKey());
  191. $oAppContext = new ApplicationContext();
  192. foreach($oAppContext->GetNames() as $sKey)
  193. {
  194. // The linked object inherits the parent's value for the context
  195. if (MetaModel::IsValidAttCode($sClass, $sKey))
  196. {
  197. $aDefaults[$sKey] = $this->Get($sKey);
  198. }
  199. }
  200. $aParams = array(
  201. 'target_attr' => $oAttDef->GetExtKeyToMe(),
  202. 'object_id' => $this->GetKey(),
  203. 'menu' => true,
  204. 'default' => $aDefaults,
  205. );
  206. $oBlock = new DisplayBlock($oFilter, 'list', false);
  207. $oBlock->Display($oPage, $sInputId, $aParams);
  208. }
  209. }
  210. else // get_class($oAttDef) == 'AttributeLinkedSetIndirect'
  211. {
  212. // n:n links
  213. $sLinkedClass = $oAttDef->GetLinkedClass();
  214. $oLinkingAttDef = MetaModel::GetAttributeDef($sLinkedClass, $oAttDef->GetExtKeyToRemote());
  215. $sTargetClass = $oLinkingAttDef->GetTargetClass();
  216. $oPage->p(MetaModel::GetClassIcon($sTargetClass)."&nbsp;".$oAttDef->GetDescription().'<span id="busy_'.$sInputId.'"></span>');
  217. $sValue = $this->Get($sAttCode);
  218. $sDisplayValue = $this->GetEditValue($sAttCode);
  219. $aArgs = array('this' => $this);
  220. $sHTMLValue = "<span id=\"field_{$sInputId}\">".self::GetFormElementForField($oPage, $sClass, $sAttCode, $oAttDef, $sValue, $sDisplayValue, $sInputId, '', $iFlags, $aArgs).'</span>';
  221. $aFieldsMap[$sAttCode] = $sInputId;
  222. $oPage->add($sHTMLValue);
  223. }
  224. }
  225. else
  226. {
  227. // Display mode
  228. if (!$oAttDef->IsIndirect())
  229. {
  230. // 1:n links
  231. $sTargetClass = $oAttDef->GetLinkedClass();
  232. $aDefaults = array($oAttDef->GetExtKeyToMe() => $this->GetKey());
  233. $oAppContext = new ApplicationContext();
  234. foreach($oAppContext->GetNames() as $sKey)
  235. {
  236. // The linked object inherits the parent's value for the context
  237. if (MetaModel::IsValidAttCode($sClass, $sKey))
  238. {
  239. $aDefaults[$sKey] = $this->Get($sKey);
  240. }
  241. }
  242. $aParams = array(
  243. 'target_attr' => $oAttDef->GetExtKeyToMe(),
  244. 'object_id' => $this->GetKey(),
  245. 'menu' => false,
  246. 'default' => $aDefaults,
  247. );
  248. }
  249. else
  250. {
  251. // n:n links
  252. $sLinkedClass = $oAttDef->GetLinkedClass();
  253. $oLinkingAttDef = MetaModel::GetAttributeDef($sLinkedClass, $oAttDef->GetExtKeyToRemote());
  254. $sTargetClass = $oLinkingAttDef->GetTargetClass();
  255. $bMenu = ($this->Get($sAttCode)->Count() > 0); // The menu is enabled only if there are already some elements...
  256. $aParams = array(
  257. 'link_attr' => $oAttDef->GetExtKeyToMe(),
  258. 'object_id' => $this->GetKey(),
  259. 'target_attr' => $oAttDef->GetExtKeyToRemote(),
  260. 'view_link' => false,
  261. 'menu' => false,
  262. 'display_limit' => true, // By default limit the list to speed up the initial load & display
  263. );
  264. }
  265. $oPage->p(MetaModel::GetClassIcon($sTargetClass)."&nbsp;".$oAttDef->GetDescription());
  266. $oBlock = new DisplayBlock($this->Get($sAttCode)->GetFilter(), 'list', false);
  267. $oBlock->Display($oPage, 'rel_'.$sAttCode, $aParams);
  268. }
  269. }
  270. $oPage->SetCurrentTab('');
  271. if (!$bEditMode)
  272. {
  273. // Get the actual class of the current object
  274. // And look for triggers referring to it
  275. // If any trigger has been found then display a tab with notifications
  276. //
  277. $sClass = get_class($this);
  278. $sClassList = implode("', '", MetaModel::EnumParentClasses($sClass, ENUM_PARENT_CLASSES_ALL));
  279. $oTriggerSet = new CMDBObjectSet(DBObjectSearch::FromOQL("SELECT TriggerOnObject AS T WHERE T.target_class IN ('$sClassList')"));
  280. if ($oTriggerSet->Count() > 0)
  281. {
  282. $oPage->SetCurrentTab(Dict::S('UI:NotificationsTab'));
  283. // Display notifications regarding the object
  284. $iId = $this->GetKey();
  285. $oBlock = new DisplayBlock(DBObjectSearch::FromOQL("SELECT EventNotificationEmail AS Ev JOIN TriggerOnObject AS T ON Ev.trigger_id = T.id WHERE T.target_class IN ('$sClassList') AND Ev.object_id = $iId"), 'list', false);
  286. $oBlock->Display($oPage, 'notifications', array());
  287. }
  288. }
  289. foreach (MetaModel::EnumPlugins('iApplicationUIExtension') as $oExtensionInstance)
  290. {
  291. $oExtensionInstance->OnDisplayRelations($this, $oPage, $bEditMode);
  292. }
  293. }
  294. function GetBareProperties(WebPage $oPage, $bEditMode = false)
  295. {
  296. $sHtml = '';
  297. $oAppContext = new ApplicationContext();
  298. $sStateAttCode = MetaModel::GetStateAttributeCode(get_class($this));
  299. $aDetails = array();
  300. $sClass = get_class($this);
  301. $aDetailsList = MetaModel::GetZListItems($sClass, 'details');
  302. $aDetailsStruct = self::ProcessZlist($aDetailsList, array('UI:PropertiesTab' => array()), 'UI:PropertiesTab', 'col1', '');
  303. // Compute the list of properties to display, first the attributes in the 'details' list, then
  304. // all the remaining attributes that are not external fields
  305. $sHtml = '';
  306. $aDetails = array();
  307. $iInputId = 0;
  308. foreach($aDetailsStruct as $sTab => $aCols )
  309. {
  310. $aDetails[$sTab] = array();
  311. ksort($aCols);
  312. $oPage->SetCurrentTab(Dict::S($sTab));
  313. $oPage->add('<table style="vertical-align:top"><tr>');
  314. foreach($aCols as $sColIndex => $aFieldsets)
  315. {
  316. $oPage->add('<td style="vertical-align:top">');
  317. //$aDetails[$sTab][$sColIndex] = array();
  318. $sLabel = '';
  319. $sPreviousLabel = '';
  320. $aDetails[$sTab][$sColIndex] = array();
  321. foreach($aFieldsets as $sFieldsetName => $aFields)
  322. {
  323. if (!empty($sFieldsetName) && ($sFieldsetName[0] != '_'))
  324. {
  325. $sLabel = $sFieldsetName;
  326. }
  327. else
  328. {
  329. $sLabel = '';
  330. }
  331. if ($sLabel != $sPreviousLabel)
  332. {
  333. if (!empty($sPreviousLabel))
  334. {
  335. $oPage->add('<fieldset>');
  336. $oPage->add('<legend>'.Dict::S($sPreviousLabel).'</legend>');
  337. }
  338. $oPage->Details($aDetails[$sTab][$sColIndex]);
  339. if (!empty($sPreviousLabel))
  340. {
  341. $oPage->add('</fieldset>');
  342. }
  343. $aDetails[$sTab][$sColIndex] = array();
  344. $sPreviousLabel = $sLabel;
  345. }
  346. foreach($aFields as $sAttCode)
  347. {
  348. $val = $this->GetFieldAsHtml($sClass, $sAttCode, $sStateAttCode);
  349. if ($val != null)
  350. {
  351. // Check if the attribute is not mastered by a synchro...
  352. $aReasons = array();
  353. $iSynchroFlags = $this->GetSynchroReplicaFlags($sAttCode, $aReasons);
  354. $sSynchroIcon = '';
  355. if ($iSynchroFlags & OPT_ATT_READONLY)
  356. {
  357. $sSynchroIcon = "&nbsp;<img id=\"synchro_$iInputId\" src=\"../images/transp-lock.png\" style=\"vertical-align:middle\"/>";
  358. $sTip = '';
  359. foreach($aReasons as $aRow)
  360. {
  361. $sTip .= "<p>Synchronized with {$aRow['name']} - {$aRow['description']}</p>";
  362. }
  363. $oPage->add_ready_script("$('#synchro_$iInputId').qtip( { content: '$sTip', show: 'mouseover', hide: 'mouseout', style: { name: 'dark', tip: 'leftTop' }, position: { corner: { target: 'rightMiddle', tooltip: 'leftTop' }} } );");
  364. }
  365. $val['value'] .= $sSynchroIcon;
  366. // The field is visible, add it to the current column
  367. $aDetails[$sTab][$sColIndex][] = $val;
  368. $iInputId++;
  369. }
  370. }
  371. }
  372. if (!empty($sPreviousLabel))
  373. {
  374. $oPage->add('<fieldset>');
  375. $oPage->add('<legend>'.Dict::S($sFieldsetName).'</legend>');
  376. }
  377. $oPage->Details($aDetails[$sTab][$sColIndex]);
  378. if (!empty($sPreviousLabel))
  379. {
  380. $oPage->add('</fieldset>');
  381. }
  382. $oPage->add('</td>');
  383. }
  384. $oPage->add('</tr></table>');
  385. }
  386. return $sHtml;
  387. }
  388. function DisplayDetails(WebPage $oPage, $bEditMode = false)
  389. {
  390. $sTemplate = Utils::ReadFromFile(MetaModel::GetDisplayTemplate(get_class($this)));
  391. if (!empty($sTemplate))
  392. {
  393. $oTemplate = new DisplayTemplate($sTemplate);
  394. // Note: to preserve backward compatibility with home-made templates, the placeholder '$pkey$' has been preserved
  395. // but the preferred method is to use '$id$'
  396. $oTemplate->Render($oPage, array('class_name'=> MetaModel::GetName(get_class($this)),'class'=> get_class($this), 'pkey'=> $this->GetKey(), 'id'=> $this->GetKey(), 'name' => $this->Get('friendlyname')));
  397. }
  398. else
  399. {
  400. // Object's details
  401. // template not found display the object using the *old style*
  402. $this->DisplayBareHeader($oPage, $bEditMode);
  403. $oPage->AddTabContainer(OBJECT_PROPERTIES_TAB);
  404. $oPage->SetCurrentTabContainer(OBJECT_PROPERTIES_TAB);
  405. $oPage->SetCurrentTab(Dict::S('UI:PropertiesTab'));
  406. $this->DisplayBareProperties($oPage, $bEditMode);
  407. $this->DisplayBareRelations($oPage, $bEditMode);
  408. $oPage->SetCurrentTab(Dict::S('UI:HistoryTab'));
  409. $this->DisplayBareHistory($oPage, $bEditMode);
  410. }
  411. }
  412. function DisplayPreview(WebPage $oPage)
  413. {
  414. $aDetails = array();
  415. $sClass = get_class($this);
  416. $aList = MetaModel::GetZListItems($sClass, 'preview');
  417. foreach($aList as $sAttCode)
  418. {
  419. $aDetails[] = array('label' => MetaModel::GetLabel($sClass, $sAttCode), 'value' =>$this->GetAsHTML($sAttCode));
  420. }
  421. $oPage->details($aDetails);
  422. }
  423. public static function DisplaySet(WebPage $oPage, CMDBObjectSet $oSet, $aExtraParams = array())
  424. {
  425. $oPage->add(self::GetDisplaySet($oPage, $oSet, $aExtraParams));
  426. }
  427. /**
  428. * Get the HTML fragment corresponding to the display of a table representing a set of objects
  429. * @param WebPage $oPage The page object is used for out-of-band information (mostly scripts) output
  430. * @param CMDBObjectSet The set of objects to display
  431. * @param Hash $aExtraParams Some extra configuration parameters to tweak the behavior of the display
  432. * @return String The HTML fragment representing the table of objects
  433. */
  434. public static function GetDisplaySet(WebPage $oPage, CMDBObjectSet $oSet, $aExtraParams = array())
  435. {
  436. if (empty($aExtraParams['currentId']))
  437. {
  438. $iListId = $oPage->GetUniqueId(); // Works only if not in an Ajax page !!
  439. }
  440. else
  441. {
  442. $iListId = $aExtraParams['currentId'];
  443. }
  444. // Initialize and check the parameters
  445. $bViewLink = isset($aExtraParams['view_link']) ? $aExtraParams['view_link'] : true;
  446. $sLinkageAttribute = isset($aExtraParams['link_attr']) ? $aExtraParams['link_attr'] : '';
  447. $iLinkedObjectId = isset($aExtraParams['object_id']) ? $aExtraParams['object_id'] : 0;
  448. $sTargetAttr = isset($aExtraParams['target_attr']) ? $aExtraParams['target_attr'] : '';
  449. if (!empty($sLinkageAttribute))
  450. {
  451. if($iLinkedObjectId == 0)
  452. {
  453. // if 'links' mode is requested the id of the object to link to must be specified
  454. throw new ApplicationException(Dict::S('UI:Error:MandatoryTemplateParameter_object_id'));
  455. }
  456. if($sTargetAttr == '')
  457. {
  458. // if 'links' mode is requested the d of the object to link to must be specified
  459. throw new ApplicationException(Dict::S('UI:Error:MandatoryTemplateParameter_target_attr'));
  460. }
  461. }
  462. $bDisplayMenu = isset($aExtraParams['menu']) ? $aExtraParams['menu'] == true : true;
  463. $bTruncated = isset($aExtraParams['truncated']) ? $aExtraParams['truncated'] == true : true;
  464. $bSelectMode = isset($aExtraParams['selection_mode']) ? $aExtraParams['selection_mode'] == true : false;
  465. $bSingleSelectMode = isset($aExtraParams['selection_type']) ? ($aExtraParams['selection_type'] == 'single') : false;
  466. $aExtraFields = isset($aExtraParams['extra_fields']) ? explode(',', trim($aExtraParams['extra_fields'])) : array();
  467. $sHtml = '';
  468. $oAppContext = new ApplicationContext();
  469. $sClassName = $oSet->GetFilter()->GetClass();
  470. $aAttribs = array();
  471. $sZListName = isset($aExtraParams['zlist']) ? ($aExtraParams['zlist']) : 'list';
  472. $aList = self::FlattenZList(MetaModel::GetZListItems($sClassName, $sZListName));
  473. $aList = array_merge($aList, $aExtraFields);
  474. // Filter the list to removed linked set since we are not able to display them here
  475. foreach($aList as $index => $sAttCode)
  476. {
  477. $oAttDef = MetaModel::GetAttributeDef($sClassName, $sAttCode);
  478. if ($oAttDef instanceof AttributeLinkedSet)
  479. {
  480. // Removed from the display list
  481. unset($aList[$index]);
  482. }
  483. }
  484. if (!empty($sLinkageAttribute))
  485. {
  486. // The set to display is in fact a set of links between the object specified in the $sLinkageAttribute
  487. // and other objects...
  488. // The display will then group all the attributes related to the link itself:
  489. // | Link_attr1 | link_attr2 | ... || Object_attr1 | Object_attr2 | Object_attr3 | .. | Object_attr_n |
  490. $aDisplayList = array();
  491. $aAttDefs = MetaModel::ListAttributeDefs($sClassName);
  492. assert(isset($aAttDefs[$sLinkageAttribute]));
  493. $oAttDef = $aAttDefs[$sLinkageAttribute];
  494. assert($oAttDef->IsExternalKey());
  495. // First display all the attributes specific to the link record
  496. foreach($aList as $sLinkAttCode)
  497. {
  498. $oLinkAttDef = $aAttDefs[$sLinkAttCode];
  499. if ( (!$oLinkAttDef->IsExternalKey()) && (!$oLinkAttDef->IsExternalField()) )
  500. {
  501. $aDisplayList[] = $sLinkAttCode;
  502. }
  503. }
  504. // Then display all the attributes neither specific to the link record nor to the 'linkage' object (because the latter are constant)
  505. foreach($aList as $sLinkAttCode)
  506. {
  507. $oLinkAttDef = $aAttDefs[$sLinkAttCode];
  508. if (($oLinkAttDef->IsExternalKey() && ($sLinkAttCode != $sLinkageAttribute))
  509. || ($oLinkAttDef->IsExternalField() && ($oLinkAttDef->GetKeyAttCode()!=$sLinkageAttribute)) )
  510. {
  511. $aDisplayList[] = $sLinkAttCode;
  512. }
  513. }
  514. // First display all the attributes specific to the link
  515. // Then display all the attributes linked to the other end of the relationship
  516. $aList = $aDisplayList;
  517. }
  518. if ($bSelectMode)
  519. {
  520. if (!$bSingleSelectMode)
  521. {
  522. $aAttribs['form::select'] = array('label' => "<input type=\"checkbox\" onClick=\"CheckAll('.selectList{$iListId}', this.checked);\"></input>", 'description' => Dict::S('UI:SelectAllToggle+'));
  523. }
  524. else
  525. {
  526. $aAttribs['form::select'] = array('label' => "", 'description' => '');
  527. }
  528. }
  529. if ($bViewLink)
  530. {
  531. $aAttribs['key'] = array('label' => MetaModel::GetName($sClassName), 'description' => '');
  532. }
  533. foreach($aList as $sAttCode)
  534. {
  535. $aAttribs[$sAttCode] = array('label' => MetaModel::GetLabel($sClassName, $sAttCode), 'description' => MetaModel::GetDescription($sClassName, $sAttCode));
  536. }
  537. $aValues = array();
  538. $bDisplayLimit = isset($aExtraParams['display_limit']) ? $aExtraParams['display_limit'] : true;
  539. $iMaxObjects = -1;
  540. if ($bDisplayLimit && $bTruncated)
  541. {
  542. if ($oSet->Count() > MetaModel::GetConfig()->GetMaxDisplayLimit())
  543. {
  544. $iMaxObjects = MetaModel::GetConfig()->GetMinDisplayLimit();
  545. $oSet->SetLimit($iMaxObjects);
  546. }
  547. }
  548. $oSet->Seek(0);
  549. while (($oObj = $oSet->Fetch()) && ($iMaxObjects != 0))
  550. {
  551. $aRow = array();
  552. $sHilightClass = $oObj->GetHilightClass();
  553. if ($sHilightClass != '')
  554. {
  555. $aRow['@class'] = $sHilightClass;
  556. }
  557. if ($bViewLink)
  558. {
  559. $aRow['key'] = $oObj->GetHyperLink();
  560. }
  561. if ($bSelectMode)
  562. {
  563. if ($bSingleSelectMode)
  564. {
  565. $aRow['form::select'] = "<input type=\"radio\" class=\"selectList{$iListId}\" name=\"selectObject\" value=\"".$oObj->GetKey()."\"></input>";
  566. }
  567. else
  568. {
  569. $aRow['form::select'] = "<input type=\"checkBox\" class=\"selectList{$iListId}\" name=\"selectObject[]\" value=\"".$oObj->GetKey()."\"></input>";
  570. }
  571. }
  572. foreach($aList as $sAttCode)
  573. {
  574. $aRow[$sAttCode] = $oObj->GetAsHTML($sAttCode);
  575. }
  576. $aValues[] = $aRow;
  577. $iMaxObjects--;
  578. }
  579. $sHtml .= '<table class="listContainer">';
  580. $sColspan = '';
  581. // if (isset($aExtraParams['block_id']))
  582. // {
  583. // $divId = $aExtraParams['block_id'];
  584. // }
  585. // else
  586. // {
  587. // $divId = 'missingblockid';
  588. // }
  589. $sFilter = $oSet->GetFilter()->serialize();
  590. $iMinDisplayLimit = MetaModel::GetConfig()->GetMinDisplayLimit();
  591. $sCollapsedLabel = Dict::Format('UI:TruncatedResults', $iMinDisplayLimit, $oSet->Count());
  592. $sLinkLabel = Dict::S('UI:DisplayAll');
  593. foreach($oSet->GetFilter()->GetInternalParams() as $sName => $sValue)
  594. {
  595. $aExtraParams['query_params'][$sName] = $sValue;
  596. }
  597. if ($bDisplayLimit && $bTruncated && ($oSet->Count() > MetaModel::GetConfig()->GetMaxDisplayLimit()))
  598. {
  599. // list truncated
  600. $aExtraParams['display_limit'] = true;
  601. $sHtml .= '<tr class="containerHeader"><td><span id="lbl_'.$iListId.'">'.$sCollapsedLabel.'</span>&nbsp;&nbsp;<a class="truncated" id="trc_'.$iListId.'">'.$sLinkLabel.'</a></td><td>';
  602. $oPage->add_ready_script(
  603. <<<EOF
  604. $('#$iListId table.listResults').addClass('truncated');
  605. $('#$iListId table.listResults tr:last td').addClass('truncated');
  606. EOF
  607. );
  608. }
  609. else if ($bDisplayLimit && !$bTruncated && ($oSet->Count() > MetaModel::GetConfig()->GetMaxDisplayLimit()))
  610. {
  611. // Collapsible list
  612. $aExtraParams['display_limit'] = true;
  613. $sHtml .= '<tr class="containerHeader"><td><span id="lbl_'.$iListId.'">'.Dict::Format('UI:CountOfResults', $oSet->Count()).'</span><a class="truncated" id="trc_'.$iListId.'">'.Dict::S('UI:CollapseList').'</a></td><td>';
  614. }
  615. $aExtraParams['truncated'] = false; // To expand the full list when clicked
  616. $sExtraParamsExpand = addslashes(str_replace('"', "'", json_encode($aExtraParams))); // JSON encode, change the style of the quotes and escape them
  617. $oPage->add_ready_script(
  618. <<<EOF
  619. // Handle truncated lists
  620. $('#trc_$iListId').click(function()
  621. {
  622. var state = {};
  623. var currentState = $.bbq.getState( this.id, true ) || 'close';
  624. // Toggle the state!
  625. if (currentState == 'close')
  626. {
  627. state[ this.id ] = 'open';
  628. }
  629. else
  630. {
  631. state[ this.id ] = 'close';
  632. }
  633. $.bbq.pushState( state );
  634. $(this).trigger(state[this.id]);
  635. });
  636. $('#trc_$iListId').unbind('open');
  637. $('#trc_$iListId').bind('open', function()
  638. {
  639. ReloadTruncatedList('$iListId', '$sFilter', '$sExtraParamsExpand');
  640. });
  641. $('#trc_$iListId').unbind('close');
  642. $('#trc_$iListId').bind('close', function()
  643. {
  644. TruncateList('$iListId', $iMinDisplayLimit, '$sCollapsedLabel', '$sLinkLabel');
  645. });
  646. EOF
  647. );
  648. if ($bDisplayMenu)
  649. {
  650. $oMenuBlock = new MenuBlock($oSet->GetFilter());
  651. $sColspan = 'colspan="2"';
  652. $aMenuExtraParams = $aExtraParams;
  653. if (!empty($sLinkageAttribute))
  654. {
  655. //$aMenuExtraParams['linkage'] = $sLinkageAttribute;
  656. $aMenuExtraParams = $aExtraParams;
  657. }
  658. $sHtml .= $oMenuBlock->GetRenderContent($oPage, $aMenuExtraParams, $iListId);
  659. $sHtml .= '</td></tr>';
  660. }
  661. $sHtml .= "<tr><td $sColspan>";
  662. $sHtml .= $oPage->GetTable($aAttribs, $aValues);
  663. $sHtml .= '</td></tr>';
  664. $sHtml .= '</table>';
  665. return $sHtml;
  666. }
  667. public static function GetDisplayExtendedSet(WebPage $oPage, CMDBObjectSet $oSet, $aExtraParams = array())
  668. {
  669. static $iListId = 0;
  670. $iListId++;
  671. $aList = array();
  672. // Initialize and check the parameters
  673. $bViewLink = isset($aExtraParams['view_link']) ? $aExtraParams['view_link'] : true;
  674. $bDisplayMenu = isset($aExtraParams['menu']) ? $aExtraParams['menu'] == true : true;
  675. // Check if there is a list of aliases to limit the display to...
  676. $aDisplayAliases = isset($aExtraParams['display_aliases']) ? explode(',', $aExtraParams['display_aliases']) : array();
  677. $sHtml = '';
  678. $oAppContext = new ApplicationContext();
  679. $aClasses = $oSet->GetFilter()->GetSelectedClasses();
  680. $aAuthorizedClasses = array();
  681. foreach($aClasses as $sAlias => $sClassName)
  682. {
  683. if ( (UserRights::IsActionAllowed($sClassName, UR_ACTION_READ, $oSet) && (UR_ALLOWED_YES || UR_ALLOWED_DEPENDS)) &&
  684. ( (count($aDisplayAliases) == 0) || (in_array($sAlias, $aDisplayAliases))) )
  685. {
  686. $aAuthorizedClasses[$sAlias] = $sClassName;
  687. }
  688. }
  689. $aAttribs = array();
  690. foreach($aAuthorizedClasses as $sAlias => $sClassName) // TO DO: check if the user has enough rights to view the classes of the list...
  691. {
  692. $aList[$sClassName] = MetaModel::GetZListItems($sClassName, 'list');
  693. if ($bViewLink)
  694. {
  695. $aAttribs['key_'.$sAlias] = array('label' => MetaModel::GetName($sClassName), 'description' => '');
  696. }
  697. foreach($aList[$sClassName] as $sAttCode)
  698. {
  699. $aAttribs[$sAttCode.'_'.$sAlias] = array('label' => MetaModel::GetLabel($sClassName, $sAttCode), 'description' => MetaModel::GetDescription($sClassName, $sAttCode));
  700. }
  701. }
  702. $aValues = array();
  703. $oSet->Seek(0);
  704. $bDisplayLimit = isset($aExtraParams['display_limit']) ? $aExtraParams['display_limit'] : true;
  705. $iMaxObjects = -1;
  706. if ($bDisplayLimit)
  707. {
  708. if ($oSet->Count() > MetaModel::GetConfig()->GetMaxDisplayLimit())
  709. {
  710. $iMaxObjects = MetaModel::GetConfig()->GetMinDisplayLimit();
  711. }
  712. }
  713. while (($aObjects = $oSet->FetchAssoc()) && ($iMaxObjects != 0))
  714. {
  715. $aRow = array();
  716. foreach($aAuthorizedClasses as $sAlias => $sClassName) // TO DO: check if the user has enough rights to view the classes of the list...
  717. {
  718. if ($bViewLink)
  719. {
  720. if (is_null($aObjects[$sAlias]))
  721. {
  722. $aRow['key_'.$sAlias] = '';
  723. }
  724. else
  725. {
  726. $aRow['key_'.$sAlias] = $aObjects[$sAlias]->GetHyperLink();
  727. }
  728. }
  729. foreach($aList[$sClassName] as $sAttCode)
  730. {
  731. if (is_null($aObjects[$sAlias]))
  732. {
  733. $aRow[$sAttCode.'_'.$sAlias] = '';
  734. }
  735. else
  736. {
  737. $aRow[$sAttCode.'_'.$sAlias] = $aObjects[$sAlias]->GetAsHTML($sAttCode);
  738. }
  739. }
  740. }
  741. $aValues[] = $aRow;
  742. $iMaxObjects--;
  743. }
  744. $sHtml .= '<table class="listContainer">';
  745. $sColspan = '';
  746. if ($bDisplayMenu)
  747. {
  748. $oMenuBlock = new MenuBlock($oSet->GetFilter());
  749. $sColspan = 'colspan="2"';
  750. $aMenuExtraParams = $aExtraParams;
  751. if (!empty($sLinkageAttribute))
  752. {
  753. $aMenuExtraParams = $aExtraParams;
  754. }
  755. if ($bDisplayLimit && ($oSet->Count() > MetaModel::GetConfig()->GetMaxDisplayLimit()))
  756. {
  757. // list truncated
  758. $divId = $aExtraParams['block_id'];
  759. $sFilter = $oSet->GetFilter()->serialize();
  760. $aExtraParams['display_limit'] = false; // To expand the full list
  761. $sExtraParams = addslashes(str_replace('"', "'", json_encode($aExtraParams))); // JSON encode, change the style of the quotes and escape them
  762. $sHtml .= '<tr class="containerHeader"><td>'.Dict::Format('UI:TruncatedResults', MetaModel::GetConfig()->GetMinDisplayLimit(), $oSet->Count()).'&nbsp;&nbsp;<a href="Javascript:ReloadTruncatedList(\''.$divId.'\', \''.$sFilter.'\', \''.$sExtraParams.'\');">'.Dict::S('UI:DisplayAll').'</a></td><td>';
  763. $oPage->add_ready_script("$('#{$divId} table.listResults').addClass('truncated');");
  764. $oPage->add_ready_script("$('#{$divId} table.listResults tr:last td').addClass('truncated');");
  765. }
  766. else
  767. {
  768. // Full list
  769. $sHtml .= '<tr class="containerHeader"><td>&nbsp;'.Dict::Format('UI:CountOfResults', $oSet->Count()).'</td><td>';
  770. }
  771. $sHtml .= $oMenuBlock->GetRenderContent($oPage, $aMenuExtraParams, $aMenuExtraParams['currentId']);
  772. $sHtml .= '</td></tr>';
  773. }
  774. $sHtml .= "<tr><td $sColspan>";
  775. $sHtml .= $oPage->GetTable($aAttribs, $aValues);
  776. $sHtml .= '</td></tr>';
  777. $sHtml .= '</table>';
  778. return $sHtml;
  779. }
  780. static function DisplaySetAsCSV(WebPage $oPage, CMDBObjectSet $oSet, $aParams = array())
  781. {
  782. $oPage->add(self::GetSetAsCSV($oSet, $aParams));
  783. }
  784. static function GetSetAsCSV(DBObjectSet $oSet, $aParams = array())
  785. {
  786. $sSeparator = isset($aParams['separator']) ? $aParams['separator'] : ','; // default separator is comma
  787. $sTextQualifier = isset($aParams['text_qualifier']) ? $aParams['text_qualifier'] : '"'; // default text qualifier is double quote
  788. $aFields = isset($aParams['fields']) ? explode(',', $aParams['fields']) : null;
  789. $aList = array();
  790. $oAppContext = new ApplicationContext();
  791. $aClasses = $oSet->GetFilter()->GetSelectedClasses();
  792. $aAuthorizedClasses = array();
  793. foreach($aClasses as $sAlias => $sClassName)
  794. {
  795. if (UserRights::IsActionAllowed($sClassName, UR_ACTION_READ, $oSet) && (UR_ALLOWED_YES || UR_ALLOWED_DEPENDS))
  796. {
  797. $aAuthorizedClasses[$sAlias] = $sClassName;
  798. }
  799. }
  800. $aAttribs = array();
  801. $aHeader = array();
  802. foreach($aAuthorizedClasses as $sAlias => $sClassName)
  803. {
  804. foreach(MetaModel::ListAttributeDefs($sClassName) as $sAttCode => $oAttDef)
  805. {
  806. if (!is_null($aFields) && !in_array($sAttCode, $aFields)) continue;
  807. if ($oAttDef->IsExternalField() || $oAttDef->IsWritable())
  808. {
  809. $aList[$sClassName][$sAttCode] = $oAttDef;
  810. }
  811. }
  812. $aHeader[] = 'id';
  813. foreach($aList[$sClassName] as $sAttCode => $oAttDef)
  814. {
  815. $sStar = '';
  816. if ($oAttDef->IsExternalField())
  817. {
  818. $sExtKeyLabel = MetaModel::GetLabel($sClassName, $oAttDef->GetKeyAttCode());
  819. $oExtKeyAttDef = MetaModel::GetAttributeDef($sClassName, $oAttDef->GetKeyAttCode());
  820. if (!$oExtKeyAttDef->IsNullAllowed() && isset($aParams['showMandatoryFields']))
  821. {
  822. $sStar = '*';
  823. }
  824. $sRemoteAttLabel = MetaModel::GetLabel($oAttDef->GetTargetClass(), $oAttDef->GetExtAttCode());
  825. $oTargetAttDef = MetaModel::GetAttributeDef($oAttDef->GetTargetClass(), $oAttDef->GetExtAttCode());
  826. $sSuffix = '';
  827. if ($oTargetAttDef->IsExternalKey())
  828. {
  829. $sSuffix = '->id';
  830. }
  831. $aHeader[] = $sExtKeyLabel.'->'.$sRemoteAttLabel.$sSuffix.$sStar;
  832. }
  833. else
  834. {
  835. if (!$oAttDef->IsNullAllowed() && isset($aParams['showMandatoryFields']))
  836. {
  837. $sStar = '*';
  838. }
  839. $aHeader[] = MetaModel::GetLabel($sClassName, $sAttCode).$sStar;
  840. }
  841. }
  842. }
  843. $sHtml = implode($sSeparator, $aHeader)."\n";
  844. $oSet->Seek(0);
  845. while ($aObjects = $oSet->FetchAssoc())
  846. {
  847. $aRow = array();
  848. foreach($aAuthorizedClasses as $sAlias => $sClassName)
  849. {
  850. $oObj = $aObjects[$sAlias];
  851. if (is_null($oObj))
  852. {
  853. $aRow[] = '';
  854. }
  855. else
  856. {
  857. $aRow[] = $oObj->GetKey();
  858. }
  859. foreach($aList[$sClassName] as $sAttCode => $oAttDef)
  860. {
  861. if (is_null($oObj))
  862. {
  863. $aRow[] = '';
  864. }
  865. else
  866. {
  867. $aRow[] = $oObj->GetAsCSV($sAttCode, $sSeparator, $sTextQualifier);
  868. }
  869. }
  870. }
  871. $sHtml .= implode($sSeparator, $aRow)."\n";
  872. }
  873. return $sHtml;
  874. }
  875. static function DisplaySetAsXML(WebPage $oPage, CMDBObjectSet $oSet, $aParams = array())
  876. {
  877. $oAppContext = new ApplicationContext();
  878. $aClasses = $oSet->GetFilter()->GetSelectedClasses();
  879. $aAuthorizedClasses = array();
  880. foreach($aClasses as $sAlias => $sClassName)
  881. {
  882. if (UserRights::IsActionAllowed($sClassName, UR_ACTION_READ, $oSet) && (UR_ALLOWED_YES || UR_ALLOWED_DEPENDS))
  883. {
  884. $aAuthorizedClasses[$sAlias] = $sClassName;
  885. }
  886. }
  887. $aAttribs = array();
  888. $aList = array();
  889. $aList[$sClassName] = MetaModel::GetZListItems($sClassName, 'details');
  890. $oPage->add("<Set>\n");
  891. $oSet->Seek(0);
  892. while ($aObjects = $oSet->FetchAssoc())
  893. {
  894. if (count($aAuthorizedClasses) > 1)
  895. {
  896. $oPage->add("<Row>\n");
  897. }
  898. foreach($aAuthorizedClasses as $sAlias => $sClassName)
  899. {
  900. $oObj = $aObjects[$sAlias];
  901. if (is_null($oObj))
  902. {
  903. $oPage->add("<$sClassName alias=\"$sAlias\" id=\"null\">\n");
  904. }
  905. else
  906. {
  907. $sClassName = get_class($oObj);
  908. $oPage->add("<$sClassName alias=\"$sAlias\" id=\"".$oObj->GetKey()."\">\n");
  909. }
  910. foreach(MetaModel::ListAttributeDefs($sClassName) as $sAttCode=>$oAttDef)
  911. {
  912. if (is_null($oObj))
  913. {
  914. $oPage->add("<$sAttCode>null</$sAttCode>\n");
  915. }
  916. else
  917. {
  918. if ($oAttDef->IsWritable())
  919. {
  920. $sValue = $oObj->GetAsXML($sAttCode);
  921. $oPage->add("<$sAttCode>$sValue</$sAttCode>\n");
  922. }
  923. }
  924. }
  925. $oPage->add("</$sClassName>\n");
  926. }
  927. if (count($aAuthorizedClasses) > 1)
  928. {
  929. $oPage->add("</Row>\n");
  930. }
  931. }
  932. $oPage->add("</Set>\n");
  933. }
  934. public static function DisplaySearchForm(WebPage $oPage, CMDBObjectSet $oSet, $aExtraParams = array())
  935. {
  936. $oPage->add(self::GetSearchForm($oPage, $oSet, $aExtraParams));
  937. }
  938. public static function GetSearchForm(WebPage $oPage, CMDBObjectSet $oSet, $aExtraParams = array())
  939. {
  940. static $iSearchFormId = 0;
  941. $oAppContext = new ApplicationContext();
  942. $sHtml = '';
  943. $numCols=4;
  944. $sClassName = $oSet->GetFilter()->GetClass();
  945. // Romain: temporarily removed the tab "OQL query" because it was not finalized
  946. // (especially when used to add a link)
  947. /*
  948. $sHtml .= "<div class=\"mini_tabs\" id=\"mini_tabs{$iSearchFormId}\"><ul>
  949. <li><a href=\"#\" onClick=\"$('div.mini_tab{$iSearchFormId}').toggle();$('#mini_tabs{$iSearchFormId} ul li a').toggleClass('selected');\">".Dict::S('UI:OQLQueryTab')."</a></li>
  950. <li><a class=\"selected\" href=\"#\" onClick=\"$('div.mini_tab{$iSearchFormId}').toggle();$('#mini_tabs{$iSearchFormId} ul li a').toggleClass('selected');\">".Dict::S('UI:SimpleSearchTab')."</a></li>
  951. </ul></div>\n";
  952. */
  953. // Simple search form
  954. if (isset($aExtraParams['currentId']))
  955. {
  956. $sSearchFormId = $aExtraParams['currentId'];
  957. }
  958. else
  959. {
  960. $iSearchFormId = $oPage->GetUniqueId();
  961. $sSearchFormId = 'SimpleSearchForm'.$iSearchFormId;
  962. $sHtml .= "<div id=\"ds_$sSearchFormId\" class=\"mini_tab{$iSearchFormId}\">\n";
  963. }
  964. // Check if the current class has some sub-classes
  965. if (isset($aExtraParams['baseClass']))
  966. {
  967. $sRootClass = $aExtraParams['baseClass'];
  968. }
  969. else
  970. {
  971. $sRootClass = $sClassName;
  972. }
  973. $aSubClasses = MetaModel::GetSubclasses($sRootClass);
  974. if (count($aSubClasses) > 0)
  975. {
  976. $aOptions = array();
  977. $aOptions[MetaModel::GetName($sRootClass)] = "<option value=\"$sRootClass\">".MetaModel::GetName($sRootClass)."</options>\n";
  978. foreach($aSubClasses as $sSubclassName)
  979. {
  980. $aOptions[MetaModel::GetName($sSubclassName)] = "<option value=\"$sSubclassName\">".MetaModel::GetName($sSubclassName)."</options>\n";
  981. }
  982. $aOptions[MetaModel::GetName($sClassName)] = "<option selected value=\"$sClassName\">".MetaModel::GetName($sClassName)."</options>\n";
  983. ksort($aOptions);
  984. $sContext = $oAppContext->GetForLink();
  985. $sClassesCombo = "<select name=\"class\" onChange=\"ReloadSearchForm('$sSearchFormId', this.value, '$sRootClass', '$sContext')\">\n".implode('', $aOptions)."</select>\n";
  986. }
  987. else
  988. {
  989. $sClassesCombo = MetaModel::GetName($sClassName);
  990. }
  991. $oUnlimitedFilter = new DBObjectSearch($sClassName);
  992. $sHtml .= "<form id=\"fs_{$sSearchFormId}\" action=\"../pages/UI.php\">\n"; // Don't use $_SERVER['SCRIPT_NAME'] since the form may be called asynchronously (from ajax.php)
  993. $sHtml .= "<h2>".Dict::Format('UI:SearchFor_Class_Objects', $sClassesCombo)."</h2>\n";
  994. $index = 0;
  995. $sHtml .= "<p>\n";
  996. $aFilterCriteria = $oSet->GetFilter()->GetCriteria();
  997. $aMapCriteria = array();
  998. foreach($aFilterCriteria as $aCriteria)
  999. {
  1000. $aMapCriteria[$aCriteria['filtercode']][] = array('value' => $aCriteria['value'], 'opcode' => $aCriteria['opcode']);
  1001. }
  1002. $aList = MetaModel::GetZListItems($sClassName, 'standard_search');
  1003. foreach($aList as $sFilterCode)
  1004. {
  1005. //$oAppContext->Reset($sFilterCode); // Make sure the same parameter will not be passed twice
  1006. $sHtml .= '<span style="white-space: nowrap;padding:5px;display:inline-block;">';
  1007. $sFilterValue = '';
  1008. $sFilterValue = utils::ReadParam($sFilterCode, '');
  1009. $sFilterOpCode = null; // Use the default 'loose' OpCode
  1010. if (empty($sFilterValue))
  1011. {
  1012. if (isset($aMapCriteria[$sFilterCode]))
  1013. {
  1014. if (count($aMapCriteria[$sFilterCode]) > 1)
  1015. {
  1016. $sFilterValue = Dict::S('UI:SearchValue:Mixed');
  1017. }
  1018. else
  1019. {
  1020. $sFilterValue = $aMapCriteria[$sFilterCode][0]['value'];
  1021. $sFilterOpCode = $aMapCriteria[$sFilterCode][0]['opcode'];
  1022. }
  1023. if ($sFilterCode != 'company')
  1024. {
  1025. $oUnlimitedFilter->AddCondition($sFilterCode, $sFilterValue, $sFilterOpCode);
  1026. }
  1027. }
  1028. }
  1029. $aAllowedValues = MetaModel::GetAllowedValues_flt($sClassName, $sFilterCode, $aExtraParams);
  1030. if ($aAllowedValues != null)
  1031. {
  1032. $oAttDef = MetaModel::GetAttributeDef($sClassName, $sFilterCode);
  1033. if ($oAttDef->IsExternalKey())
  1034. {
  1035. $iFieldSize = $oAttDef->GetMaxSize();
  1036. $iMaxComboLength = $oAttDef->GetMaximumComboLength();
  1037. $oWidget = new UIExtKeyWidget($sFilterCode, $sClassName, $oAttDef->GetLabel(), $aAllowedValues, $sFilterValue, 'search_'.$sFilterCode, false, '', '', '');
  1038. $sHtml .= "<label>".MetaModel::GetFilterLabel($sClassName, $sFilterCode).":</label>&nbsp;";
  1039. $sHtml .= $oWidget->Display($oPage, $aExtraParams, true /* bSearchMode */);
  1040. }
  1041. else
  1042. {
  1043. //Enum field or external key, display a combo
  1044. $sValue = "<select name=\"$sFilterCode\">\n";
  1045. $sValue .= "<option value=\"\">".Dict::S('UI:SearchValue:Any')."</option>\n";
  1046. foreach($aAllowedValues as $key => $value)
  1047. {
  1048. if ($sFilterValue == $key)
  1049. {
  1050. $sSelected = ' selected';
  1051. }
  1052. else
  1053. {
  1054. $sSelected = '';
  1055. }
  1056. $sValue .= "<option value=\"$key\"$sSelected>$value</option>\n";
  1057. }
  1058. $sValue .= "</select>\n";
  1059. $sHtml .= "<label>".MetaModel::GetFilterLabel($sClassName, $sFilterCode).":</label>&nbsp;$sValue\n";
  1060. }
  1061. unset($aExtraParams[$sFilterCode]);
  1062. }
  1063. else
  1064. {
  1065. // Any value is possible, display an input box
  1066. $sHtml .= "<label>".MetaModel::GetFilterLabel($sClassName, $sFilterCode).":</label>&nbsp;<input class=\"textSearch\" name=\"$sFilterCode\" value=\"$sFilterValue\"/>\n";
  1067. unset($aExtraParams[$sFilterCode]);
  1068. }
  1069. $index++;
  1070. $sHtml .= '</span> ';
  1071. }
  1072. $sHtml .= "</p>\n";
  1073. $sHtml .= "<p align=\"right\"><input type=\"submit\" value=\"".Dict::S('UI:Button:Search')."\"></p>\n";
  1074. foreach($aExtraParams as $sName => $sValue)
  1075. {
  1076. $sHtml .= "<input type=\"hidden\" name=\"$sName\" value=\"$sValue\" />\n";
  1077. }
  1078. $sHtml .= "<input type=\"hidden\" name=\"class\" value=\"$sClassName\" />\n";
  1079. $sHtml .= "<input type=\"hidden\" name=\"dosearch\" value=\"1\" />\n";
  1080. $sHtml .= "<input type=\"hidden\" name=\"operation\" value=\"search_form\" />\n";
  1081. $sHtml .= $oAppContext->GetForForm();
  1082. $sHtml .= "</form>\n";
  1083. if (!isset($aExtraParams['currentId']))
  1084. {
  1085. $sHtml .= "</div><!-- Simple search form -->\n";
  1086. }
  1087. /*
  1088. // OQL query builder
  1089. $sHtml .= "<div id=\"OQLQuery{$iSearchFormId}\" style=\"display:none\" class=\"mini_tab{$iSearchFormId}\">\n";
  1090. $sHtml .= "<h1>".Dict::S('UI:OQLQueryBuilderTitle')."</h1>\n";
  1091. $sHtml .= "<form id=\"formOQL{$iSearchFormId}\"><table style=\"width:80%;\"><tr style=\"vertical-align:top\">\n";
  1092. $sHtml .= "<td style=\"text-align:right\"><label>SELECT&nbsp;</label><select name=\"oql_class\">";
  1093. $aClasses = MetaModel::EnumChildClasses($sClassName, ENUM_CHILD_CLASSES_ALL);
  1094. $sSelectedClass = utils::ReadParam('oql_class', $sClassName);
  1095. $sOQLClause = utils::ReadParam('oql_clause', '');
  1096. asort($aClasses);
  1097. foreach($aClasses as $sChildClass)
  1098. {
  1099. $sSelected = ($sChildClass == $sSelectedClass) ? 'selected' : '';
  1100. $sHtml.= "<option value=\"$sChildClass\" $sSelected>".MetaModel::GetName($sChildClass)."</option>\n";
  1101. }
  1102. $sHtml .= "</select>&nbsp;</td><td>\n";
  1103. $sHtml .= "<textarea name=\"oql_clause\" style=\"width:100%\">$sOQLClause</textarea></td></tr>\n";
  1104. $sHtml .= "<tr><td colspan=\"2\" style=\"text-align:right\"><input type=\"submit\" value=\"".Dict::S('UI:Button:Query')."\"></td></tr>\n";
  1105. $sHtml .= "<input type=\"hidden\" name=\"dosearch\" value=\"1\" />\n";
  1106. foreach($aExtraParams as $sName => $sValue)
  1107. {
  1108. $sHtml .= "<input type=\"hidden\" name=\"$sName\" value=\"$sValue\" />\n";
  1109. }
  1110. $sHtml .= "<input type=\"hidden\" name=\"operation\" value=\"search_oql\" />\n";
  1111. $sHtml .= $oAppContext->GetForForm();
  1112. $sHtml .= "</table></form>\n";
  1113. $sHtml .= "</div><!-- OQL query form -->\n";
  1114. */
  1115. return $sHtml;
  1116. }
  1117. public static function GetFormElementForField($oPage, $sClass, $sAttCode, $oAttDef, $value = '', $sDisplayValue = '', $iId = '', $sNameSuffix = '', $iFlags = 0, $aArgs = array())
  1118. {
  1119. static $iInputId = 0;
  1120. $sFieldPrefix = '';
  1121. $sFormPrefix = isset($aArgs['formPrefix']) ? $aArgs['formPrefix'] : '';
  1122. $sFieldPrefix = isset($aArgs['prefix']) ? $sFormPrefix.$aArgs['prefix'] : $sFormPrefix;
  1123. if (isset($aArgs[$sAttCode]) && empty($value))
  1124. {
  1125. // default value passed by the context (either the app context of the operation)
  1126. $value = $aArgs[$sAttCode];
  1127. }
  1128. if (!empty($iId))
  1129. {
  1130. $iInputId = $iId;
  1131. }
  1132. else
  1133. {
  1134. $iInputId = $oPage->GetUniqueId();
  1135. }
  1136. if (!$oAttDef->IsExternalField())
  1137. {
  1138. $bMandatory = 'false';
  1139. if ( (!$oAttDef->IsNullAllowed()) || ($iFlags & OPT_ATT_MANDATORY))
  1140. {
  1141. $bMandatory = 'true';
  1142. }
  1143. $sValidationField = "<span class=\"form_validation\" id=\"v_{$iId}\"></span>";
  1144. $sHelpText = $oAttDef->GetHelpOnEdition();
  1145. $aEventsList = array();
  1146. switch($oAttDef->GetEditClass())
  1147. {
  1148. case 'Date':
  1149. case 'DateTime':
  1150. $aEventsList[] ='validate';
  1151. $aEventsList[] ='keyup';
  1152. $aEventsList[] ='change';
  1153. $sHTMLValue = "<input title=\"$sHelpText\" class=\"date-pick\" type=\"text\" size=\"20\" name=\"attr_{$sFieldPrefix}{$sAttCode}{$sNameSuffix}\" value=\"$value\" id=\"$iId\"/>&nbsp;{$sValidationField}";
  1154. break;
  1155. case 'Duration':
  1156. $aEventsList[] ='validate';
  1157. $aEventsList[] ='change';
  1158. $oPage->add_ready_script("$('#{$iId}_d').bind('keyup change', function(evt, sFormId) { return UpdateDuration('$iId'); });");
  1159. $oPage->add_ready_script("$('#{$iId}_h').bind('keyup change', function(evt, sFormId) { return UpdateDuration('$iId'); });");
  1160. $oPage->add_ready_script("$('#{$iId}_m').bind('keyup change', function(evt, sFormId) { return UpdateDuration('$iId'); });");
  1161. $oPage->add_ready_script("$('#{$iId}_s').bind('keyup change', function(evt, sFormId) { return UpdateDuration('$iId'); });");
  1162. $aVal = AttributeDuration::SplitDuration($value);
  1163. $sDays = "<input title=\"$sHelpText\" type=\"text\" size=\"3\" name=\"attr_{$sFieldPrefix}{$sAttCode}[d]{$sNameSuffix}\" value=\"{$aVal['days']}\" id=\"{$iId}_d\"/>";
  1164. $sHours = "<input title=\"$sHelpText\" type=\"text\" size=\"2\" name=\"attr_{$sFieldPrefix}{$sAttCode}[h]{$sNameSuffix}\" value=\"{$aVal['hours']}\" id=\"{$iId}_h\"/>";
  1165. $sMinutes = "<input title=\"$sHelpText\" type=\"text\" size=\"2\" name=\"attr_{$sFieldPrefix}{$sAttCode}[m]{$sNameSuffix}\" value=\"{$aVal['minutes']}\" id=\"{$iId}_m\"/>";
  1166. $sSeconds = "<input title=\"$sHelpText\" type=\"text\" size=\"2\" name=\"attr_{$sFieldPrefix}{$sAttCode}[s]{$sNameSuffix}\" value=\"{$aVal['seconds']}\" id=\"{$iId}_s\"/>";
  1167. $sHidden = "<input type=\"hidden\" id=\"{$iId}\" value=\"$value\"/>";
  1168. $sHTMLValue = Dict::Format('UI:DurationForm_Days_Hours_Minutes_Seconds', $sDays, $sHours, $sMinutes, $sSeconds).$sHidden."&nbsp;".$sValidationField;
  1169. break;
  1170. case 'Password':
  1171. $aEventsList[] ='validate';
  1172. $aEventsList[] ='keyup';
  1173. $aEventsList[] ='change';
  1174. $sHTMLValue = "<input title=\"$sHelpText\" type=\"password\" size=\"30\" name=\"attr_{$sFieldPrefix}{$sAttCode}{$sNameSuffix}\" value=\"$value\" id=\"$iId\"/>&nbsp;{$sValidationField}";
  1175. break;
  1176. case 'Text':
  1177. $aEventsList[] ='validate';
  1178. $aEventsList[] ='keyup';
  1179. $aEventsList[] ='change';
  1180. $sEditValue = $oAttDef->GetEditValue($value);
  1181. $sHTMLValue = "<table><tr><td><textarea class=\"resizable\" title=\"$sHelpText\" name=\"attr_{$sFieldPrefix}{$sAttCode}{$sNameSuffix}\" rows=\"8\" cols=\"40\" id=\"$iId\">$sEditValue</textarea></td><td>{$sValidationField}</td></tr></table>";
  1182. break;
  1183. case 'HTML':
  1184. $oWidget = new UIHTMLEditorWidget($iId, $sAttCode, $sNameSuffix, $sFieldPrefix, $sHelpText, $sValidationField, $value, $bMandatory);
  1185. $sHTMLValue = $oWidget->Display($oPage, $aArgs);
  1186. break;
  1187. case 'LinkedSet':
  1188. $aEventsList[] ='validate';
  1189. $aEventsList[] ='change';
  1190. $oWidget = new UILinksWidget($sClass, $sAttCode, $iId, $sNameSuffix, $oAttDef->DuplicatesAllowed());
  1191. $sHTMLValue = $oWidget->Display($oPage, $value);
  1192. break;
  1193. case 'Document':
  1194. $aEventsList[] ='validate';
  1195. $aEventsList[] ='change';
  1196. $oDocument = $value; // Value is an ormDocument object
  1197. $sFileName = '';
  1198. if (is_object($oDocument))
  1199. {
  1200. $sFileName = $oDocument->GetFileName();
  1201. }
  1202. $iMaxFileSize = utils::ConvertToBytes(ini_get('upload_max_filesize'));
  1203. $sHTMLValue = "<input type=\"hidden\" name=\"MAX_FILE_SIZE\" value=\"$iMaxFileSize\" />\n";
  1204. $sHTMLValue .= "<input name=\"attr_{$sFieldPrefix}{$sAttCode}{$sNameSuffix}\" type=\"hidden\" id=\"$iId\" \" value=\"$sFileName\"/>\n";
  1205. $sHTMLValue .= "<span id=\"name_$iInputId\">$sFileName</span><br/>\n";
  1206. $sHTMLValue .= "<input title=\"$sHelpText\" name=\"file_{$sFieldPrefix}{$sAttCode}{$sNameSuffix}\" type=\"file\" id=\"file_$iId\" onChange=\"UpdateFileName('$iId', this.value)\"/>&nbsp;{$sValidationField}\n";
  1207. break;
  1208. case 'List':
  1209. // Not editable for now...
  1210. $sHTMLValue = '';
  1211. break;
  1212. case 'One Way Password':
  1213. $aEventsList[] ='validate';
  1214. $oWidget = new UIPasswordWidget($sAttCode, $iId, $sNameSuffix);
  1215. $sHTMLValue = $oWidget->Display($oPage, $aArgs);
  1216. // Event list & validation is handled directly by the widget
  1217. break;
  1218. case 'ExtKey':
  1219. $aEventsList[] ='validate';
  1220. $aEventsList[] ='change';
  1221. $aAllowedValues = MetaModel::GetAllowedValues_att($sClass, $sAttCode, $aArgs);
  1222. $iFieldSize = $oAttDef->GetMaxSize();
  1223. $iMaxComboLength = $oAttDef->GetMaximumComboLength();
  1224. $oWidget = new UIExtKeyWidget($sAttCode, $sClass, $oAttDef->GetLabel(), $aAllowedValues, $value, $iId, $bMandatory, $sNameSuffix, $sFieldPrefix, $sFormPrefix);
  1225. $sHTMLValue = $oWidget->Display($oPage, $aArgs);
  1226. break;
  1227. case 'String':
  1228. default:
  1229. $aEventsList[] ='validate';
  1230. // #@# todo - add context information (depending on dimensions)
  1231. $aAllowedValues = MetaModel::GetAllowedValues_att($sClass, $sAttCode, $aArgs);
  1232. $iFieldSize = $oAttDef->GetMaxSize();
  1233. if ($aAllowedValues !== null)
  1234. {
  1235. // Discrete list of values, use a SELECT
  1236. $sHTMLValue = "<select title=\"$sHelpText\" name=\"attr_{$sFieldPrefix}{$sAttCode}{$sNameSuffix}\" id=\"$iId\">\n";
  1237. $sHTMLValue .= "<option value=\"\">".Dict::S('UI:SelectOne')."</option>\n";
  1238. foreach($aAllowedValues as $key => $display_value)
  1239. {
  1240. if ((count($aAllowedValues) == 1) && ($bMandatory == 'true') )
  1241. {
  1242. // When there is only once choice, select it by default
  1243. $sSelected = ' selected';
  1244. }
  1245. else
  1246. {
  1247. $sSelected = ($value == $key) ? ' selected' : '';
  1248. }
  1249. $sHTMLValue .= "<option value=\"$key\"$sSelected>$display_value</option>\n";
  1250. }
  1251. $sHTMLValue .= "</select>&nbsp;{$sValidationField}\n";
  1252. $aEventsList[] ='change';
  1253. }
  1254. else
  1255. {
  1256. $sHTMLValue = "<input title=\"$sHelpText\" type=\"text\" size=\"30\" maxlength=\"$iFieldSize\" name=\"attr_{$sFieldPrefix}{$sAttCode}{$sNameSuffix}\" value=\"$value\" id=\"$iId\"/>&nbsp;{$sValidationField}";
  1257. $aEventsList[] ='keyup';
  1258. $aEventsList[] ='change';
  1259. }
  1260. break;
  1261. }
  1262. $sPattern = addslashes($oAttDef->GetValidationPattern()); //'^([0-9]+)$';
  1263. if (!empty($aEventsList))
  1264. {
  1265. $sNullValue = $oAttDef->GetNullValue();
  1266. if (!is_numeric($sNullValue))
  1267. {
  1268. $sNullValue = "'$sNullValue'"; // Add quotes to turn this into a JS string if it's not a number
  1269. }
  1270. $oPage->add_ready_script("$('#$iId').bind('".implode(' ', $aEventsList)."', function(evt, sFormId) { return ValidateField('$iId', '$sPattern', $bMandatory, sFormId, $sNullValue) } );\n"); // Bind to a custom event: validate
  1271. }
  1272. $aDependencies = MetaModel::GetDependentAttributes($sClass, $sAttCode); // List of attributes that depend on the current one
  1273. if (count($aDependencies) > 0)
  1274. {
  1275. $oPage->add_ready_script("$('#$iId').bind('change', function(evt, sFormId) { return oWizardHelper{$sFormPrefix}.UpdateDependentFields(['".implode("','", $aDependencies)."']) } );\n"); // Bind to a custom event: validate
  1276. }
  1277. }
  1278. return "<div>{$sHTMLValue}</div>";
  1279. }
  1280. public function DisplayModifyForm(WebPage $oPage, $aExtraParams = array())
  1281. {
  1282. static $iGlobalFormId = 1;
  1283. $iGlobalFormId++;
  1284. $sPrefix = '';
  1285. if (isset($aExtraParams['formPrefix']))
  1286. {
  1287. $sPrefix = $aExtraParams['formPrefix'];
  1288. }
  1289. $this->m_iFormId = $sPrefix.$iGlobalFormId;
  1290. $sClass = get_class($this);
  1291. $oAppContext = new ApplicationContext();
  1292. $sStateAttCode = MetaModel::GetStateAttributeCode($sClass);
  1293. $iKey = $this->GetKey();
  1294. $aDetails = array();
  1295. $aFieldsMap = array();
  1296. if (!isset($aExtraParams['action']))
  1297. {
  1298. $sFormAction = $_SERVER['SCRIPT_NAME']; // No parameter in the URL, the only parameter will be the ones passed through the form
  1299. }
  1300. else
  1301. {
  1302. $sFormAction = $aExtraParams['action'];
  1303. }
  1304. $oPage->add("<form action=\"$sFormAction\" id=\"form_{$this->m_iFormId}\" enctype=\"multipart/form-data\" method=\"post\" onSubmit=\"return CheckFields('form_{$this->m_iFormId}', true)\">\n");
  1305. $oPage->AddTabContainer(OBJECT_PROPERTIES_TAB, $sPrefix);
  1306. $oPage->SetCurrentTabContainer(OBJECT_PROPERTIES_TAB);
  1307. $oPage->SetCurrentTab(Dict::S('UI:PropertiesTab'));
  1308. // $aDetailsList = $this->FLattenZList(MetaModel::GetZListItems($sClass, 'details'));
  1309. //$aFullList = MetaModel::ListAttributeDefs($sClass);
  1310. $aList = array();
  1311. // Compute the list of properties to display, first the attributes in the 'details' list, then
  1312. // all the remaining attributes that are not external fields
  1313. // foreach($aDetailsList as $sAttCode)
  1314. // {
  1315. // $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  1316. // if (!$oAttDef->IsExternalField())
  1317. // {
  1318. // $aList[] = $sAttCode;
  1319. // }
  1320. // }
  1321. $aDetailsList = MetaModel::GetZListItems($sClass, 'details');
  1322. $aDetailsStruct = self::ProcessZlist($aDetailsList, array('UI:PropertiesTab' => array()), 'UI:PropertiesTab', 'col1', '');
  1323. $sHtml = '';
  1324. $aDetails = array();
  1325. foreach($aDetailsStruct as $sTab => $aCols )
  1326. {
  1327. $aDetails[$sTab] = array();
  1328. ksort($aCols);
  1329. $oPage->SetCurrentTab(Dict::S($sTab));
  1330. $oPage->add('<table style="vertical-align:top"><tr>');
  1331. foreach($aCols as $sColIndex => $aFieldsets)
  1332. {
  1333. $sLabel = '';
  1334. $sPreviousLabel = '';
  1335. $aDetails[$sTab][$sColIndex] = array();
  1336. $oPage->add('<td style="vertical-align:top">');
  1337. //$aDetails[$sTab][$sColIndex] = array();
  1338. foreach($aFieldsets as $sFieldsetName => $aFields)
  1339. {
  1340. if (!empty($sFieldsetName) && ($sFieldsetName[0]!='_'))
  1341. {
  1342. $sLabel = $sFieldsetName;
  1343. }
  1344. else
  1345. {
  1346. $sLabel = '';
  1347. }
  1348. if ($sLabel != $sPreviousLabel)
  1349. {
  1350. if (!empty($sPreviousLabel))
  1351. {
  1352. $oPage->add('<fieldset>');
  1353. $oPage->add('<legend>'.Dict::S($sPreviousLabel).'</legend>');
  1354. }
  1355. $oPage->Details($aDetails[$sTab][$sColIndex]);
  1356. if (!empty($sPreviousLabel))
  1357. {
  1358. $oPage->add('</fieldset>');
  1359. }
  1360. $aDetails[$sTab][$sColIndex] = array();
  1361. $sPreviousLabel = $sLabel;
  1362. }
  1363. foreach($aFields as $sAttCode)
  1364. {
  1365. $aVal = null;
  1366. $iFlags = $this->GetAttributeFlags($sAttCode);
  1367. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  1368. if ( (!$oAttDef->IsLinkSet()) && (($iFlags & OPT_ATT_HIDDEN) == 0))
  1369. {
  1370. if ($oAttDef->IsWritable())
  1371. {
  1372. if ($sStateAttCode == $sAttCode)
  1373. {
  1374. // State attribute is always read-only from the UI
  1375. $sHTMLValue = $this->GetStateLabel();
  1376. $aVal = array('label' => $oAttDef->GetLabel(), 'value' => $sHTMLValue);
  1377. }
  1378. else
  1379. {
  1380. $sInputId = $this->m_iFormId.'_'.$sAttCode;
  1381. if ($iFlags & OPT_ATT_HIDDEN)
  1382. {
  1383. // Attribute is hidden, add a hidden input
  1384. $oPage->add('<input type="hidden" id="'.$sInputId.'" name="attr_'.$sPrefix.$sAttCode.'" value="'.htmlentities($this->Get($sAttCode), ENT_QUOTES, 'UTF-8').'"/>');
  1385. $aFieldsMap[$sAttCode] = $sInputId;
  1386. }
  1387. else
  1388. {
  1389. if ($iFlags & OPT_ATT_READONLY)
  1390. {
  1391. // Check if the attribute is not read-only becuase of a synchro...
  1392. $aReasons = array();
  1393. $iSynchroFlags = $this->GetSynchroReplicaFlags($sAttCode, $aReasons);
  1394. $sSynchroIcon = '';
  1395. if ($iSynchroFlags & OPT_ATT_READONLY)
  1396. {
  1397. $sSynchroIcon = "&nbsp;<img id=\"synchro_$sInputId\" src=\"../images/transp-lock.png\" style=\"vertical-align:middle\"/>";
  1398. $sTip = '';
  1399. foreach($aReasons as $aRow)
  1400. {
  1401. $sTip .= "<p>Synchronized with {$aRow['name']} - {$aRow['description']}</p>";
  1402. }
  1403. $oPage->add_ready_script("$('#synchro_$sInputId').qtip( { content: '$sTip', show: 'mouseover', hide: 'mouseout', style: { name: 'dark', tip: 'leftTop' }, position: { corner: { target: 'rightMiddle', tooltip: 'leftTop' }} } );");
  1404. }
  1405. // Attribute is read-only
  1406. $sHTMLValue = $this->GetAsHTML($sAttCode).$sSynchroIcon;
  1407. $sHTMLValue .= '<input type="hidden" id="'.$sInputId.'" name="attr_'.$sPrefix.$sAttCode.'" value="'.htmlentities($this->Get($sAttCode), ENT_QUOTES, 'UTF-8').'"/>';
  1408. $aFieldsMap[$sAttCode] = $sInputId;
  1409. }
  1410. else
  1411. {
  1412. $sValue = $this->Get($sAttCode);
  1413. $sDisplayValue = $this->GetEditValue($sAttCode);
  1414. $aArgs = array('this' => $this, 'formPrefix' => $sPrefix);
  1415. $sHTMLValue = "<span id=\"field_{$sInputId}\">".self::GetFormElementForField($oPage, $sClass, $sAttCode, $oAttDef, $sValue, $sDisplayValue, $sInputId, '', $iFlags, $aArgs).'</span>';
  1416. $aFieldsMap[$sAttCode] = $sInputId;
  1417. }
  1418. $aVal = array('label' => '<span title="'.$oAttDef->GetDescription().'">'.$oAttDef->GetLabel().'</span>', 'value' => $sHTMLValue);
  1419. }
  1420. }
  1421. }
  1422. else
  1423. {
  1424. $aVal = array('label' => '<span title="'.$oAttDef->GetDescription().'">'.$oAttDef->GetLabel().'</span>', 'value' => $this->GetAsHTML($sAttCode));
  1425. }
  1426. }
  1427. if ($aVal != null)
  1428. {
  1429. // The field is visible, add it to the current column
  1430. $aDetails[$sTab][$sColIndex][] = $aVal;
  1431. }
  1432. }
  1433. }
  1434. if (!empty($sPreviousLabel))
  1435. {
  1436. $oPage->add('<fieldset>');
  1437. $oPage->add('<legend>'.Dict::S($sPreviousLabel).'</legend>');
  1438. }
  1439. $oPage->Details($aDetails[$sTab][$sColIndex]);
  1440. if (!empty($sPreviousLabel))
  1441. {
  1442. $oPage->add('</fieldset>');
  1443. }
  1444. $oPage->add('</td>');
  1445. }
  1446. $oPage->add('</tr></table>');
  1447. }
  1448. // Now display the relations, one tab per relation
  1449. if (!isset($aExtraParams['noRelations']))
  1450. {
  1451. $this->DisplayBareRelations($oPage, true); // Edit mode
  1452. }
  1453. $oPage->SetCurrentTab('');
  1454. $oPage->add("<input type=\"hidden\" name=\"class\" value=\"$sClass\">\n");
  1455. $oPage->add("<input type=\"hidden\" name=\"transaction_id\" value=\"".utils::GetNewTransactionId()."\">\n");
  1456. foreach($aExtraParams as $sName => $value)
  1457. {
  1458. $oPage->add("<input type=\"hidden\" name=\"$sName\" value=\"$value\">\n");
  1459. }
  1460. $oPage->add($oAppContext->GetForForm());
  1461. if ($iKey > 0)
  1462. {
  1463. // The object already exists in the database, it's modification
  1464. $oPage->add("<input type=\"hidden\" name=\"id\" value=\"$iKey\">\n");
  1465. $oPage->add("<input type=\"hidden\" name=\"operation\" value=\"apply_modify\">\n");
  1466. // $oPage->add("<button type=\"button\" id=\"btn_cancel_{$sPrefix}\" class=\"action\" onClick=\"BackToDetails('$sClass', $iKey)\"><span>".Dict::S('UI:Button:Cancel')."</span></button>&nbsp;&nbsp;&nbsp;&nbsp;\n");
  1467. $oPage->add("<button type=\"button\" class=\"action cancel\"><span>".Dict::S('UI:Button:Cancel')."</span></button>&nbsp;&nbsp;&nbsp;&nbsp;\n");
  1468. $oPage->add("<button type=\"submit\" class=\"action\"><span>".Dict::S('UI:Button:Apply')."</span></button>\n");
  1469. }
  1470. else
  1471. {
  1472. // The object does not exist in the database it's a creation
  1473. $oPage->add("<input type=\"hidden\" name=\"operation\" value=\"apply_new\">\n");
  1474. // $oPage->add("<button type=\"button\" id=\"btn_cancel_{$sPrefix}\" class=\"action\" onClick=\"BackToDetails('$sClass', $iKey)\"><span>".Dict::S('UI:Button:Cancel')."</span></button>&nbsp;&nbsp;&nbsp;&nbsp;\n");
  1475. $oPage->add("<button type=\"button\" class=\"action cancel\">".Dict::S('UI:Button:Cancel')."</button>&nbsp;&nbsp;&nbsp;&nbsp;\n");
  1476. $oPage->add("<button type=\"submit\" class=\"action\"><span>".Dict::S('UI:Button:Create')."</span></button>\n");
  1477. }
  1478. // Hook the cancel button via jQuery so that it can be unhooked easily as well if needed
  1479. $sDefaultUrl = '../pages/UI.php?operation=cancel';
  1480. $oPage->add_ready_script("$('#form_{$this->m_iFormId} button.cancel').click( function() { BackToDetails('$sClass', $iKey, '$sDefaultUrl')} );");
  1481. $oPage->add("</form>\n");
  1482. $iFieldsCount = count($aFieldsMap);
  1483. $sJsonFieldsMap = json_encode($aFieldsMap);
  1484. $oPage->add_script(
  1485. <<<EOF
  1486. // Create the object once at the beginning of the page...
  1487. var oWizardHelper$sPrefix = new WizardHelper('$sClass', '$sPrefix');
  1488. oWizardHelper$sPrefix.SetFieldsMap($sJsonFieldsMap);
  1489. oWizardHelper$sPrefix.SetFieldsCount($iFieldsCount);
  1490. EOF
  1491. );
  1492. $oPage->add_ready_script(
  1493. <<<EOF
  1494. // Starts the validation when the page is ready
  1495. CheckFields('form_{$this->m_iFormId}', false);
  1496. EOF
  1497. );
  1498. }
  1499. public static function DisplayCreationForm(WebPage $oPage, $sClass, $oObjectToClone = null, $aArgs = array(), $aExtraParams = array())
  1500. {
  1501. $oAppContext = new ApplicationContext();
  1502. $sClass = ($oObjectToClone == null) ? $sClass : get_class($oObjectToClone);
  1503. $sStateAttCode = MetaModel::GetStateAttributeCode($sClass);
  1504. $aStates = MetaModel::EnumStates($sClass);
  1505. if ($oObjectToClone == null)
  1506. {
  1507. $oObj = MetaModel::NewObject($sClass);
  1508. if (!empty($sStateAttCode))
  1509. {
  1510. $sTargetState = MetaModel::GetDefaultState($sClass);
  1511. $oObj->Set($sStateAttCode, $sTargetState);
  1512. }
  1513. }
  1514. else
  1515. {
  1516. $oObj = clone $oObjectToClone;
  1517. }
  1518. // Pre-fill the object with default values, when there is only on possible choice
  1519. // AND the field is mandatory (otherwise there is always the possiblity to let it empty)
  1520. $aArgs['this'] = $oObj;
  1521. $aDetailsList = self::FLattenZList(MetaModel::GetZListItems($sClass, 'details'));
  1522. // Order the fields based on their dependencies
  1523. $aDeps = array();
  1524. foreach($aDetailsList as $sAttCode)
  1525. {
  1526. $aDeps[$sAttCode] = MetaModel::GetPrequisiteAttributes($sClass, $sAttCode);
  1527. }
  1528. $aList = self::OrderDependentFields($aDeps);
  1529. // Now fill-in the fields with default/supplied values
  1530. foreach($aList as $sAttCode)
  1531. {
  1532. $aAllowedValues = MetaModel::GetAllowedValues_att($sClass, $sAttCode, $aArgs);
  1533. if (isset($aArgs['default'][$sAttCode]))
  1534. {
  1535. $oObj->Set($sAttCode, $aArgs['default'][$sAttCode]);
  1536. }
  1537. elseif (count($aAllowedValues) == 1)
  1538. {
  1539. // If the field is mandatory, set it to the only possible value
  1540. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  1541. $iFlags = $oObj->GetAttributeFlags($sAttCode);
  1542. if ( (!$oAttDef->IsNullAllowed()) || ($iFlags & OPT_ATT_MANDATORY))
  1543. {
  1544. $aValues = array_keys($aAllowedValues);
  1545. $oObj->Set($sAttCode, $aValues[0]);
  1546. }
  1547. }
  1548. }
  1549. return $oObj->DisplayModifyForm( $oPage, $aExtraParams);
  1550. }
  1551. protected static function ProcessZlist($aList, $aDetails, $sCurrentTab, $sCurrentCol, $sCurrentSet)
  1552. {
  1553. //echo "<pre>ZList: ";
  1554. //print_r($aList);
  1555. //echo "</pre>\n";
  1556. $index = 0;
  1557. foreach($aList as $sKey => $value)
  1558. {
  1559. if (is_array($value))
  1560. {
  1561. if (preg_match('/^(.*):(.*)$/U', $sKey, $aMatches))
  1562. {
  1563. $sCode = $aMatches[1];
  1564. $sName = $aMatches[2];
  1565. switch($sCode)
  1566. {
  1567. case 'tab':
  1568. //echo "<p>Found a tab: $sName ($sKey)</p>\n";
  1569. if(!isset($aDetails[$sName]))
  1570. {
  1571. $aDetails[$sName] = array('col1' => array());
  1572. }
  1573. $aDetails = self::ProcessZlist($value, $aDetails, $sName, 'col1', '');
  1574. break;
  1575. case 'fieldset':
  1576. //echo "<p>Found a fieldset: $sName ($sKey)</p>\n";
  1577. if(!isset($aDetailsStruct[$sCurrentTab][$sCurrentCol][$sName]))
  1578. {
  1579. $aDetails[$sCurrentTab][$sCurrentCol][$sName] = array();
  1580. }
  1581. $aDetails = self::ProcessZlist($value, $aDetails, $sCurrentTab, $sCurrentCol, $sName);
  1582. break;
  1583. default:
  1584. case 'col':
  1585. //echo "<p>Found a column: $sName ($sKey)</p>\n";
  1586. if(!isset($aDetails[$sCurrentTab][$sName]))
  1587. {
  1588. $aDetails[$sCurrentTab][$sName] = array();
  1589. }
  1590. $aDetails = self::ProcessZlist($value, $aDetails, $sCurrentTab, $sName, '');
  1591. break;
  1592. }
  1593. }
  1594. }
  1595. else
  1596. {
  1597. //echo "<p>Scalar value: $value, in [$sCurrentTab][$sCurrentCol][$sCurrentSet][]</p>\n";
  1598. if (empty($sCurrentSet))
  1599. {
  1600. $aDetails[$sCurrentTab][$sCurrentCol]['_'.$index][] = $value;
  1601. }
  1602. else
  1603. {
  1604. $aDetails[$sCurrentTab][$sCurrentCol][$sCurrentSet][] = $value;
  1605. }
  1606. }
  1607. $index++;
  1608. }
  1609. return $aDetails;
  1610. }
  1611. protected static function FlattenZList($aList)
  1612. {
  1613. $aResult = array();
  1614. foreach($aList as $value)
  1615. {
  1616. if (!is_array($value))
  1617. {
  1618. $aResult[] = $value;
  1619. }
  1620. else
  1621. {
  1622. $aResult = array_merge($aResult,self::FlattenZList($value));
  1623. }
  1624. }
  1625. return $aResult;
  1626. }
  1627. protected function GetFieldAsHtml($sClass, $sAttCode, $sStateAttCode)
  1628. {
  1629. $retVal = null;
  1630. $iFlags = $this->GetAttributeFlags($sAttCode);
  1631. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  1632. if ( (!$oAttDef->IsLinkSet()) && (($iFlags & OPT_ATT_HIDDEN) == 0) )
  1633. {
  1634. // The field is visible in the current state of the object
  1635. if ($sStateAttCode == $sAttCode)
  1636. {
  1637. // Special display for the 'state' attribute itself
  1638. $sDisplayValue = $this->GetStateLabel();
  1639. }
  1640. else if ($oAttDef->GetEditClass() == 'Document')
  1641. {
  1642. $oDocument = $this->Get($sAttCode);
  1643. $sDisplayValue = $this->GetAsHTML($sAttCode);
  1644. $sDisplayValue .= "<br/>".Dict::Format('UI:OpenDocumentInNewWindow_', $oDocument->GetDisplayLink(get_class($this), $this->GetKey(), $sAttCode)).", \n";
  1645. $sDisplayValue .= "<br/>".Dict::Format('UI:DownloadDocument_', $oDocument->GetDisplayLink(get_class($this), $this->GetKey(), $sAttCode)).", \n";
  1646. }
  1647. else
  1648. {
  1649. $sDisplayValue = $this->GetAsHTML($sAttCode);
  1650. }
  1651. $retVal = array('label' => '<span title="'.MetaModel::GetDescription($sClass, $sAttCode).'">'.MetaModel::GetLabel($sClass, $sAttCode).'</span>', 'value' => $sDisplayValue);
  1652. }
  1653. return $retVal;
  1654. }
  1655. /**
  1656. * Displays a blob document *inline* (if possible, depending on the type of the document)
  1657. * @return string
  1658. */
  1659. public function DisplayDocumentInline(WebPage $oPage, $sAttCode)
  1660. {
  1661. $oDoc = $this->Get($sAttCode);
  1662. $sClass = get_class($this);
  1663. $Id = $this->GetKey();
  1664. switch ($oDoc->GetMainMimeType())
  1665. {
  1666. case 'text':
  1667. case 'html':
  1668. $data = $oDoc->GetData();
  1669. switch($oDoc->GetMimeType())
  1670. {
  1671. case 'text/html':
  1672. case 'text/xml':
  1673. $oPage->add("<iframe id='preview_$sAttCode' src=\"../pages/ajax.render.php?operation=display_document&class=$sClass&id=$Id&field=$sAttCode\" width=\"100%\" height=\"400\">Loading...</iframe>\n");
  1674. break;
  1675. default:
  1676. $oPage->add("<pre>".htmlentities(MyHelpers::beautifulstr($data, 1000, true), ENT_QUOTES, 'UTF-8')."</pre>\n");
  1677. }
  1678. break;
  1679. case 'application':
  1680. switch($oDoc->GetMimeType())
  1681. {
  1682. case 'application/pdf':
  1683. $oPage->add("<iframe id='preview_$sAttCode' src=\"../pages/ajax.render.php?operation=display_document&class=$sClass&id=$Id&field=$sAttCode\" width=\"100%\" height=\"400\">Loading...</iframe>\n");
  1684. break;
  1685. default:
  1686. $oPage->add(Dict::S('UI:Document:NoPreview'));
  1687. }
  1688. break;
  1689. case 'image':
  1690. $oPage->add("<img src=\"../pages/ajax.render.php?operation=display_document&class=$sClass&id=$Id&field=$sAttCode\" />\n");
  1691. break;
  1692. default:
  1693. $oPage->add(Dict::S('UI:Document:NoPreview'));
  1694. }
  1695. }
  1696. // $m_highlightComparison[previous][new] => next value
  1697. protected static $m_highlightComparison = array(
  1698. HILIGHT_CLASS_CRITICAL => array(
  1699. HILIGHT_CLASS_CRITICAL => HILIGHT_CLASS_CRITICAL,
  1700. HILIGHT_CLASS_WARNING => HILIGHT_CLASS_CRITICAL,
  1701. HILIGHT_CLASS_OK => HILIGHT_CLASS_CRITICAL,
  1702. HILIGHT_CLASS_NONE => HILIGHT_CLASS_CRITICAL,
  1703. ),
  1704. HILIGHT_CLASS_WARNING => array(
  1705. HILIGHT_CLASS_CRITICAL => HILIGHT_CLASS_CRITICAL,
  1706. HILIGHT_CLASS_WARNING => HILIGHT_CLASS_WARNING,
  1707. HILIGHT_CLASS_OK => HILIGHT_CLASS_WARNING,
  1708. HILIGHT_CLASS_NONE => HILIGHT_CLASS_WARNING,
  1709. ),
  1710. HILIGHT_CLASS_OK => array(
  1711. HILIGHT_CLASS_CRITICAL => HILIGHT_CLASS_CRITICAL,
  1712. HILIGHT_CLASS_WARNING => HILIGHT_CLASS_WARNING,
  1713. HILIGHT_CLASS_OK => HILIGHT_CLASS_OK,
  1714. HILIGHT_CLASS_NONE => HILIGHT_CLASS_OK,
  1715. ),
  1716. HILIGHT_CLASS_NONE => array(
  1717. HILIGHT_CLASS_CRITICAL => HILIGHT_CLASS_CRITICAL,
  1718. HILIGHT_CLASS_WARNING => HILIGHT_CLASS_WARNING,
  1719. HILIGHT_CLASS_OK => HILIGHT_CLASS_OK,
  1720. HILIGHT_CLASS_NONE => HILIGHT_CLASS_NONE,
  1721. ),
  1722. );
  1723. /**
  1724. * This function returns a 'hilight' CSS class, used to hilight a given row in a table
  1725. * There are currently (i.e defined in the CSS) 4 possible values HILIGHT_CLASS_CRITICAL,
  1726. * HILIGHT_CLASS_WARNING, HILIGHT_CLASS_OK, HILIGHT_CLASS_NONE
  1727. * To Be overridden by derived classes
  1728. * @param void
  1729. * @return String The desired higlight class for the object/row
  1730. */
  1731. public function GetHilightClass()
  1732. {
  1733. // Possible return values are:
  1734. // HILIGHT_CLASS_CRITICAL, HILIGHT_CLASS_WARNING, HILIGHT_CLASS_OK, HILIGHT_CLASS_NONE
  1735. $current = HILIGHT_CLASS_NONE; // Not hilighted by default
  1736. // Invoke extensions before the deletion (the deletion will do some cleanup and we might loose some information
  1737. foreach (MetaModel::EnumPlugins('iApplicationObjectExtension') as $oExtensionInstance)
  1738. {
  1739. $new = $oExtensionInstance->GetHilightClass($this);
  1740. @$current = self::$m_highlightComparison[$current][$new];
  1741. }
  1742. return $current;
  1743. }
  1744. /**
  1745. * Re-order the fields based on their inter-dependencies
  1746. * @params hash @aFields field_code => array_of_depencies
  1747. * @return array Ordered array of fields or throws an exception
  1748. */
  1749. public static function OrderDependentFields($aFields)
  1750. {
  1751. $bCircular = false;
  1752. $aResult = array();
  1753. $iCount = 0;
  1754. do
  1755. {
  1756. $bSet = false;
  1757. $iCount++;
  1758. foreach($aFields as $sFieldCode => $aDeps)
  1759. {
  1760. foreach($aDeps as $key => $sDependency)
  1761. {
  1762. if (in_array($sDependency, $aResult))
  1763. {
  1764. // Dependency is resolved, remove it
  1765. unset($aFields[$sFieldCode][$key]);
  1766. }
  1767. }
  1768. if (count($aFields[$sFieldCode]) == 0)
  1769. {
  1770. // No more pending depencies for this field, add it to the list
  1771. $aResult[] = $sFieldCode;
  1772. unset($aFields[$sFieldCode]);
  1773. $bSet = true;
  1774. }
  1775. }
  1776. }
  1777. while($bSet && (count($aFields) > 0));
  1778. if (count($aFields) > 0)
  1779. {
  1780. $sMessage = "Error: Circular dependencies between the fields ! <pre>".print_r($aFields, true)."</pre>";
  1781. throw(new Exception($sMessage));
  1782. }
  1783. return $aResult;
  1784. }
  1785. /**
  1786. * Maps the given context parameter name to the appropriate filter/search code for this class
  1787. * @param string $sContextParam Name of the context parameter, i.e. 'org_id'
  1788. * @return string Filter code, i.e. 'customer_id'
  1789. */
  1790. public static function MapContextParam($sContextParam)
  1791. {
  1792. if ($sContextParam == 'menu')
  1793. {
  1794. return null;
  1795. }
  1796. else
  1797. {
  1798. return $sContextParam;
  1799. }
  1800. }
  1801. /**
  1802. * Updates the object from the POSTed parameters
  1803. */
  1804. public function UpdateObject($sFormPrefix = '')
  1805. {
  1806. foreach(MetaModel::ListAttributeDefs(get_class($this)) as $sAttCode=>$oAttDef)
  1807. {
  1808. if ($oAttDef->IsLinkSet() && $oAttDef->IsIndirect())
  1809. {
  1810. $aLinks = utils::ReadPostedParam("attr_{$sFormPrefix}{$sAttCode}", '');
  1811. $sLinkedClass = $oAttDef->GetLinkedClass();
  1812. $sExtKeyToRemote = $oAttDef->GetExtKeyToRemote();
  1813. $sExtKeyToMe = $oAttDef->GetExtKeyToMe();
  1814. $oLinkedSet = DBObjectSet::FromScratch($sLinkedClass);
  1815. if (is_array($aLinks))
  1816. {
  1817. foreach($aLinks as $id => $aData)
  1818. {
  1819. if (is_numeric($id))
  1820. {
  1821. if ($id < 0)
  1822. {
  1823. // New link to be created, the opposite of the id (-$id) is the ID of the remote object
  1824. $oLink = MetaModel::NewObject($sLinkedClass);
  1825. $oLink->Set($sExtKeyToRemote, -$id);
  1826. $oLink->Set($sExtKeyToMe, $this->GetKey());
  1827. }
  1828. else
  1829. {
  1830. // Existing link, potentially to be updated...
  1831. $oLink = MetaModel::GetObject($sLinkedClass, $id);
  1832. }
  1833. // Now populate the attributes
  1834. foreach($aData as $sName => $value)
  1835. {
  1836. if (MetaModel::IsValidAttCode($sLinkedClass, $sName))
  1837. {
  1838. $oLinkAttDef = MetaModel::GetAttributeDef($sLinkedClass, $sName);
  1839. if ($oLinkAttDef->IsWritable())
  1840. {
  1841. $oLink->Set($sName, $value);
  1842. }
  1843. }
  1844. }
  1845. $oLinkedSet->AddObject($oLink);
  1846. }
  1847. }
  1848. }
  1849. $this->Set($sAttCode, $oLinkedSet);
  1850. }
  1851. else if ($oAttDef->IsWritable())
  1852. {
  1853. $iFlags = $this->GetAttributeFlags($sAttCode);
  1854. if ($iFlags & (OPT_ATT_HIDDEN | OPT_ATT_READONLY))
  1855. {
  1856. // Non-visible, or read-only attribute, do nothing
  1857. }
  1858. elseif ($oAttDef->GetEditClass() == 'Document')
  1859. {
  1860. // There should be an uploaded file with the named attr_<attCode>
  1861. $oDocument = utils::ReadPostedDocument("file_{$sFormPrefix}{$sAttCode}");
  1862. if (!$oDocument->IsEmpty())
  1863. {
  1864. // A new file has been uploaded
  1865. $this->Set($sAttCode, $oDocument);
  1866. }
  1867. }
  1868. elseif ($oAttDef->GetEditClass() == 'One Way Password')
  1869. {
  1870. // Check if the password was typed/changed
  1871. $bChanged = utils::ReadPostedParam("attr_{$sFormPrefix}{$sAttCode}_changed", false);
  1872. if ($bChanged)
  1873. {
  1874. // The password has been changed or set
  1875. $rawValue = utils::ReadPostedParam("attr_{$sFormPrefix}{$sAttCode}", null);
  1876. $this->Set($sAttCode, $rawValue);
  1877. }
  1878. }
  1879. elseif ($oAttDef->GetEditClass() == 'Duration')
  1880. {
  1881. $rawValue = utils::ReadPostedParam("attr_{$sFormPrefix}{$sAttCode}", null);
  1882. if (!is_array($rawValue))
  1883. {
  1884. $iValue = null;
  1885. }
  1886. else
  1887. {
  1888. $iValue = (((24*$rawValue['d'])+$rawValue['h'])*60 +$rawValue['m'])*60 + $rawValue['s'];
  1889. }
  1890. $this->Set($sAttCode, $iValue);
  1891. $previousValue = $this->Get($sAttCode);
  1892. if ($previousValue !== $iValue)
  1893. {
  1894. $this->Set($sAttCode, $iValue);
  1895. }
  1896. }
  1897. else
  1898. {
  1899. $rawValue = utils::ReadPostedParam("attr_{$sFormPrefix}{$sAttCode}", null);
  1900. if (!is_null($rawValue))
  1901. {
  1902. $aAttributes[$sAttCode] = trim($rawValue);
  1903. $previousValue = $this->Get($sAttCode);
  1904. if ($previousValue !== $aAttributes[$sAttCode])
  1905. {
  1906. $this->Set($sAttCode, $aAttributes[$sAttCode]);
  1907. }
  1908. }
  1909. }
  1910. }
  1911. }
  1912. }
  1913. protected function DBInsertTracked_Internal($bDoNotReload = false)
  1914. {
  1915. $res = parent::DBInsertTracked_Internal($bDoNotReload);
  1916. // Invoke extensions after insertion (the object must exist, have an id, etc.)
  1917. foreach (MetaModel::EnumPlugins('iApplicationObjectExtension') as $oExtensionInstance)
  1918. {
  1919. $oExtensionInstance->OnDBInsert($this, self::$m_oCurrChange);
  1920. }
  1921. return $res;
  1922. }
  1923. protected function DBCloneTracked_Internal($newKey = null)
  1924. {
  1925. $oNewObj = parent::DBCloneTracked_Internal($newKey);
  1926. // Invoke extensions after insertion (the object must exist, have an id, etc.)
  1927. foreach (MetaModel::EnumPlugins('iApplicationObjectExtension') as $oExtensionInstance)
  1928. {
  1929. $oExtensionInstance->OnDBInsert($oNewObj, self::$m_oCurrChange);
  1930. }
  1931. return $oNewObj;
  1932. }
  1933. protected function DBUpdateTracked_Internal()
  1934. {
  1935. $res = parent::DBUpdateTracked_Internal();
  1936. // Invoke extensions after the update (could be before)
  1937. foreach (MetaModel::EnumPlugins('iApplicationObjectExtension') as $oExtensionInstance)
  1938. {
  1939. $oExtensionInstance->OnDBUpdate($this, self::$m_oCurrChange);
  1940. }
  1941. return $res;
  1942. }
  1943. protected static function BulkUpdateTracked_Internal(DBObjectSearch $oFilter, array $aValues)
  1944. {
  1945. // Todo - invoke the extension
  1946. return parent::BulkUpdateTracked_Internal($oFilter, $aValues);
  1947. }
  1948. protected function DBDeleteTracked_Internal()
  1949. {
  1950. // Invoke extensions before the deletion (the deletion will do some cleanup and we might loose some information
  1951. foreach (MetaModel::EnumPlugins('iApplicationObjectExtension') as $oExtensionInstance)
  1952. {
  1953. $oExtensionInstance->OnDBDelete($this, self::$m_oCurrChange);
  1954. }
  1955. return parent::DBDeleteTracked_Internal();
  1956. }
  1957. protected static function BulkDeleteTracked_Internal(DBObjectSearch $oFilter)
  1958. {
  1959. // Todo - invoke the extension
  1960. return parent::BulkDeleteTracked_Internal($oFilter);
  1961. }
  1962. public function DoCheckToWrite()
  1963. {
  1964. parent::DoCheckToWrite();
  1965. foreach (MetaModel::EnumPlugins('iApplicationObjectExtension') as $oExtensionInstance)
  1966. {
  1967. $aNewIssues = $oExtensionInstance->OnCheckToWrite($this);
  1968. if (count($aNewIssues) > 0)
  1969. {
  1970. $this->m_aCheckIssues = array_merge($this->m_aCheckIssues, $aNewIssues);
  1971. }
  1972. }
  1973. }
  1974. protected function DoCheckToDelete()
  1975. {
  1976. parent::DoCheckToDelete();
  1977. foreach (MetaModel::EnumPlugins('iApplicationObjectExtension') as $oExtensionInstance)
  1978. {
  1979. $aNewIssues = $oExtensionInstance->OnCheckToDelete($this);
  1980. if (count($aNewIssues) > 0)
  1981. {
  1982. $this->m_aDeleteIssues = array_merge($this->m_aDeleteIssues, $aNewIssues);
  1983. }
  1984. }
  1985. }
  1986. }
  1987. ?>