cmdbabstract.class.inc.php 89 KB

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