cmdbabstract.class.inc.php 90 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498
  1. <?php
  2. // Copyright (C) 2010 Combodo SARL
  3. //
  4. // This program is free software; you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation; version 3 of the License.
  7. //
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. /**
  17. * Abstract class that implements some common and useful methods for displaying
  18. * the objects
  19. *
  20. * @author Erwan Taloc <erwan.taloc@combodo.com>
  21. * @author Romain Quetiez <romain.quetiez@combodo.com>
  22. * @author Denis Flaven <denis.flaven@combodo.com>
  23. * @license http://www.opensource.org/licenses/gpl-3.0.html LGPL
  24. */
  25. define('OBJECT_PROPERTIES_TAB', 'ObjectProperties');
  26. define('HILIGHT_CLASS_CRITICAL', 'red');
  27. define('HILIGHT_CLASS_WARNING', 'orange');
  28. define('HILIGHT_CLASS_OK', 'green');
  29. define('HILIGHT_CLASS_NONE', '');
  30. require_once(APPROOT.'/core/cmdbobject.class.inc.php');
  31. require_once(APPROOT.'/application/applicationextension.inc.php');
  32. require_once(APPROOT.'/application/utils.inc.php');
  33. require_once(APPROOT.'/application/applicationcontext.class.inc.php');
  34. require_once(APPROOT.'/application/ui.linkswidget.class.inc.php');
  35. require_once(APPROOT.'/application/ui.passwordwidget.class.inc.php');
  36. require_once(APPROOT.'/application/ui.extkeywidget.class.inc.php');
  37. require_once(APPROOT.'/application/ui.htmleditorwidget.class.inc.php');
  38. /**
  39. * All objects to be displayed in the application (either as a list or as details)
  40. * must implement this interface.
  41. */
  42. interface iDisplay
  43. {
  44. /**
  45. * Maps the given context parameter name to the appropriate filter/search code for this class
  46. * @param string $sContextParam Name of the context parameter, i.e. 'org_id'
  47. * @return string Filter code, i.e. 'customer_id'
  48. */
  49. public static function MapContextParam($sContextParam);
  50. /**
  51. * This function returns a 'hilight' CSS class, used to hilight a given row in a table
  52. * There are currently (i.e defined in the CSS) 4 possible values HILIGHT_CLASS_CRITICAL,
  53. * HILIGHT_CLASS_WARNING, HILIGHT_CLASS_OK, HILIGHT_CLASS_NONE
  54. * To Be overridden by derived classes
  55. * @param void
  56. * @return String The desired higlight class for the object/row
  57. */
  58. public function GetHilightClass();
  59. /**
  60. * Returns the relative path to the page that handles the display of the object
  61. * @return string
  62. */
  63. public static function GetUIPage();
  64. /**
  65. * Displays the details of the object
  66. */
  67. public function DisplayDetails(WebPage $oPage, $bEditMode = false);
  68. }
  69. abstract class cmdbAbstractObject extends CMDBObject implements iDisplay
  70. {
  71. protected $m_iFormId; // The ID of the form used to edit the object (when in edition mode !)
  72. static $iGlobalFormId = 1;
  73. /**
  74. * returns what will be the next ID for the forms
  75. */
  76. public static function GetNextFormId()
  77. {
  78. return 1 + self::$iGlobalFormId;
  79. }
  80. public static function GetUIPage()
  81. {
  82. return '../pages/UI.php';
  83. }
  84. function DisplayBareHeader(WebPage $oPage, $bEditMode = false)
  85. {
  86. // Standard Header with name, actions menu and history block
  87. //
  88. // action menu
  89. $oSingletonFilter = new DBObjectSearch(get_class($this));
  90. $oSingletonFilter->AddCondition('id', $this->GetKey(), '=');
  91. $oBlock = new MenuBlock($oSingletonFilter, 'popup', false);
  92. $oBlock->Display($oPage, -1);
  93. // Master data sources
  94. $sSynchroIcon = '';
  95. $oReplicaSet = $this->GetMasterReplica();
  96. $bSynchronized = false;
  97. $oCreatorTask = null;
  98. $bCanBeDeletedByTask = false;
  99. $bCanBeDeletedByUser = true;
  100. $aMasterSources = array();
  101. if ($oReplicaSet->Count() > 0)
  102. {
  103. $bSynchronized = true;
  104. while($aData = $oReplicaSet->FetchAssoc())
  105. {
  106. // Assumption: $aData['datasource'] will not be null because the data source id is always set...
  107. $sApplicationURL = $aData['datasource']->GetApplicationUrl($this, $aData['replica']);
  108. $sLink = $aData['datasource']->GetName();
  109. if (!empty($sApplicationURL))
  110. {
  111. $sLink = "<a href=\"$sApplicationURL\" target=\"_blank\">".$aData['datasource']->GetName()."</a>";
  112. }
  113. if ($aData['replica']->Get('status_dest_creator') == 1)
  114. {
  115. $oCreatorTask = $aData['datasource'];
  116. $bCreatedByTask = true;
  117. }
  118. else
  119. {
  120. $bCreatedByTask = false;
  121. }
  122. if ($bCreatedByTask)
  123. {
  124. $sDeletePolicy = $aData['datasource']->Get('delete_policy');
  125. if (($sDeletePolicy == 'delete') || ($sDeletePolicy == 'update_then_delete'))
  126. {
  127. $bCanBeDeletedByTask = true;
  128. }
  129. $sUserDeletePolicy = $aData['datasource']->Get('user_delete_policy');
  130. if ($sUserDeletePolicy == 'nobody')
  131. {
  132. $bCanBeDeletedByUser = false;
  133. }
  134. elseif (($sUserDeletePolicy == 'administrators') && !UserRights::IsAdministrator())
  135. {
  136. $bCanBeDeletedByUser = false;
  137. }
  138. else // everybody...
  139. {
  140. }
  141. }
  142. $aMasterSources[$aData['datasource']->GetKey()]['datasource'] = $aData['datasource'];
  143. $aMasterSources[$aData['datasource']->GetKey()]['url'] = $sLink;
  144. $aMasterSources[$aData['datasource']->GetKey()]['last_synchro'] = $aData['replica']->Get('status_last_seen');
  145. }
  146. if (is_object($oCreatorTask))
  147. {
  148. $sTaskUrl = $aMasterSources[$oCreatorTask->GetKey()]['url'];
  149. if (!$bCanBeDeletedByUser)
  150. {
  151. $sTip = "<p>".Dict::Format('Core:Synchro:TheObjectCannotBeDeletedByUser_Source', $sTaskUrl)."</p>";
  152. }
  153. else
  154. {
  155. $sTip = "<p>".Dict::Format('Core:Synchro:TheObjectWasCreatedBy_Source', $sTaskUrl)."</p>";
  156. }
  157. if ($bCanBeDeletedByTask)
  158. {
  159. $sTip .= "<p>".Dict::Format('Core:Synchro:TheObjectCanBeDeletedBy_Source', $sTaskUrl)."</p>";
  160. }
  161. }
  162. else
  163. {
  164. $sTip = "<p>".Dict::S('Core:Synchro:ThisObjectIsSynchronized')."</p>";
  165. }
  166. $sTip .= "<p><b>".Dict::S('Core:Synchro:ListOfDataSources')."</b></p>";
  167. foreach($aMasterSources as $aStruct)
  168. {
  169. $oDataSource = $aStruct['datasource'];
  170. $sLink = $aStruct['url'];
  171. $sTip .= "<p style=\"white-space:nowrap\">".$oDataSource->GetIcon(true, 'style="vertical-align:middle"')."&nbsp;$sLink<br/>";
  172. $sTip .= Dict::S('Core:Synchro:LastSynchro').'<br/>'.$aStruct['last_synchro']."</p>";
  173. }
  174. $sSynchroIcon = '&nbsp;<img style="vertical-align:middle;" id="synchro_icon" src="../images/locked.png"/>';
  175. $oPage->add_ready_script("$('#synchro_icon').qtip( { content: '$sTip', show: 'mouseover', hide: 'unfocus', style: { name: 'dark', tip: 'leftTop' }, position: { corner: { target: 'rightMiddle', tooltip: 'leftTop' }} } );");
  176. }
  177. $oPage->add("<div class=\"page_header\"><h1>".$this->GetIcon()."&nbsp;\n");
  178. $oPage->add(MetaModel::GetName(get_class($this)).": <span class=\"hilite\">".$this->GetName()."</span>$sSynchroIcon</h1>\n");
  179. $oPage->add("</div>\n");
  180. }
  181. function DisplayBareHistory(WebPage $oPage, $bEditMode = false)
  182. {
  183. // history block (with as a tab)
  184. $oHistoryFilter = new DBObjectSearch('CMDBChangeOp');
  185. $oHistoryFilter->AddCondition('objkey', $this->GetKey(), '=');
  186. $oHistoryFilter->AddCondition('objclass', get_class($this), '=');
  187. $oBlock = new HistoryBlock($oHistoryFilter, 'table', false);
  188. $oBlock->Display($oPage, -1);
  189. }
  190. function DisplayBareProperties(WebPage $oPage, $bEditMode = false)
  191. {
  192. $oPage->add($this->GetBareProperties($oPage, $bEditMode));
  193. // Special case to display the case log, if any...
  194. // 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. if ($oSet->Count() > MetaModel::GetConfig()->GetMaxDisplayLimit())
  687. {
  688. $iCount = $oSet->Count();
  689. if ($bSelectMode)
  690. {
  691. $sHeader = Dict::Format('UI:Pagination:HeaderSelection', '<span id="total">0</span>', '<span class="selectedCount"></span>');
  692. }
  693. else
  694. {
  695. $sHeader = Dict::Format('UI:Pagination:HeaderNoSelection', '<span id="total">0</span>');
  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. $sHeaders = '';
  745. if ($bSelectMode)
  746. {
  747. $sHeaders = 'headers: { 0: {sorter: false}},';
  748. }
  749. $oPage->add_ready_script("$('#{$iListId} table.listResults').tableHover().tablesorter( { $sHeaders widgets: ['myZebra', 'truncatedList']} );\n");
  750. // Manage how we update the 'Ok/Add' buttons that depend on the number of selected items
  751. if (isset($aExtraParams['cssCount']))
  752. {
  753. $sCssCount = $aExtraParams['cssCount'];
  754. if ($bSingleSelectMode)
  755. {
  756. $sSelectSelector = ":radio[name^=selectObj]";
  757. }
  758. else
  759. {
  760. $sSelectSelector = ":checkbox[name^=selectObj]";
  761. }
  762. $oPage->add_ready_script(
  763. <<<EOF
  764. $('#{$iListId} table.listResults $sSelectSelector').change(function() {
  765. var c = $('{$sCssCount}');
  766. var v = $('#{$iListId} table.listResults $sSelectSelector:checked').length;
  767. c.val(v);
  768. c.trigger('change');
  769. });
  770. EOF
  771. );
  772. }
  773. }
  774. return $sHtml;
  775. }
  776. public static function GetDisplayExtendedSet(WebPage $oPage, CMDBObjectSet $oSet, $aExtraParams = array())
  777. {
  778. static $iListId = 0;
  779. $iListId++;
  780. $aList = array();
  781. // Initialize and check the parameters
  782. $bViewLink = isset($aExtraParams['view_link']) ? $aExtraParams['view_link'] : true;
  783. $bDisplayMenu = isset($aExtraParams['menu']) ? $aExtraParams['menu'] == true : true;
  784. // Check if there is a list of aliases to limit the display to...
  785. $aDisplayAliases = isset($aExtraParams['display_aliases']) ? explode(',', $aExtraParams['display_aliases']) : array();
  786. $sHtml = '';
  787. $oAppContext = new ApplicationContext();
  788. $aClasses = $oSet->GetFilter()->GetSelectedClasses();
  789. $aAuthorizedClasses = array();
  790. foreach($aClasses as $sAlias => $sClassName)
  791. {
  792. if ( (UserRights::IsActionAllowed($sClassName, UR_ACTION_READ, $oSet) && (UR_ALLOWED_YES || UR_ALLOWED_DEPENDS)) &&
  793. ( (count($aDisplayAliases) == 0) || (in_array($sAlias, $aDisplayAliases))) )
  794. {
  795. $aAuthorizedClasses[$sAlias] = $sClassName;
  796. }
  797. }
  798. $aAttribs = array();
  799. foreach($aAuthorizedClasses as $sAlias => $sClassName) // TO DO: check if the user has enough rights to view the classes of the list...
  800. {
  801. $aList[$sClassName] = MetaModel::GetZListItems($sClassName, 'list');
  802. if ($bViewLink)
  803. {
  804. $aAttribs['key_'.$sAlias] = array('label' => MetaModel::GetName($sClassName), 'description' => '');
  805. }
  806. foreach($aList[$sClassName] as $sAttCode)
  807. {
  808. $aAttribs[$sAttCode.'_'.$sAlias] = array('label' => MetaModel::GetLabel($sClassName, $sAttCode), 'description' => MetaModel::GetDescription($sClassName, $sAttCode));
  809. }
  810. }
  811. // Load only the requested columns
  812. $aAttToLoad = array(); // attributes to load
  813. foreach($aAuthorizedClasses as $sAlias => $sClassName)
  814. {
  815. foreach($aList[$sClassName] as $sAttCode)
  816. {
  817. $aAttToLoad[$sAlias][] = $sAttCode;
  818. }
  819. }
  820. $oSet->OptimizeColumnLoad($aAttToLoad);
  821. $aValues = array();
  822. $oSet->Seek(0);
  823. $bDisplayLimit = isset($aExtraParams['display_limit']) ? $aExtraParams['display_limit'] : true;
  824. $iMaxObjects = -1;
  825. if ($bDisplayLimit)
  826. {
  827. if ($oSet->Count() > MetaModel::GetConfig()->GetMaxDisplayLimit())
  828. {
  829. $iMaxObjects = MetaModel::GetConfig()->GetMinDisplayLimit();
  830. }
  831. }
  832. while (($aObjects = $oSet->FetchAssoc()) && ($iMaxObjects != 0))
  833. {
  834. $aRow = array();
  835. foreach($aAuthorizedClasses as $sAlias => $sClassName) // TO DO: check if the user has enough rights to view the classes of the list...
  836. {
  837. if ($bViewLink)
  838. {
  839. if (is_null($aObjects[$sAlias]))
  840. {
  841. $aRow['key_'.$sAlias] = '';
  842. }
  843. else
  844. {
  845. $aRow['key_'.$sAlias] = $aObjects[$sAlias]->GetHyperLink();
  846. }
  847. }
  848. foreach($aList[$sClassName] as $sAttCode)
  849. {
  850. if (is_null($aObjects[$sAlias]))
  851. {
  852. $aRow[$sAttCode.'_'.$sAlias] = '';
  853. }
  854. else
  855. {
  856. $aRow[$sAttCode.'_'.$sAlias] = $aObjects[$sAlias]->GetAsHTML($sAttCode);
  857. }
  858. }
  859. }
  860. $aValues[] = $aRow;
  861. $iMaxObjects--;
  862. }
  863. $sHtml .= '<table class="listContainer">';
  864. $sColspan = '';
  865. if ($bDisplayMenu)
  866. {
  867. $oMenuBlock = new MenuBlock($oSet->GetFilter());
  868. $sColspan = 'colspan="2"';
  869. $aMenuExtraParams = $aExtraParams;
  870. if (!empty($sLinkageAttribute))
  871. {
  872. $aMenuExtraParams = $aExtraParams;
  873. }
  874. if ($bDisplayLimit && ($oSet->Count() > MetaModel::GetConfig()->GetMaxDisplayLimit()))
  875. {
  876. // list truncated
  877. $divId = $aExtraParams['block_id'];
  878. $sFilter = $oSet->GetFilter()->serialize();
  879. $aExtraParams['display_limit'] = false; // To expand the full list
  880. $sExtraParams = addslashes(str_replace('"', "'", json_encode($aExtraParams))); // JSON encode, change the style of the quotes and escape them
  881. $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>';
  882. $oPage->add_ready_script("$('#{$divId} table.listResults').addClass('truncated');");
  883. $oPage->add_ready_script("$('#{$divId} table.listResults tr:last td').addClass('truncated');");
  884. }
  885. else
  886. {
  887. // Full list
  888. $sHtml .= '<tr class="containerHeader"><td>&nbsp;'.Dict::Format('UI:CountOfResults', $oSet->Count()).'</td><td>';
  889. }
  890. $sHtml .= $oMenuBlock->GetRenderContent($oPage, $aMenuExtraParams, $aMenuExtraParams['currentId']);
  891. $sHtml .= '</td></tr>';
  892. }
  893. $sHtml .= "<tr><td $sColspan>";
  894. $sHtml .= $oPage->GetTable($aAttribs, $aValues);
  895. $sHtml .= '</td></tr>';
  896. $sHtml .= '</table>';
  897. return $sHtml;
  898. }
  899. static function DisplaySetAsCSV(WebPage $oPage, CMDBObjectSet $oSet, $aParams = array())
  900. {
  901. $oPage->add(self::GetSetAsCSV($oSet, $aParams));
  902. }
  903. static function GetSetAsCSV(DBObjectSet $oSet, $aParams = array())
  904. {
  905. $sSeparator = isset($aParams['separator']) ? $aParams['separator'] : ','; // default separator is comma
  906. $sTextQualifier = isset($aParams['text_qualifier']) ? $aParams['text_qualifier'] : '"'; // default text qualifier is double quote
  907. $aFields = null;
  908. if (isset($aParams['fields']) && (strlen($aParams['fields']) > 0))
  909. {
  910. $aFields = explode(',', $aParams['fields']);
  911. }
  912. $aList = array();
  913. $oAppContext = new ApplicationContext();
  914. $aClasses = $oSet->GetFilter()->GetSelectedClasses();
  915. $aAuthorizedClasses = array();
  916. foreach($aClasses as $sAlias => $sClassName)
  917. {
  918. if (UserRights::IsActionAllowed($sClassName, UR_ACTION_READ, $oSet) && (UR_ALLOWED_YES || UR_ALLOWED_DEPENDS))
  919. {
  920. $aAuthorizedClasses[$sAlias] = $sClassName;
  921. }
  922. }
  923. $aAttribs = array();
  924. $aHeader = array();
  925. foreach($aAuthorizedClasses as $sAlias => $sClassName)
  926. {
  927. foreach(MetaModel::ListAttributeDefs($sClassName) as $sAttCode => $oAttDef)
  928. {
  929. if (is_null($aFields) || (count($aFields) == 0))
  930. {
  931. // Standard list of attributes (no link sets)
  932. if ($oAttDef->IsScalar() && ($oAttDef->IsWritable() || $oAttDef->IsExternalField()))
  933. {
  934. $aList[$sClassName][$sAttCode] = $oAttDef;
  935. }
  936. }
  937. else
  938. {
  939. // User defined list of attributes
  940. if (in_array($sAttCode, $aFields))
  941. {
  942. $aList[$sClassName][$sAttCode] = $oAttDef;
  943. }
  944. }
  945. }
  946. $aHeader[] = 'id';
  947. foreach($aList[$sClassName] as $sAttCode => $oAttDef)
  948. {
  949. $sStar = '';
  950. if ($oAttDef->IsExternalField())
  951. {
  952. $sExtKeyLabel = MetaModel::GetLabel($sClassName, $oAttDef->GetKeyAttCode());
  953. $oExtKeyAttDef = MetaModel::GetAttributeDef($sClassName, $oAttDef->GetKeyAttCode());
  954. if (!$oExtKeyAttDef->IsNullAllowed() && isset($aParams['showMandatoryFields']))
  955. {
  956. $sStar = '*';
  957. }
  958. $sRemoteAttLabel = MetaModel::GetLabel($oAttDef->GetTargetClass(), $oAttDef->GetExtAttCode());
  959. $oTargetAttDef = MetaModel::GetAttributeDef($oAttDef->GetTargetClass(), $oAttDef->GetExtAttCode());
  960. $sSuffix = '';
  961. if ($oTargetAttDef->IsExternalKey())
  962. {
  963. $sSuffix = '->id';
  964. }
  965. $aHeader[] = $sExtKeyLabel.'->'.$sRemoteAttLabel.$sSuffix.$sStar;
  966. }
  967. else
  968. {
  969. if (!$oAttDef->IsNullAllowed() && isset($aParams['showMandatoryFields']))
  970. {
  971. $sStar = '*';
  972. }
  973. $aHeader[] = MetaModel::GetLabel($sClassName, $sAttCode).$sStar;
  974. }
  975. }
  976. }
  977. $sHtml = implode($sSeparator, $aHeader)."\n";
  978. $oSet->Seek(0);
  979. while ($aObjects = $oSet->FetchAssoc())
  980. {
  981. $aRow = array();
  982. foreach($aAuthorizedClasses as $sAlias => $sClassName)
  983. {
  984. $oObj = $aObjects[$sAlias];
  985. if (is_null($oObj))
  986. {
  987. $aRow[] = '';
  988. }
  989. else
  990. {
  991. $aRow[] = $oObj->GetKey();
  992. }
  993. foreach($aList[$sClassName] as $sAttCode => $oAttDef)
  994. {
  995. if (is_null($oObj))
  996. {
  997. $aRow[] = '';
  998. }
  999. else
  1000. {
  1001. $aRow[] = $oObj->GetAsCSV($sAttCode, $sSeparator, $sTextQualifier);
  1002. }
  1003. }
  1004. }
  1005. $sHtml .= implode($sSeparator, $aRow)."\n";
  1006. }
  1007. return $sHtml;
  1008. }
  1009. static function DisplaySetAsXML(WebPage $oPage, CMDBObjectSet $oSet, $aParams = array())
  1010. {
  1011. $oAppContext = new ApplicationContext();
  1012. $aClasses = $oSet->GetFilter()->GetSelectedClasses();
  1013. $aAuthorizedClasses = array();
  1014. foreach($aClasses as $sAlias => $sClassName)
  1015. {
  1016. if (UserRights::IsActionAllowed($sClassName, UR_ACTION_READ, $oSet) && (UR_ALLOWED_YES || UR_ALLOWED_DEPENDS))
  1017. {
  1018. $aAuthorizedClasses[$sAlias] = $sClassName;
  1019. }
  1020. }
  1021. $aAttribs = array();
  1022. $aList = array();
  1023. $aList[$sClassName] = MetaModel::GetZListItems($sClassName, 'details');
  1024. $oPage->add("<Set>\n");
  1025. $oSet->Seek(0);
  1026. while ($aObjects = $oSet->FetchAssoc())
  1027. {
  1028. if (count($aAuthorizedClasses) > 1)
  1029. {
  1030. $oPage->add("<Row>\n");
  1031. }
  1032. foreach($aAuthorizedClasses as $sAlias => $sClassName)
  1033. {
  1034. $oObj = $aObjects[$sAlias];
  1035. if (is_null($oObj))
  1036. {
  1037. $oPage->add("<$sClassName alias=\"$sAlias\" id=\"null\">\n");
  1038. }
  1039. else
  1040. {
  1041. $sClassName = get_class($oObj);
  1042. $oPage->add("<$sClassName alias=\"$sAlias\" id=\"".$oObj->GetKey()."\">\n");
  1043. }
  1044. foreach(MetaModel::ListAttributeDefs($sClassName) as $sAttCode=>$oAttDef)
  1045. {
  1046. if (is_null($oObj))
  1047. {
  1048. $oPage->add("<$sAttCode>null</$sAttCode>\n");
  1049. }
  1050. else
  1051. {
  1052. if ($oAttDef->IsWritable())
  1053. {
  1054. $sValue = $oObj->GetAsXML($sAttCode);
  1055. $oPage->add("<$sAttCode>$sValue</$sAttCode>\n");
  1056. }
  1057. }
  1058. }
  1059. $oPage->add("</$sClassName>\n");
  1060. }
  1061. if (count($aAuthorizedClasses) > 1)
  1062. {
  1063. $oPage->add("</Row>\n");
  1064. }
  1065. }
  1066. $oPage->add("</Set>\n");
  1067. }
  1068. public static function DisplaySearchForm(WebPage $oPage, CMDBObjectSet $oSet, $aExtraParams = array())
  1069. {
  1070. $oPage->add(self::GetSearchForm($oPage, $oSet, $aExtraParams));
  1071. }
  1072. public static function GetSearchForm(WebPage $oPage, CMDBObjectSet $oSet, $aExtraParams = array())
  1073. {
  1074. static $iSearchFormId = 0;
  1075. $oAppContext = new ApplicationContext();
  1076. $sHtml = '';
  1077. $numCols=4;
  1078. $sClassName = $oSet->GetFilter()->GetClass();
  1079. // Romain: temporarily removed the tab "OQL query" because it was not finalized
  1080. // (especially when used to add a link)
  1081. /*
  1082. $sHtml .= "<div class=\"mini_tabs\" id=\"mini_tabs{$iSearchFormId}\"><ul>
  1083. <li><a href=\"#\" onClick=\"$('div.mini_tab{$iSearchFormId}').toggle();$('#mini_tabs{$iSearchFormId} ul li a').toggleClass('selected');\">".Dict::S('UI:OQLQueryTab')."</a></li>
  1084. <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>
  1085. </ul></div>\n";
  1086. */
  1087. // Simple search form
  1088. if (isset($aExtraParams['currentId']))
  1089. {
  1090. $sSearchFormId = $aExtraParams['currentId'];
  1091. }
  1092. else
  1093. {
  1094. $iSearchFormId = $oPage->GetUniqueId();
  1095. $sSearchFormId = 'SimpleSearchForm'.$iSearchFormId;
  1096. $sHtml .= "<div id=\"ds_$sSearchFormId\" class=\"mini_tab{$iSearchFormId}\">\n";
  1097. }
  1098. // Check if the current class has some sub-classes
  1099. if (isset($aExtraParams['baseClass']))
  1100. {
  1101. $sRootClass = $aExtraParams['baseClass'];
  1102. }
  1103. else
  1104. {
  1105. $sRootClass = $sClassName;
  1106. }
  1107. $aSubClasses = MetaModel::GetSubclasses($sRootClass);
  1108. if (count($aSubClasses) > 0)
  1109. {
  1110. $aOptions = array();
  1111. $aOptions[MetaModel::GetName($sRootClass)] = "<option value=\"$sRootClass\">".MetaModel::GetName($sRootClass)."</options>\n";
  1112. foreach($aSubClasses as $sSubclassName)
  1113. {
  1114. $aOptions[MetaModel::GetName($sSubclassName)] = "<option value=\"$sSubclassName\">".MetaModel::GetName($sSubclassName)."</options>\n";
  1115. }
  1116. $aOptions[MetaModel::GetName($sClassName)] = "<option selected value=\"$sClassName\">".MetaModel::GetName($sClassName)."</options>\n";
  1117. ksort($aOptions);
  1118. $sContext = $oAppContext->GetForLink();
  1119. $sClassesCombo = "<select name=\"class\" onChange=\"ReloadSearchForm('$sSearchFormId', this.value, '$sRootClass', '$sContext')\">\n".implode('', $aOptions)."</select>\n";
  1120. }
  1121. else
  1122. {
  1123. $sClassesCombo = MetaModel::GetName($sClassName);
  1124. }
  1125. $oUnlimitedFilter = new DBObjectSearch($sClassName);
  1126. $sAction = (isset($aExtraParams['action'])) ? $aExtraParams['action'] : '../pages/UI.php';
  1127. $sHtml .= "<form id=\"fs_{$sSearchFormId}\" action=\"{$sAction}\">\n"; // Don't use $_SERVER['SCRIPT_NAME'] since the form may be called asynchronously (from ajax.php)
  1128. $sHtml .= "<h2>".Dict::Format('UI:SearchFor_Class_Objects', $sClassesCombo)."</h2>\n";
  1129. $index = 0;
  1130. $sHtml .= "<p>\n";
  1131. $aFilterCriteria = $oSet->GetFilter()->GetCriteria();
  1132. $aMapCriteria = array();
  1133. foreach($aFilterCriteria as $aCriteria)
  1134. {
  1135. $aMapCriteria[$aCriteria['filtercode']][] = array('value' => $aCriteria['value'], 'opcode' => $aCriteria['opcode']);
  1136. }
  1137. $aList = MetaModel::GetZListItems($sClassName, 'standard_search');
  1138. foreach($aList as $sFilterCode)
  1139. {
  1140. //$oAppContext->Reset($sFilterCode); // Make sure the same parameter will not be passed twice
  1141. $sHtml .= '<span style="white-space: nowrap;padding:5px;display:inline-block;">';
  1142. $sFilterValue = '';
  1143. $sFilterValue = utils::ReadParam($sFilterCode, '');
  1144. $sFilterOpCode = null; // Use the default 'loose' OpCode
  1145. if (empty($sFilterValue))
  1146. {
  1147. if (isset($aMapCriteria[$sFilterCode]))
  1148. {
  1149. if (count($aMapCriteria[$sFilterCode]) > 1)
  1150. {
  1151. $sFilterValue = Dict::S('UI:SearchValue:Mixed');
  1152. }
  1153. else
  1154. {
  1155. $sFilterValue = $aMapCriteria[$sFilterCode][0]['value'];
  1156. $sFilterOpCode = $aMapCriteria[$sFilterCode][0]['opcode'];
  1157. }
  1158. if ($sFilterCode != 'company')
  1159. {
  1160. $oUnlimitedFilter->AddCondition($sFilterCode, $sFilterValue, $sFilterOpCode);
  1161. }
  1162. }
  1163. }
  1164. $oAttDef = MetaModel::GetAttributeDef($sClassName, $sFilterCode);
  1165. if ($oAttDef->IsExternalKey())
  1166. {
  1167. $sTargetClass = $oAttDef->GetTargetClass();
  1168. $oAllowedValues = new DBObjectSet(new DBObjectSearch($sTargetClass));
  1169. $iFieldSize = $oAttDef->GetMaxSize();
  1170. $iMaxComboLength = $oAttDef->GetMaximumComboLength();
  1171. $sHtml .= "<label>".MetaModel::GetFilterLabel($sClassName, $sFilterCode).":</label>&nbsp;";
  1172. $aExtKeyParams = $aExtraParams;
  1173. $aExtKeyParams['iFieldSize'] = $oAttDef->GetMaxSize();
  1174. $aExtKeyParams['iMinChars'] = $oAttDef->GetMinAutoCompleteChars();
  1175. $sHtml .= UIExtKeyWidget::DisplayFromAttCode($oPage, $sFilterCode, $sClassName, $oAttDef->GetLabel(), $oAllowedValues, $sFilterValue, $sSearchFormId.'search_'.$sFilterCode, false, $sFilterCode, '', $aExtKeyParams, true);
  1176. }
  1177. else
  1178. {
  1179. $aAllowedValues = MetaModel::GetAllowedValues_flt($sClassName, $sFilterCode, $aExtraParams);
  1180. if (is_null($aAllowedValues))
  1181. {
  1182. // Any value is possible, display an input box
  1183. $sHtml .= "<label>".MetaModel::GetFilterLabel($sClassName, $sFilterCode).":</label>&nbsp;<input class=\"textSearch\" name=\"$sFilterCode\" value=\"$sFilterValue\"/>\n";
  1184. }
  1185. else
  1186. {
  1187. //Enum field, display a combo
  1188. $sValue = "<select name=\"$sFilterCode\">\n";
  1189. $sValue .= "<option value=\"\">".Dict::S('UI:SearchValue:Any')."</option>\n";
  1190. foreach($aAllowedValues as $key => $value)
  1191. {
  1192. if ($sFilterValue == $key)
  1193. {
  1194. $sSelected = ' selected';
  1195. }
  1196. else
  1197. {
  1198. $sSelected = '';
  1199. }
  1200. $sValue .= "<option value=\"$key\"$sSelected>$value</option>\n";
  1201. }
  1202. $sValue .= "</select>\n";
  1203. $sHtml .= "<label>".MetaModel::GetFilterLabel($sClassName, $sFilterCode).":</label>&nbsp;$sValue\n";
  1204. }
  1205. }
  1206. unset($aExtraParams[$sFilterCode]);
  1207. $index++;
  1208. $sHtml .= '</span> ';
  1209. }
  1210. $sHtml .= "</p>\n";
  1211. $sHtml .= "<p align=\"right\"><input type=\"submit\" value=\"".Dict::S('UI:Button:Search')."\"></p>\n";
  1212. foreach($aExtraParams as $sName => $sValue)
  1213. {
  1214. $sHtml .= "<input type=\"hidden\" name=\"$sName\" value=\"$sValue\" />\n";
  1215. }
  1216. $sHtml .= "<input type=\"hidden\" name=\"class\" value=\"$sClassName\" />\n";
  1217. $sHtml .= "<input type=\"hidden\" name=\"dosearch\" value=\"1\" />\n";
  1218. $sHtml .= "<input type=\"hidden\" name=\"operation\" value=\"search_form\" />\n";
  1219. $sHtml .= $oAppContext->GetForForm();
  1220. $sHtml .= "</form>\n";
  1221. if (!isset($aExtraParams['currentId']))
  1222. {
  1223. $sHtml .= "</div><!-- Simple search form -->\n";
  1224. }
  1225. /*
  1226. // OQL query builder
  1227. $sHtml .= "<div id=\"OQLQuery{$iSearchFormId}\" style=\"display:none\" class=\"mini_tab{$iSearchFormId}\">\n";
  1228. $sHtml .= "<h1>".Dict::S('UI:OQLQueryBuilderTitle')."</h1>\n";
  1229. $sHtml .= "<form id=\"formOQL{$iSearchFormId}\"><table style=\"width:80%;\"><tr style=\"vertical-align:top\">\n";
  1230. $sHtml .= "<td style=\"text-align:right\"><label>SELECT&nbsp;</label><select name=\"oql_class\">";
  1231. $aClasses = MetaModel::EnumChildClasses($sClassName, ENUM_CHILD_CLASSES_ALL);
  1232. $sSelectedClass = utils::ReadParam('oql_class', $sClassName);
  1233. $sOQLClause = utils::ReadParam('oql_clause', '');
  1234. asort($aClasses);
  1235. foreach($aClasses as $sChildClass)
  1236. {
  1237. $sSelected = ($sChildClass == $sSelectedClass) ? 'selected' : '';
  1238. $sHtml.= "<option value=\"$sChildClass\" $sSelected>".MetaModel::GetName($sChildClass)."</option>\n";
  1239. }
  1240. $sHtml .= "</select>&nbsp;</td><td>\n";
  1241. $sHtml .= "<textarea name=\"oql_clause\" style=\"width:100%\">$sOQLClause</textarea></td></tr>\n";
  1242. $sHtml .= "<tr><td colspan=\"2\" style=\"text-align:right\"><input type=\"submit\" value=\"".Dict::S('UI:Button:Query')."\"></td></tr>\n";
  1243. $sHtml .= "<input type=\"hidden\" name=\"dosearch\" value=\"1\" />\n";
  1244. foreach($aExtraParams as $sName => $sValue)
  1245. {
  1246. $sHtml .= "<input type=\"hidden\" name=\"$sName\" value=\"$sValue\" />\n";
  1247. }
  1248. $sHtml .= "<input type=\"hidden\" name=\"operation\" value=\"search_oql\" />\n";
  1249. $sHtml .= $oAppContext->GetForForm();
  1250. $sHtml .= "</table></form>\n";
  1251. $sHtml .= "</div><!-- OQL query form -->\n";
  1252. */
  1253. return $sHtml;
  1254. }
  1255. public static function GetFormElementForField($oPage, $sClass, $sAttCode, $oAttDef, $value = '', $sDisplayValue = '', $iId = '', $sNameSuffix = '', $iFlags = 0, $aArgs = array())
  1256. {
  1257. static $iInputId = 0;
  1258. $sFieldPrefix = '';
  1259. $sFormPrefix = isset($aArgs['formPrefix']) ? $aArgs['formPrefix'] : '';
  1260. $sFieldPrefix = isset($aArgs['prefix']) ? $sFormPrefix.$aArgs['prefix'] : $sFormPrefix;
  1261. if (isset($aArgs[$sAttCode]) && empty($value))
  1262. {
  1263. // default value passed by the context (either the app context of the operation)
  1264. $value = $aArgs[$sAttCode];
  1265. }
  1266. if (!empty($iId))
  1267. {
  1268. $iInputId = $iId;
  1269. }
  1270. else
  1271. {
  1272. $iInputId = $oPage->GetUniqueId();
  1273. }
  1274. if (!$oAttDef->IsExternalField())
  1275. {
  1276. $bMandatory = 'false';
  1277. if ( (!$oAttDef->IsNullAllowed()) || ($iFlags & OPT_ATT_MANDATORY))
  1278. {
  1279. $bMandatory = 'true';
  1280. }
  1281. $sValidationField = "<span class=\"form_validation\" id=\"v_{$iId}\"></span>";
  1282. $sHelpText = $oAttDef->GetHelpOnEdition();
  1283. $aEventsList = array();
  1284. switch($oAttDef->GetEditClass())
  1285. {
  1286. case 'Date':
  1287. case 'DateTime':
  1288. $aEventsList[] ='validate';
  1289. $aEventsList[] ='keyup';
  1290. $aEventsList[] ='change';
  1291. $sHTMLValue = "<input title=\"$sHelpText\" class=\"date-pick\" type=\"text\" size=\"20\" name=\"attr_{$sFieldPrefix}{$sAttCode}{$sNameSuffix}\" value=\"$value\" id=\"$iId\"/>&nbsp;{$sValidationField}";
  1292. break;
  1293. case 'Duration':
  1294. $aEventsList[] ='validate';
  1295. $aEventsList[] ='change';
  1296. $oPage->add_ready_script("$('#{$iId}_d').bind('keyup change', function(evt, sFormId) { return UpdateDuration('$iId'); });");
  1297. $oPage->add_ready_script("$('#{$iId}_h').bind('keyup change', function(evt, sFormId) { return UpdateDuration('$iId'); });");
  1298. $oPage->add_ready_script("$('#{$iId}_m').bind('keyup change', function(evt, sFormId) { return UpdateDuration('$iId'); });");
  1299. $oPage->add_ready_script("$('#{$iId}_s').bind('keyup change', function(evt, sFormId) { return UpdateDuration('$iId'); });");
  1300. $aVal = AttributeDuration::SplitDuration($value);
  1301. $sDays = "<input title=\"$sHelpText\" type=\"text\" size=\"3\" name=\"attr_{$sFieldPrefix}{$sAttCode}[d]{$sNameSuffix}\" value=\"{$aVal['days']}\" id=\"{$iId}_d\"/>";
  1302. $sHours = "<input title=\"$sHelpText\" type=\"text\" size=\"2\" name=\"attr_{$sFieldPrefix}{$sAttCode}[h]{$sNameSuffix}\" value=\"{$aVal['hours']}\" id=\"{$iId}_h\"/>";
  1303. $sMinutes = "<input title=\"$sHelpText\" type=\"text\" size=\"2\" name=\"attr_{$sFieldPrefix}{$sAttCode}[m]{$sNameSuffix}\" value=\"{$aVal['minutes']}\" id=\"{$iId}_m\"/>";
  1304. $sSeconds = "<input title=\"$sHelpText\" type=\"text\" size=\"2\" name=\"attr_{$sFieldPrefix}{$sAttCode}[s]{$sNameSuffix}\" value=\"{$aVal['seconds']}\" id=\"{$iId}_s\"/>";
  1305. $sHidden = "<input type=\"hidden\" id=\"{$iId}\" value=\"$value\"/>";
  1306. $sHTMLValue = Dict::Format('UI:DurationForm_Days_Hours_Minutes_Seconds', $sDays, $sHours, $sMinutes, $sSeconds).$sHidden."&nbsp;".$sValidationField;
  1307. $oPage->add_ready_script("$('#{$iId}').bind('update', function(evt, sFormId) { return ToggleDurationField('$iId'); });");
  1308. break;
  1309. case 'Password':
  1310. $aEventsList[] ='validate';
  1311. $aEventsList[] ='keyup';
  1312. $aEventsList[] ='change';
  1313. $sHTMLValue = "<input title=\"$sHelpText\" type=\"password\" size=\"30\" name=\"attr_{$sFieldPrefix}{$sAttCode}{$sNameSuffix}\" value=\"$value\" id=\"$iId\"/>&nbsp;{$sValidationField}";
  1314. break;
  1315. case 'Text':
  1316. $aEventsList[] ='validate';
  1317. $aEventsList[] ='keyup';
  1318. $aEventsList[] ='change';
  1319. $sEditValue = $oAttDef->GetEditValue($value);
  1320. $aStyles = array();
  1321. $sStyle = '';
  1322. $sWidth = $oAttDef->GetWidth('width', '');
  1323. if (!empty($sWidth))
  1324. {
  1325. $aStyles[] = 'width:'.$sWidth;
  1326. }
  1327. $sHeight = $oAttDef->GetHeight('height', '');
  1328. if (!empty($sHeight))
  1329. {
  1330. $aStyles[] = 'height:'.$sHeight;
  1331. }
  1332. if (count($aStyles) > 0)
  1333. {
  1334. $sStyle = 'style="'.implode('; ', $aStyles).'"';
  1335. }
  1336. $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>";
  1337. break;
  1338. case 'CaseLog':
  1339. $aEventsList[] ='validate';
  1340. $aEventsList[] ='keyup';
  1341. $aEventsList[] ='change';
  1342. $aStyles = array();
  1343. $sStyle = '';
  1344. $sWidth = $oAttDef->GetWidth('width', '');
  1345. if (!empty($sWidth))
  1346. {
  1347. $aStyles[] = 'width:'.$sWidth;
  1348. }
  1349. $sHeight = $oAttDef->GetHeight('height', '');
  1350. if (!empty($sHeight))
  1351. {
  1352. $aStyles[] = 'height:'.$sHeight;
  1353. }
  1354. if (count($aStyles) > 0)
  1355. {
  1356. $sStyle = 'style="'.implode('; ', $aStyles).'"';
  1357. }
  1358. $sHeader = '<div class="caselog_input_header">&nbsp;'.Dict::S('UI:CaseLogTypeYourTextHere').'</div>';
  1359. $sEditValue = $oAttDef->GetEditValue($value);
  1360. $sPreviousLog = $value->GetAsHTML();
  1361. $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>";
  1362. break;
  1363. case 'HTML':
  1364. $oWidget = new UIHTMLEditorWidget($iId, $sAttCode, $sNameSuffix, $sFieldPrefix, $sHelpText, $sValidationField, $value, $bMandatory);
  1365. $sHTMLValue = $oWidget->Display($oPage, $aArgs);
  1366. break;
  1367. case 'LinkedSet':
  1368. $aEventsList[] ='validate';
  1369. $aEventsList[] ='change';
  1370. $oWidget = new UILinksWidget($sClass, $sAttCode, $iId, $sNameSuffix, $oAttDef->DuplicatesAllowed());
  1371. $sHTMLValue = $oWidget->Display($oPage, $value);
  1372. break;
  1373. case 'Document':
  1374. $aEventsList[] ='validate';
  1375. $aEventsList[] ='change';
  1376. $oDocument = $value; // Value is an ormDocument object
  1377. $sFileName = '';
  1378. if (is_object($oDocument))
  1379. {
  1380. $sFileName = $oDocument->GetFileName();
  1381. }
  1382. $iMaxFileSize = utils::ConvertToBytes(ini_get('upload_max_filesize'));
  1383. $sHTMLValue = "<input type=\"hidden\" name=\"MAX_FILE_SIZE\" value=\"$iMaxFileSize\" />\n";
  1384. $sHTMLValue .= "<input name=\"attr_{$sFieldPrefix}{$sAttCode}{$sNameSuffix}\" type=\"hidden\" id=\"$iId\" \" value=\"$sFileName\"/>\n";
  1385. $sHTMLValue .= "<span id=\"name_$iInputId\">$sFileName</span><br/>\n";
  1386. $sHTMLValue .= "<input title=\"$sHelpText\" name=\"file_{$sFieldPrefix}{$sAttCode}{$sNameSuffix}\" type=\"file\" id=\"file_$iId\" onChange=\"UpdateFileName('$iId', this.value)\"/>&nbsp;{$sValidationField}\n";
  1387. break;
  1388. case 'List':
  1389. // Not editable for now...
  1390. $sHTMLValue = '';
  1391. break;
  1392. case 'One Way Password':
  1393. $aEventsList[] ='validate';
  1394. $oWidget = new UIPasswordWidget($sAttCode, $iId, $sNameSuffix);
  1395. $sHTMLValue = $oWidget->Display($oPage, $aArgs);
  1396. // Event list & validation is handled directly by the widget
  1397. break;
  1398. case 'ExtKey':
  1399. $aEventsList[] ='validate';
  1400. $aEventsList[] ='change';
  1401. $oAllowedValues = MetaModel::GetAllowedValuesAsObjectSet($sClass, $sAttCode, $aArgs);
  1402. $sFieldName = $sFieldPrefix.$sAttCode.$sNameSuffix;
  1403. $aExtKeyParams = $aArgs;
  1404. $aExtKeyParams['iFieldSize'] = $oAttDef->GetMaxSize();
  1405. $aExtKeyParams['iMinChars'] = $oAttDef->GetMinAutoCompleteChars();
  1406. $sHTMLValue = UIExtKeyWidget::DisplayFromAttCode($oPage, $sAttCode, $sClass, $oAttDef->GetLabel(), $oAllowedValues, $value, $iId, $bMandatory, $sFieldName, $sFormPrefix, $aArgs);
  1407. $sHTMLValue .= "<!-- iFlags: $iFlags bMandatory: $bMandatory -->\n";
  1408. break;
  1409. case 'String':
  1410. default:
  1411. $aEventsList[] ='validate';
  1412. // #@# todo - add context information (depending on dimensions)
  1413. $aAllowedValues = MetaModel::GetAllowedValues_att($sClass, $sAttCode, $aArgs);
  1414. $iFieldSize = $oAttDef->GetMaxSize();
  1415. if ($aAllowedValues !== null)
  1416. {
  1417. // Discrete list of values, use a SELECT
  1418. $sHTMLValue = "<select title=\"$sHelpText\" name=\"attr_{$sFieldPrefix}{$sAttCode}{$sNameSuffix}\" id=\"$iId\">\n";
  1419. $sHTMLValue .= "<option value=\"\">".Dict::S('UI:SelectOne')."</option>\n";
  1420. foreach($aAllowedValues as $key => $display_value)
  1421. {
  1422. if ((count($aAllowedValues) == 1) && ($bMandatory == 'true') )
  1423. {
  1424. // When there is only once choice, select it by default
  1425. $sSelected = ' selected';
  1426. }
  1427. else
  1428. {
  1429. $sSelected = ($value == $key) ? ' selected' : '';
  1430. }
  1431. $sHTMLValue .= "<option value=\"$key\"$sSelected>$display_value</option>\n";
  1432. }
  1433. $sHTMLValue .= "</select>&nbsp;{$sValidationField}\n";
  1434. $aEventsList[] ='change';
  1435. }
  1436. else
  1437. {
  1438. $sHTMLValue = "<input title=\"$sHelpText\" type=\"text\" size=\"30\" maxlength=\"$iFieldSize\" name=\"attr_{$sFieldPrefix}{$sAttCode}{$sNameSuffix}\" value=\"$value\" id=\"$iId\"/>&nbsp;{$sValidationField}";
  1439. $aEventsList[] ='keyup';
  1440. $aEventsList[] ='change';
  1441. }
  1442. break;
  1443. }
  1444. $sPattern = addslashes($oAttDef->GetValidationPattern()); //'^([0-9]+)$';
  1445. if (!empty($aEventsList))
  1446. {
  1447. $sNullValue = $oAttDef->GetNullValue();
  1448. if (!is_numeric($sNullValue))
  1449. {
  1450. $sNullValue = "'$sNullValue'"; // Add quotes to turn this into a JS string if it's not a number
  1451. }
  1452. $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
  1453. }
  1454. $aDependencies = MetaModel::GetDependentAttributes($sClass, $sAttCode); // List of attributes that depend on the current one
  1455. if (count($aDependencies) > 0)
  1456. {
  1457. $oPage->add_ready_script("$('#$iId').bind('change', function(evt, sFormId) { return oWizardHelper{$sFormPrefix}.UpdateDependentFields(['".implode("','", $aDependencies)."']) } );\n"); // Bind to a custom event: validate
  1458. }
  1459. }
  1460. return "<div>{$sHTMLValue}</div>";
  1461. }
  1462. public function DisplayModifyForm(WebPage $oPage, $aExtraParams = array())
  1463. {
  1464. self::$iGlobalFormId++;
  1465. $sPrefix = '';
  1466. if (isset($aExtraParams['formPrefix']))
  1467. {
  1468. $sPrefix = $aExtraParams['formPrefix'];
  1469. }
  1470. $aFieldsComments = (isset($aExtraParams['fieldsComments'])) ? $aExtraParams['fieldsComments'] : array();
  1471. $this->m_iFormId = $sPrefix.self::$iGlobalFormId;
  1472. $sClass = get_class($this);
  1473. $oAppContext = new ApplicationContext();
  1474. $sStateAttCode = MetaModel::GetStateAttributeCode($sClass);
  1475. $iKey = $this->GetKey();
  1476. $aDetails = array();
  1477. $aFieldsMap = array();
  1478. if (!isset($aExtraParams['action']))
  1479. {
  1480. $sFormAction = $_SERVER['SCRIPT_NAME']; // No parameter in the URL, the only parameter will be the ones passed through the form
  1481. }
  1482. else
  1483. {
  1484. $sFormAction = $aExtraParams['action'];
  1485. }
  1486. $iTransactionId = utils::GetNewTransactionId();
  1487. $oPage->SetTransactionId($iTransactionId);
  1488. $oPage->add("<form action=\"$sFormAction\" id=\"form_{$this->m_iFormId}\" enctype=\"multipart/form-data\" method=\"post\" onSubmit=\"return OnSubmit('form_{$this->m_iFormId}');\">\n");
  1489. $oPage->add_ready_script("$(window).unload(function() { OnUnload('$iTransactionId') } );\n");
  1490. $oPage->AddTabContainer(OBJECT_PROPERTIES_TAB, $sPrefix);
  1491. $oPage->SetCurrentTabContainer(OBJECT_PROPERTIES_TAB);
  1492. $oPage->SetCurrentTab(Dict::S('UI:PropertiesTab'));
  1493. // $aDetailsList = $this->FLattenZList(MetaModel::GetZListItems($sClass, 'details'));
  1494. //$aFullList = MetaModel::ListAttributeDefs($sClass);
  1495. $aList = array();
  1496. // Compute the list of properties to display, first the attributes in the 'details' list, then
  1497. // all the remaining attributes that are not external fields
  1498. // foreach($aDetailsList as $sAttCode)
  1499. // {
  1500. // $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  1501. // if (!$oAttDef->IsExternalField())
  1502. // {
  1503. // $aList[] = $sAttCode;
  1504. // }
  1505. // }
  1506. $aDetailsList = MetaModel::GetZListItems($sClass, 'details');
  1507. $aDetailsStruct = self::ProcessZlist($aDetailsList, array('UI:PropertiesTab' => array()), 'UI:PropertiesTab', 'col1', '');
  1508. $sHtml = '';
  1509. $aDetails = array();
  1510. foreach($aDetailsStruct as $sTab => $aCols )
  1511. {
  1512. $aDetails[$sTab] = array();
  1513. ksort($aCols);
  1514. $oPage->SetCurrentTab(Dict::S($sTab));
  1515. $oPage->add('<table style="vertical-align:top"><tr>');
  1516. foreach($aCols as $sColIndex => $aFieldsets)
  1517. {
  1518. $sLabel = '';
  1519. $sPreviousLabel = '';
  1520. $aDetails[$sTab][$sColIndex] = array();
  1521. $oPage->add('<td style="vertical-align:top">');
  1522. //$aDetails[$sTab][$sColIndex] = array();
  1523. foreach($aFieldsets as $sFieldsetName => $aFields)
  1524. {
  1525. if (!empty($sFieldsetName) && ($sFieldsetName[0]!='_'))
  1526. {
  1527. $sLabel = $sFieldsetName;
  1528. }
  1529. else
  1530. {
  1531. $sLabel = '';
  1532. }
  1533. if ($sLabel != $sPreviousLabel)
  1534. {
  1535. if (!empty($sPreviousLabel))
  1536. {
  1537. $oPage->add('<fieldset>');
  1538. $oPage->add('<legend>'.Dict::S($sPreviousLabel).'</legend>');
  1539. }
  1540. $oPage->Details($aDetails[$sTab][$sColIndex]);
  1541. if (!empty($sPreviousLabel))
  1542. {
  1543. $oPage->add('</fieldset>');
  1544. }
  1545. $aDetails[$sTab][$sColIndex] = array();
  1546. $sPreviousLabel = $sLabel;
  1547. }
  1548. foreach($aFields as $sAttCode)
  1549. {
  1550. $aVal = null;
  1551. $sComments = isset($aFieldsComments[$sAttCode]) ? $aFieldsComments[$sAttCode] : '&nbsp;';
  1552. $sInfos = '&nbsp;';
  1553. $iFlags = $this->GetAttributeFlags($sAttCode);
  1554. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  1555. if ( (!$oAttDef->IsLinkSet()) && (($iFlags & OPT_ATT_HIDDEN) == 0))
  1556. {
  1557. if ($oAttDef->IsWritable())
  1558. {
  1559. if ($sStateAttCode == $sAttCode)
  1560. {
  1561. // State attribute is always read-only from the UI
  1562. $sHTMLValue = $this->GetStateLabel();
  1563. $aVal = array('label' => '<span>'.$oAttDef->GetLabel().'</span>', 'value' => $sHTMLValue, 'comments' => $sComments, 'infos' => $sInfos);
  1564. }
  1565. else
  1566. {
  1567. $sInputId = $this->m_iFormId.'_'.$sAttCode;
  1568. if ($iFlags & OPT_ATT_HIDDEN)
  1569. {
  1570. // Attribute is hidden, add a hidden input
  1571. $oPage->add('<input type="hidden" id="'.$sInputId.'" name="attr_'.$sPrefix.$sAttCode.'" value="'.htmlentities($this->Get($sAttCode), ENT_QUOTES, 'UTF-8').'"/>');
  1572. $aFieldsMap[$sAttCode] = $sInputId;
  1573. }
  1574. else
  1575. {
  1576. if ($iFlags & (OPT_ATT_READONLY|OPT_ATT_SLAVE))
  1577. {
  1578. // Check if the attribute is not read-only becuase of a synchro...
  1579. $aReasons = array();
  1580. $sSynchroIcon = '';
  1581. if ($iFlags & OPT_ATT_SLAVE)
  1582. {
  1583. $iSynchroFlags = $this->GetSynchroReplicaFlags($sAttCode, $aReasons);
  1584. $sSynchroIcon = "&nbsp;<img id=\"synchro_$sInputId\" src=\"../images/transp-lock.png\" style=\"vertical-align:middle\"/>";
  1585. $sTip = '';
  1586. foreach($aReasons as $aRow)
  1587. {
  1588. $sTip .= "<p>Synchronized with {$aRow['name']} - {$aRow['description']}</p>";
  1589. }
  1590. $oPage->add_ready_script("$('#synchro_$sInputId').qtip( { content: '$sTip', show: 'mouseover', hide: 'mouseout', style: { name: 'dark', tip: 'leftTop' }, position: { corner: { target: 'rightMiddle', tooltip: 'leftTop' }} } );");
  1591. }
  1592. // Attribute is read-only
  1593. $sHTMLValue = $this->GetAsHTML($sAttCode);
  1594. $sHTMLValue .= '<input type="hidden" id="'.$sInputId.'" name="attr_'.$sPrefix.$sAttCode.'" value="'.htmlentities($this->Get($sAttCode), ENT_QUOTES, 'UTF-8').'"/>';
  1595. $aFieldsMap[$sAttCode] = $sInputId;
  1596. $sComments = $sSynchroIcon;
  1597. }
  1598. else
  1599. {
  1600. $sValue = $this->Get($sAttCode);
  1601. $sDisplayValue = $this->GetEditValue($sAttCode);
  1602. $aArgs = array('this' => $this, 'formPrefix' => $sPrefix);
  1603. $sHTMLValue = "<span id=\"field_{$sInputId}\">".self::GetFormElementForField($oPage, $sClass, $sAttCode, $oAttDef, $sValue, $sDisplayValue, $sInputId, '', $iFlags, $aArgs).'</span>';
  1604. $aFieldsMap[$sAttCode] = $sInputId;
  1605. }
  1606. $aVal = array('label' => '<span title="'.$oAttDef->GetDescription().'">'.$oAttDef->GetLabel().'</span>', 'value' => $sHTMLValue, 'comments' => $sComments, 'infos' => $sInfos);
  1607. }
  1608. }
  1609. }
  1610. else
  1611. {
  1612. $aVal = array('label' => '<span title="'.$oAttDef->GetDescription().'">'.$oAttDef->GetLabel().'</span>', 'value' => $this->GetAsHTML($sAttCode), 'comments' => $sComments, 'infos' => $sInfos);
  1613. }
  1614. }
  1615. if ($aVal != null)
  1616. {
  1617. // The field is visible, add it to the current column
  1618. $aDetails[$sTab][$sColIndex][] = $aVal;
  1619. }
  1620. }
  1621. }
  1622. if (!empty($sPreviousLabel))
  1623. {
  1624. $oPage->add('<fieldset>');
  1625. $oPage->add('<legend>'.Dict::S($sPreviousLabel).'</legend>');
  1626. }
  1627. $oPage->Details($aDetails[$sTab][$sColIndex]);
  1628. if (!empty($sPreviousLabel))
  1629. {
  1630. $oPage->add('</fieldset>');
  1631. }
  1632. $oPage->add('</td>');
  1633. }
  1634. $oPage->add('</tr></table>');
  1635. }
  1636. // Special case to display the case log, if any...
  1637. // WARNING: if you modify the loop below, also check the corresponding code in UpdateObject and DisplayDetails
  1638. foreach(MetaModel::ListAttributeDefs($sClass) as $sAttCode => $oAttDef)
  1639. {
  1640. if ($oAttDef instanceof AttributeCaseLog)
  1641. {
  1642. $sComments = isset($aFieldsComments[$sAttCode]) ? $aFieldsComments[$sAttCode] : '&nbsp;';
  1643. $this->DisplayCaseLog($oPage, $sAttCode, $sComments, $sPrefix, true);
  1644. $sInputId = $this->m_iFormId.'_'.$sAttCode;
  1645. $aFieldsMap[$sAttCode] = $sInputId;
  1646. }
  1647. }
  1648. // Now display the relations, one tab per relation
  1649. if (!isset($aExtraParams['noRelations']))
  1650. {
  1651. $this->DisplayBareRelations($oPage, true); // Edit mode
  1652. }
  1653. $oPage->SetCurrentTab('');
  1654. $oPage->add("<input type=\"hidden\" name=\"class\" value=\"$sClass\">\n");
  1655. $oPage->add("<input type=\"hidden\" name=\"transaction_id\" value=\"$iTransactionId\">\n");
  1656. foreach($aExtraParams as $sName => $value)
  1657. {
  1658. $oPage->add("<input type=\"hidden\" name=\"$sName\" value=\"$value\">\n");
  1659. }
  1660. $oPage->add($oAppContext->GetForForm());
  1661. // Custom operation for the form ?
  1662. if (isset($aExtraParams['custom_operation']))
  1663. {
  1664. $sOperation = $aExtraParams['custom_operation'];
  1665. }
  1666. else if ($iKey > 0)
  1667. {
  1668. $sOperation = 'apply_modify';
  1669. }
  1670. else
  1671. {
  1672. $sOperation = 'apply_new';
  1673. }
  1674. // Custom label for the apply button ?
  1675. if (isset($aExtraParams['custom_button']))
  1676. {
  1677. $sApplyButton = $aExtraParams['custom_button'];
  1678. }
  1679. else if ($iKey > 0)
  1680. {
  1681. $sApplyButton = Dict::S('UI:Button:Apply');
  1682. }
  1683. else
  1684. {
  1685. $sApplyButton = Dict::S('UI:Button:Create');
  1686. }
  1687. if ($iKey > 0)
  1688. {
  1689. // The object already exists in the database, it's a modification
  1690. $oPage->add("<input type=\"hidden\" name=\"id\" value=\"$iKey\">\n");
  1691. $oPage->add("<input type=\"hidden\" name=\"operation\" value=\"{$sOperation}\">\n");
  1692. $oPage->add("<button type=\"button\" class=\"action cancel\"><span>".Dict::S('UI:Button:Cancel')."</span></button>&nbsp;&nbsp;&nbsp;&nbsp;\n");
  1693. $oPage->add("<button type=\"submit\" class=\"action\"><span>{$sApplyButton}</span></button>\n");
  1694. }
  1695. else
  1696. {
  1697. // The object does not exist in the database it's a creation
  1698. $oPage->add("<input type=\"hidden\" name=\"operation\" value=\"$sOperation\">\n");
  1699. $oPage->add("<button type=\"button\" class=\"action cancel\">".Dict::S('UI:Button:Cancel')."</button>&nbsp;&nbsp;&nbsp;&nbsp;\n");
  1700. $oPage->add("<button type=\"submit\" class=\"action\"><span>{$sApplyButton}</span></button>\n");
  1701. }
  1702. // Hook the cancel button via jQuery so that it can be unhooked easily as well if needed
  1703. $sDefaultUrl = '../pages/UI.php?operation=cancel';
  1704. $oPage->add_ready_script("$('#form_{$this->m_iFormId} button.cancel').click( function() { BackToDetails('$sClass', $iKey, '$sDefaultUrl')} );");
  1705. $oPage->add("</form>\n");
  1706. $iFieldsCount = count($aFieldsMap);
  1707. $sJsonFieldsMap = json_encode($aFieldsMap);
  1708. $sState = $this->GetState();
  1709. $oPage->add_script(
  1710. <<<EOF
  1711. // Create the object once at the beginning of the page...
  1712. var oWizardHelper$sPrefix = new WizardHelper('$sClass', '$sPrefix', '$sState');
  1713. oWizardHelper$sPrefix.SetFieldsMap($sJsonFieldsMap);
  1714. oWizardHelper$sPrefix.SetFieldsCount($iFieldsCount);
  1715. EOF
  1716. );
  1717. $oPage->add_ready_script(
  1718. <<<EOF
  1719. // Starts the validation when the page is ready
  1720. CheckFields('form_{$this->m_iFormId}', false);
  1721. EOF
  1722. );
  1723. }
  1724. public static function DisplayCreationForm(WebPage $oPage, $sClass, $oObjectToClone = null, $aArgs = array(), $aExtraParams = array())
  1725. {
  1726. $oAppContext = new ApplicationContext();
  1727. $sClass = ($oObjectToClone == null) ? $sClass : get_class($oObjectToClone);
  1728. $sStateAttCode = MetaModel::GetStateAttributeCode($sClass);
  1729. $aStates = MetaModel::EnumStates($sClass);
  1730. if ($oObjectToClone == null)
  1731. {
  1732. $oObj = MetaModel::NewObject($sClass);
  1733. if (!empty($sStateAttCode))
  1734. {
  1735. $sTargetState = MetaModel::GetDefaultState($sClass);
  1736. $oObj->Set($sStateAttCode, $sTargetState);
  1737. }
  1738. }
  1739. else
  1740. {
  1741. $oObj = clone $oObjectToClone;
  1742. }
  1743. // Pre-fill the object with default values, when there is only on possible choice
  1744. // AND the field is mandatory (otherwise there is always the possiblity to let it empty)
  1745. $aArgs['this'] = $oObj;
  1746. $aDetailsList = self::FLattenZList(MetaModel::GetZListItems($sClass, 'details'));
  1747. // Order the fields based on their dependencies
  1748. $aDeps = array();
  1749. foreach($aDetailsList as $sAttCode)
  1750. {
  1751. $aDeps[$sAttCode] = MetaModel::GetPrequisiteAttributes($sClass, $sAttCode);
  1752. }
  1753. $aList = self::OrderDependentFields($aDeps);
  1754. // Now fill-in the fields with default/supplied values
  1755. foreach($aList as $sAttCode)
  1756. {
  1757. if (isset($aArgs['default'][$sAttCode]))
  1758. {
  1759. $oObj->Set($sAttCode, $aArgs['default'][$sAttCode]);
  1760. }
  1761. else
  1762. {
  1763. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  1764. // If the field is mandatory, set it to the only possible value
  1765. $iFlags = $oObj->GetAttributeFlags($sAttCode);
  1766. if ((!$oAttDef->IsNullAllowed()) || ($iFlags & OPT_ATT_MANDATORY))
  1767. {
  1768. if ($oAttDef->IsExternalKey())
  1769. {
  1770. $oAllowedValues = MetaModel::GetAllowedValuesAsObjectSet($sClass, $sAttCode, $aArgs);
  1771. if ($oAllowedValues->Count() == 1)
  1772. {
  1773. $oRemoteObj = $oAllowedValues->Fetch();
  1774. $oObj->Set($sAttCode, $oRemoteObj->GetKey());
  1775. }
  1776. }
  1777. else
  1778. {
  1779. $aAllowedValues = MetaModel::GetAllowedValues_att($sClass, $sAttCode, $aArgs);
  1780. if (count($aAllowedValues) == 1)
  1781. {
  1782. $aValues = array_keys($aAllowedValues);
  1783. $oObj->Set($sAttCode, $aValues[0]);
  1784. }
  1785. }
  1786. }
  1787. }
  1788. }
  1789. return $oObj->DisplayModifyForm( $oPage, $aExtraParams);
  1790. }
  1791. public static function ProcessZlist($aList, $aDetails, $sCurrentTab, $sCurrentCol, $sCurrentSet)
  1792. {
  1793. //echo "<pre>ZList: ";
  1794. //print_r($aList);
  1795. //echo "</pre>\n";
  1796. $index = 0;
  1797. foreach($aList as $sKey => $value)
  1798. {
  1799. if (is_array($value))
  1800. {
  1801. if (preg_match('/^(.*):(.*)$/U', $sKey, $aMatches))
  1802. {
  1803. $sCode = $aMatches[1];
  1804. $sName = $aMatches[2];
  1805. switch($sCode)
  1806. {
  1807. case 'tab':
  1808. //echo "<p>Found a tab: $sName ($sKey)</p>\n";
  1809. if(!isset($aDetails[$sName]))
  1810. {
  1811. $aDetails[$sName] = array('col1' => array());
  1812. }
  1813. $aDetails = self::ProcessZlist($value, $aDetails, $sName, 'col1', '');
  1814. break;
  1815. case 'fieldset':
  1816. //echo "<p>Found a fieldset: $sName ($sKey)</p>\n";
  1817. if(!isset($aDetailsStruct[$sCurrentTab][$sCurrentCol][$sName]))
  1818. {
  1819. $aDetails[$sCurrentTab][$sCurrentCol][$sName] = array();
  1820. }
  1821. $aDetails = self::ProcessZlist($value, $aDetails, $sCurrentTab, $sCurrentCol, $sName);
  1822. break;
  1823. default:
  1824. case 'col':
  1825. //echo "<p>Found a column: $sName ($sKey)</p>\n";
  1826. if(!isset($aDetails[$sCurrentTab][$sName]))
  1827. {
  1828. $aDetails[$sCurrentTab][$sName] = array();
  1829. }
  1830. $aDetails = self::ProcessZlist($value, $aDetails, $sCurrentTab, $sName, '');
  1831. break;
  1832. }
  1833. }
  1834. }
  1835. else
  1836. {
  1837. //echo "<p>Scalar value: $value, in [$sCurrentTab][$sCurrentCol][$sCurrentSet][]</p>\n";
  1838. if (empty($sCurrentSet))
  1839. {
  1840. $aDetails[$sCurrentTab][$sCurrentCol]['_'.$index][] = $value;
  1841. }
  1842. else
  1843. {
  1844. $aDetails[$sCurrentTab][$sCurrentCol][$sCurrentSet][] = $value;
  1845. }
  1846. }
  1847. $index++;
  1848. }
  1849. return $aDetails;
  1850. }
  1851. static function FlattenZList($aList)
  1852. {
  1853. $aResult = array();
  1854. foreach($aList as $value)
  1855. {
  1856. if (!is_array($value))
  1857. {
  1858. $aResult[] = $value;
  1859. }
  1860. else
  1861. {
  1862. $aResult = array_merge($aResult,self::FlattenZList($value));
  1863. }
  1864. }
  1865. return $aResult;
  1866. }
  1867. protected function GetFieldAsHtml($sClass, $sAttCode, $sStateAttCode)
  1868. {
  1869. $retVal = null;
  1870. $iFlags = $this->GetAttributeFlags($sAttCode);
  1871. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  1872. if ( (!$oAttDef->IsLinkSet()) && (($iFlags & OPT_ATT_HIDDEN) == 0) )
  1873. {
  1874. // The field is visible in the current state of the object
  1875. if ($sStateAttCode == $sAttCode)
  1876. {
  1877. // Special display for the 'state' attribute itself
  1878. $sDisplayValue = $this->GetStateLabel();
  1879. }
  1880. else if ($oAttDef->GetEditClass() == 'Document')
  1881. {
  1882. $oDocument = $this->Get($sAttCode);
  1883. $sDisplayValue = $this->GetAsHTML($sAttCode);
  1884. $sDisplayValue .= "<br/>".Dict::Format('UI:OpenDocumentInNewWindow_', $oDocument->GetDisplayLink(get_class($this), $this->GetKey(), $sAttCode)).", \n";
  1885. $sDisplayValue .= "<br/>".Dict::Format('UI:DownloadDocument_', $oDocument->GetDisplayLink(get_class($this), $this->GetKey(), $sAttCode)).", \n";
  1886. }
  1887. else
  1888. {
  1889. $sDisplayValue = $this->GetAsHTML($sAttCode);
  1890. }
  1891. $retVal = array('label' => '<span title="'.MetaModel::GetDescription($sClass, $sAttCode).'">'.MetaModel::GetLabel($sClass, $sAttCode).'</span>', 'value' => $sDisplayValue);
  1892. }
  1893. return $retVal;
  1894. }
  1895. /**
  1896. * Displays a blob document *inline* (if possible, depending on the type of the document)
  1897. * @return string
  1898. */
  1899. public function DisplayDocumentInline(WebPage $oPage, $sAttCode)
  1900. {
  1901. $oDoc = $this->Get($sAttCode);
  1902. $sClass = get_class($this);
  1903. $Id = $this->GetKey();
  1904. switch ($oDoc->GetMainMimeType())
  1905. {
  1906. case 'text':
  1907. case 'html':
  1908. $data = $oDoc->GetData();
  1909. switch($oDoc->GetMimeType())
  1910. {
  1911. case 'text/html':
  1912. case 'text/xml':
  1913. $oPage->add("<iframe id='preview_$sAttCode' src=\"../pages/ajax.render.php?operation=display_document&class=$sClass&id=$Id&field=$sAttCode\" width=\"100%\" height=\"400\">Loading...</iframe>\n");
  1914. break;
  1915. default:
  1916. $oPage->add("<pre>".htmlentities(MyHelpers::beautifulstr($data, 1000, true), ENT_QUOTES, 'UTF-8')."</pre>\n");
  1917. }
  1918. break;
  1919. case 'application':
  1920. switch($oDoc->GetMimeType())
  1921. {
  1922. case 'application/pdf':
  1923. $oPage->add("<iframe id='preview_$sAttCode' src=\"../pages/ajax.render.php?operation=display_document&class=$sClass&id=$Id&field=$sAttCode\" width=\"100%\" height=\"400\">Loading...</iframe>\n");
  1924. break;
  1925. default:
  1926. $oPage->add(Dict::S('UI:Document:NoPreview'));
  1927. }
  1928. break;
  1929. case 'image':
  1930. $oPage->add("<img src=\"../pages/ajax.render.php?operation=display_document&class=$sClass&id=$Id&field=$sAttCode\" />\n");
  1931. break;
  1932. default:
  1933. $oPage->add(Dict::S('UI:Document:NoPreview'));
  1934. }
  1935. }
  1936. // $m_highlightComparison[previous][new] => next value
  1937. protected static $m_highlightComparison = array(
  1938. HILIGHT_CLASS_CRITICAL => array(
  1939. HILIGHT_CLASS_CRITICAL => HILIGHT_CLASS_CRITICAL,
  1940. HILIGHT_CLASS_WARNING => HILIGHT_CLASS_CRITICAL,
  1941. HILIGHT_CLASS_OK => HILIGHT_CLASS_CRITICAL,
  1942. HILIGHT_CLASS_NONE => HILIGHT_CLASS_CRITICAL,
  1943. ),
  1944. HILIGHT_CLASS_WARNING => array(
  1945. HILIGHT_CLASS_CRITICAL => HILIGHT_CLASS_CRITICAL,
  1946. HILIGHT_CLASS_WARNING => HILIGHT_CLASS_WARNING,
  1947. HILIGHT_CLASS_OK => HILIGHT_CLASS_WARNING,
  1948. HILIGHT_CLASS_NONE => HILIGHT_CLASS_WARNING,
  1949. ),
  1950. HILIGHT_CLASS_OK => array(
  1951. HILIGHT_CLASS_CRITICAL => HILIGHT_CLASS_CRITICAL,
  1952. HILIGHT_CLASS_WARNING => HILIGHT_CLASS_WARNING,
  1953. HILIGHT_CLASS_OK => HILIGHT_CLASS_OK,
  1954. HILIGHT_CLASS_NONE => HILIGHT_CLASS_OK,
  1955. ),
  1956. HILIGHT_CLASS_NONE => array(
  1957. HILIGHT_CLASS_CRITICAL => HILIGHT_CLASS_CRITICAL,
  1958. HILIGHT_CLASS_WARNING => HILIGHT_CLASS_WARNING,
  1959. HILIGHT_CLASS_OK => HILIGHT_CLASS_OK,
  1960. HILIGHT_CLASS_NONE => HILIGHT_CLASS_NONE,
  1961. ),
  1962. );
  1963. /**
  1964. * This function returns a 'hilight' CSS class, used to hilight a given row in a table
  1965. * There are currently (i.e defined in the CSS) 4 possible values HILIGHT_CLASS_CRITICAL,
  1966. * HILIGHT_CLASS_WARNING, HILIGHT_CLASS_OK, HILIGHT_CLASS_NONE
  1967. * To Be overridden by derived classes
  1968. * @param void
  1969. * @return String The desired higlight class for the object/row
  1970. */
  1971. public function GetHilightClass()
  1972. {
  1973. // Possible return values are:
  1974. // HILIGHT_CLASS_CRITICAL, HILIGHT_CLASS_WARNING, HILIGHT_CLASS_OK, HILIGHT_CLASS_NONE
  1975. $current = HILIGHT_CLASS_NONE; // Not hilighted by default
  1976. // Invoke extensions before the deletion (the deletion will do some cleanup and we might loose some information
  1977. foreach (MetaModel::EnumPlugins('iApplicationObjectExtension') as $oExtensionInstance)
  1978. {
  1979. $new = $oExtensionInstance->GetHilightClass($this);
  1980. @$current = self::$m_highlightComparison[$current][$new];
  1981. }
  1982. return $current;
  1983. }
  1984. /**
  1985. * Re-order the fields based on their inter-dependencies
  1986. * @params hash @aFields field_code => array_of_depencies
  1987. * @return array Ordered array of fields or throws an exception
  1988. */
  1989. public static function OrderDependentFields($aFields)
  1990. {
  1991. $bCircular = false;
  1992. $aResult = array();
  1993. $iCount = 0;
  1994. do
  1995. {
  1996. $bSet = false;
  1997. $iCount++;
  1998. foreach($aFields as $sFieldCode => $aDeps)
  1999. {
  2000. foreach($aDeps as $key => $sDependency)
  2001. {
  2002. if (in_array($sDependency, $aResult))
  2003. {
  2004. // Dependency is resolved, remove it
  2005. unset($aFields[$sFieldCode][$key]);
  2006. }
  2007. }
  2008. if (count($aFields[$sFieldCode]) == 0)
  2009. {
  2010. // No more pending depencies for this field, add it to the list
  2011. $aResult[] = $sFieldCode;
  2012. unset($aFields[$sFieldCode]);
  2013. $bSet = true;
  2014. }
  2015. }
  2016. }
  2017. while($bSet && (count($aFields) > 0));
  2018. if (count($aFields) > 0)
  2019. {
  2020. $sMessage = "Error: Circular dependencies between the fields (or field missing in ZList) ! <pre>".print_r($aFields, true)."</pre>";
  2021. throw(new Exception($sMessage));
  2022. }
  2023. return $aResult;
  2024. }
  2025. /**
  2026. * Maps the given context parameter name to the appropriate filter/search code for this class
  2027. * @param string $sContextParam Name of the context parameter, i.e. 'org_id'
  2028. * @return string Filter code, i.e. 'customer_id'
  2029. */
  2030. public static function MapContextParam($sContextParam)
  2031. {
  2032. if ($sContextParam == 'menu')
  2033. {
  2034. return null;
  2035. }
  2036. else
  2037. {
  2038. return $sContextParam;
  2039. }
  2040. }
  2041. /**
  2042. * Updates the object from the POSTed parameters
  2043. */
  2044. public function UpdateObject($sFormPrefix = '', $aAttList = null)
  2045. {
  2046. $aErrors = array();
  2047. if (!is_array($aAttList))
  2048. {
  2049. $aAttList = $this->FlattenZList(MetaModel::GetZListItems(get_class($this), 'details'));
  2050. // Special case to process the case log, if any...
  2051. // WARNING: if you change this also check the functions DisplayModifyForm and DisplayCaseLog
  2052. foreach(MetaModel::ListAttributeDefs(get_class($this)) as $sAttCode => $oAttDef)
  2053. {
  2054. $iFlags = $this->GetAttributeFlags($sAttCode);
  2055. if ($oAttDef instanceof AttributeCaseLog)
  2056. {
  2057. if (!($iFlags & (OPT_ATT_HIDDEN|OPT_ATT_SLAVE|OPT_ATT_READONLY)))
  2058. {
  2059. // The case log is editable, append it to the list of fields to retrieve
  2060. $aAttList[] = $sAttCode;
  2061. }
  2062. }
  2063. }
  2064. }
  2065. foreach($aAttList as $sAttCode)
  2066. {
  2067. $oAttDef = MetaModel::GetAttributeDef(get_class($this), $sAttCode);
  2068. $iFlags = $this->GetAttributeFlags($sAttCode);
  2069. if ($oAttDef->IsWritable())
  2070. {
  2071. if ( $iFlags & (OPT_ATT_HIDDEN | OPT_ATT_READONLY))
  2072. {
  2073. // Non-visible, or read-only attribute, do nothing
  2074. }
  2075. elseif($iFlags & OPT_ATT_SLAVE)
  2076. {
  2077. $value = utils::ReadPostedParam("attr_{$sFormPrefix}{$sAttCode}", null);
  2078. if (!is_null($value) && ($value != $this->Get($sAttCode)))
  2079. {
  2080. $aErrors[] = Dict::Format('UI:AttemptingToSetASlaveAttribute_Name', $oAttDef->GetLabel());
  2081. }
  2082. }
  2083. elseif ($oAttDef->IsLinkSet() && $oAttDef->IsIndirect())
  2084. {
  2085. $aLinks = utils::ReadPostedParam("attr_{$sFormPrefix}{$sAttCode}", null);
  2086. $sLinkedClass = $oAttDef->GetLinkedClass();
  2087. $sExtKeyToRemote = $oAttDef->GetExtKeyToRemote();
  2088. $sExtKeyToMe = $oAttDef->GetExtKeyToMe();
  2089. $oLinkedSet = DBObjectSet::FromScratch($sLinkedClass);
  2090. if (is_array($aLinks))
  2091. {
  2092. foreach($aLinks as $id => $aData)
  2093. {
  2094. if (is_numeric($id))
  2095. {
  2096. if ($id < 0)
  2097. {
  2098. // New link to be created, the opposite of the id (-$id) is the ID of the remote object
  2099. $oLink = MetaModel::NewObject($sLinkedClass);
  2100. $oLink->Set($sExtKeyToRemote, -$id);
  2101. $oLink->Set($sExtKeyToMe, $this->GetKey());
  2102. }
  2103. else
  2104. {
  2105. // Existing link, potentially to be updated...
  2106. $oLink = MetaModel::GetObject($sLinkedClass, $id);
  2107. }
  2108. // Now populate the attributes
  2109. foreach($aData as $sName => $value)
  2110. {
  2111. if (MetaModel::IsValidAttCode($sLinkedClass, $sName))
  2112. {
  2113. $oLinkAttDef = MetaModel::GetAttributeDef($sLinkedClass, $sName);
  2114. if ($oLinkAttDef->IsWritable())
  2115. {
  2116. $oLink->Set($sName, $value);
  2117. }
  2118. }
  2119. }
  2120. $oLinkedSet->AddObject($oLink);
  2121. }
  2122. }
  2123. }
  2124. $this->Set($sAttCode, $oLinkedSet);
  2125. }
  2126. elseif ($oAttDef->GetEditClass() == 'Document')
  2127. {
  2128. // There should be an uploaded file with the named attr_<attCode>
  2129. $oDocument = utils::ReadPostedDocument("file_{$sFormPrefix}{$sAttCode}");
  2130. if (!$oDocument->IsEmpty())
  2131. {
  2132. // A new file has been uploaded
  2133. $this->Set($sAttCode, $oDocument);
  2134. }
  2135. }
  2136. elseif ($oAttDef->GetEditClass() == 'One Way Password')
  2137. {
  2138. // Check if the password was typed/changed
  2139. $bChanged = utils::ReadPostedParam("attr_{$sFormPrefix}{$sAttCode}_changed", false);
  2140. if ($bChanged)
  2141. {
  2142. // The password has been changed or set
  2143. $rawValue = utils::ReadPostedParam("attr_{$sFormPrefix}{$sAttCode}", null);
  2144. $this->Set($sAttCode, $rawValue);
  2145. }
  2146. }
  2147. elseif ($oAttDef->GetEditClass() == 'Duration')
  2148. {
  2149. $rawValue = utils::ReadPostedParam("attr_{$sFormPrefix}{$sAttCode}", null);
  2150. if (!is_array($rawValue)) continue;
  2151. $iValue = (((24*$rawValue['d'])+$rawValue['h'])*60 +$rawValue['m'])*60 + $rawValue['s'];
  2152. $this->Set($sAttCode, $iValue);
  2153. $previousValue = $this->Get($sAttCode);
  2154. if ($previousValue !== $iValue)
  2155. {
  2156. $this->Set($sAttCode, $iValue);
  2157. }
  2158. }
  2159. else
  2160. {
  2161. $rawValue = utils::ReadPostedParam("attr_{$sFormPrefix}{$sAttCode}", null);
  2162. if (!is_null($rawValue))
  2163. {
  2164. $aAttributes[$sAttCode] = trim($rawValue);
  2165. $previousValue = $this->Get($sAttCode);
  2166. if ($previousValue !== $aAttributes[$sAttCode])
  2167. {
  2168. $this->Set($sAttCode, $aAttributes[$sAttCode]);
  2169. }
  2170. }
  2171. }
  2172. }
  2173. }
  2174. // Invoke extensions after the update of the object from the form
  2175. foreach (MetaModel::EnumPlugins('iApplicationObjectExtension') as $oExtensionInstance)
  2176. {
  2177. $oExtensionInstance->OnFormSubmit($this, $sFormPrefix);
  2178. }
  2179. return $aErrors;
  2180. }
  2181. protected function DBInsertTracked_Internal($bDoNotReload = false)
  2182. {
  2183. $res = parent::DBInsertTracked_Internal($bDoNotReload);
  2184. // Invoke extensions after insertion (the object must exist, have an id, etc.)
  2185. foreach (MetaModel::EnumPlugins('iApplicationObjectExtension') as $oExtensionInstance)
  2186. {
  2187. $oExtensionInstance->OnDBInsert($this, self::$m_oCurrChange);
  2188. }
  2189. return $res;
  2190. }
  2191. protected function DBCloneTracked_Internal($newKey = null)
  2192. {
  2193. $oNewObj = parent::DBCloneTracked_Internal($newKey);
  2194. // Invoke extensions after insertion (the object must exist, have an id, etc.)
  2195. foreach (MetaModel::EnumPlugins('iApplicationObjectExtension') as $oExtensionInstance)
  2196. {
  2197. $oExtensionInstance->OnDBInsert($oNewObj, self::$m_oCurrChange);
  2198. }
  2199. return $oNewObj;
  2200. }
  2201. protected function DBUpdateTracked_Internal()
  2202. {
  2203. $res = parent::DBUpdateTracked_Internal();
  2204. // Invoke extensions after the update (could be before)
  2205. foreach (MetaModel::EnumPlugins('iApplicationObjectExtension') as $oExtensionInstance)
  2206. {
  2207. $oExtensionInstance->OnDBUpdate($this, self::$m_oCurrChange);
  2208. }
  2209. return $res;
  2210. }
  2211. protected static function BulkUpdateTracked_Internal(DBObjectSearch $oFilter, array $aValues)
  2212. {
  2213. // Todo - invoke the extension
  2214. return parent::BulkUpdateTracked_Internal($oFilter, $aValues);
  2215. }
  2216. protected function DBDeleteTracked_Internal(&$oDeletionPlan = null)
  2217. {
  2218. // Invoke extensions before the deletion (the deletion will do some cleanup and we might loose some information
  2219. foreach (MetaModel::EnumPlugins('iApplicationObjectExtension') as $oExtensionInstance)
  2220. {
  2221. $oExtensionInstance->OnDBDelete($this, self::$m_oCurrChange);
  2222. }
  2223. return parent::DBDeleteTracked_Internal($oDeletionPlan);
  2224. }
  2225. protected static function BulkDeleteTracked_Internal(DBObjectSearch $oFilter)
  2226. {
  2227. // Todo - invoke the extension
  2228. return parent::BulkDeleteTracked_Internal($oFilter);
  2229. }
  2230. public function IsModified()
  2231. {
  2232. if (parent::IsModified())
  2233. {
  2234. return true;
  2235. }
  2236. // Plugins
  2237. //
  2238. foreach (MetaModel::EnumPlugins('iApplicationObjectExtension') as $oExtensionInstance)
  2239. {
  2240. if ($oExtensionInstance->OnIsModified($this))
  2241. {
  2242. return true;
  2243. }
  2244. }
  2245. }
  2246. public function DoCheckToWrite()
  2247. {
  2248. parent::DoCheckToWrite();
  2249. // Plugins
  2250. //
  2251. foreach (MetaModel::EnumPlugins('iApplicationObjectExtension') as $oExtensionInstance)
  2252. {
  2253. $aNewIssues = $oExtensionInstance->OnCheckToWrite($this);
  2254. if (count($aNewIssues) > 0)
  2255. {
  2256. $this->m_aCheckIssues = array_merge($this->m_aCheckIssues, $aNewIssues);
  2257. }
  2258. }
  2259. // User rights
  2260. //
  2261. $aChanges = $this->ListChanges();
  2262. if (count($aChanges) > 0)
  2263. {
  2264. $aForbiddenFields = array();
  2265. foreach ($this->ListChanges() as $sAttCode => $value)
  2266. {
  2267. $bUpdateAllowed = UserRights::IsActionAllowedOnAttribute(get_class($this), $sAttCode, UR_ACTION_MODIFY, DBObjectSet::FromObject($this));
  2268. if (!$bUpdateAllowed)
  2269. {
  2270. $oAttCode = MetaModel::GetAttributeDef(get_class($this), $sAttCode);
  2271. $aForbiddenFields[] = $oAttCode->GetLabel();
  2272. }
  2273. }
  2274. if (count($aForbiddenFields) > 0)
  2275. {
  2276. // Security issue
  2277. $this->m_bSecurityIssue = true;
  2278. $this->m_aCheckIssues[] = Dict::Format('UI:Delete:NotAllowedToUpdate_Fields',implode(', ', $aForbiddenFields));
  2279. }
  2280. }
  2281. }
  2282. protected function DoCheckToDelete(&$oDeletionPlan)
  2283. {
  2284. parent::DoCheckToDelete($oDeletionPlan);
  2285. // Plugins
  2286. //
  2287. foreach (MetaModel::EnumPlugins('iApplicationObjectExtension') as $oExtensionInstance)
  2288. {
  2289. $aNewIssues = $oExtensionInstance->OnCheckToDelete($this);
  2290. if (count($aNewIssues) > 0)
  2291. {
  2292. $this->m_aDeleteIssues = array_merge($this->m_aDeleteIssues, $aNewIssues);
  2293. }
  2294. }
  2295. // User rights
  2296. //
  2297. $bDeleteAllowed = UserRights::IsActionAllowed(get_class($this), UR_ACTION_DELETE, DBObjectSet::FromObject($this));
  2298. if (!$bDeleteAllowed)
  2299. {
  2300. // Security issue
  2301. $this->m_bSecurityIssue = true;
  2302. $this->m_aDeleteIssues[] = Dict::S('UI:Delete:NotAllowedToDelete');
  2303. }
  2304. }
  2305. /**
  2306. * Special display where the case log uses the whole "screen" at the bottom of the "Properties" tab
  2307. */
  2308. public function DisplayCaseLog(WebPage $oPage, $sAttCode, $sComment = '', $sPrefix = '', $bEditMode = false)
  2309. {
  2310. $oPage->SetCurrentTab(Dict::S('UI:PropertiesTab'));
  2311. $sClass = get_class($this);
  2312. $iFlags = $this->GetAttributeFlags($sAttCode);
  2313. if ( $iFlags & OPT_ATT_HIDDEN)
  2314. {
  2315. // The case log is hidden do nothing
  2316. }
  2317. else
  2318. {
  2319. $oAttDef = MetaModel::GetAttributeDef(get_class($this), $sAttCode);
  2320. $sInputId = $this->m_iFormId.'_'.$sAttCode;
  2321. if ((!$bEditMode) || ($iFlags & (OPT_ATT_READONLY|OPT_ATT_SLAVE)))
  2322. {
  2323. // Check if the attribute is not read-only becuase of a synchro...
  2324. $aReasons = array();
  2325. $sSynchroIcon = '';
  2326. if ($iFlags & OPT_ATT_SLAVE)
  2327. {
  2328. $iSynchroFlags = $this->GetSynchroReplicaFlags($sAttCode, $aReasons);
  2329. $sSynchroIcon = "&nbsp;<img id=\"synchro_$sInputId\" src=\"../images/transp-lock.png\" style=\"vertical-align:middle\"/>";
  2330. $sTip = '';
  2331. foreach($aReasons as $aRow)
  2332. {
  2333. $sTip .= "<p>Synchronized with {$aRow['name']} - {$aRow['description']}</p>";
  2334. }
  2335. $oPage->add_ready_script("$('#synchro_$sInputId').qtip( { content: '$sTip', show: 'mouseover', hide: 'mouseout', style: { name: 'dark', tip: 'leftTop' }, position: { corner: { target: 'rightMiddle', tooltip: 'leftTop' }} } );");
  2336. }
  2337. // Attribute is read-only
  2338. $sHTMLValue = $this->GetAsHTML($sAttCode);
  2339. $sHTMLValue .= '<input type="hidden" id="'.$sInputId.'" name="attr_'.$sPrefix.$sAttCode.'" value="'.htmlentities($this->Get($sAttCode), ENT_QUOTES, 'UTF-8').'"/>';
  2340. $aFieldsMap[$sAttCode] = $sInputId;
  2341. $sComment .= $sSynchroIcon;
  2342. }
  2343. else
  2344. {
  2345. $sValue = $this->Get($sAttCode);
  2346. $sDisplayValue = $this->GetEditValue($sAttCode);
  2347. $aArgs = array('this' => $this, 'formPrefix' => $sPrefix);
  2348. $sHTMLValue = "<span id=\"field_{$sInputId}\">".self::GetFormElementForField($oPage, $sClass, $sAttCode, $oAttDef, $sValue, $sDisplayValue, $sInputId, '', $iFlags, $aArgs).'</span>';
  2349. $aFieldsMap[$sAttCode] = $sInputId;
  2350. }
  2351. //$aVal = array('label' => '<span title="'.$oAttDef->GetDescription().'">'.$oAttDef->GetLabel().'</span>', 'value' => $sHTMLValue, 'comments' => $sComments, 'infos' => $sInfos);
  2352. $oPage->add('<fieldset><legend>'.$oAttDef->GetLabel().'&nbsp<span>'.$sComment.'</span></legend>');
  2353. $oPage->add($sHTMLValue);
  2354. $oPage->add('</fieldset>');
  2355. }
  2356. }
  2357. }
  2358. ?>