cmdbabstract.class.inc.php 89 KB

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