cmdbabstract.class.inc.php 92 KB

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