cmdbabstract.class.inc.php 93 KB

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