cmdbabstract.class.inc.php 92 KB

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