cmdbabstract.class.inc.php 92 KB

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