cmdbabstract.class.inc.php 72 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946
  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/utils.inc.php');
  32. require_once(APPROOT.'/application/applicationcontext.class.inc.php');
  33. require_once(APPROOT.'/application/ui.linkswidget.class.inc.php');
  34. require_once(APPROOT.'/application/ui.passwordwidget.class.inc.php');
  35. require_once(APPROOT.'/application/ui.extkeywidget.class.inc.php');
  36. require_once(APPROOT.'/application/ui.htmleditorwidget.class.inc.php');
  37. /**
  38. * All objects to be displayed in the application (either as a list or as details)
  39. * must implement this interface.
  40. */
  41. interface iDisplay
  42. {
  43. /**
  44. * Maps the given context parameter name to the appropriate filter/search code for this class
  45. * @param string $sContextParam Name of the context parameter, i.e. 'org_id'
  46. * @return string Filter code, i.e. 'customer_id'
  47. */
  48. public static function MapContextParam($sContextParam);
  49. /**
  50. * This function returns a 'hilight' CSS class, used to hilight a given row in a table
  51. * There are currently (i.e defined in the CSS) 4 possible values HILIGHT_CLASS_CRITICAL,
  52. * HILIGHT_CLASS_WARNING, HILIGHT_CLASS_OK, HILIGHT_CLASS_NONE
  53. * To Be overridden by derived classes
  54. * @param void
  55. * @return String The desired higlight class for the object/row
  56. */
  57. public function GetHilightClass();
  58. /**
  59. * Returns the relative path to the page that handles the display of the object
  60. * @return string
  61. */
  62. public static function GetUIPage();
  63. /**
  64. * Displays the details of the object
  65. */
  66. public function DisplayDetails(WebPage $oPage, $bEditMode = false);
  67. }
  68. abstract class cmdbAbstractObject extends CMDBObject implements iDisplay
  69. {
  70. protected $m_iFormId; // The ID of the form used to edit the object (when in edition mode !)
  71. public static function GetUIPage()
  72. {
  73. return '../pages/UI.php';
  74. }
  75. function DisplayBareHeader(WebPage $oPage, $bEditMode = false)
  76. {
  77. // Standard Header with name, actions menu and history block
  78. //
  79. // action menu
  80. $oSingletonFilter = new DBObjectSearch(get_class($this));
  81. $oSingletonFilter->AddCondition('id', $this->GetKey(), '=');
  82. $oBlock = new MenuBlock($oSingletonFilter, 'popup', false);
  83. $oBlock->Display($oPage, -1);
  84. // Master data sources
  85. $oReplicaSet = $this->GetMasterReplica();
  86. $bSynchronized = false;
  87. $bCreated = false;
  88. $bCanBeDeleted = false;
  89. $aMasterSources = array();
  90. if ($oReplicaSet->Count() > 0)
  91. {
  92. $bSynchronized = true;
  93. $sTip = "<p>The object is synchronized with an external data source</p>";
  94. while($aData = $oReplicaSet->FetchAssoc())
  95. {
  96. // Assumption: $aData['datasource'] will not be null because the data source id is always set...
  97. $sApplicationURL = $aData['datasource']->GetApplicationUrl($this, $aData['replica']);
  98. $sLink = "<a href=\"$sApplicationURL\" target=\"_blank\">".$aData['datasource']->GetName()."</a>";
  99. if ($aData['replica']->Get('status_dest_creator') == 1)
  100. {
  101. $sTip .= "<p>The object was <b>created</b> by the external data source $sLink</p>";
  102. $bCreated = true;
  103. }
  104. if ($bCreated)
  105. {
  106. $sDeletePolicy = $aData['datasource']->Get('delete_policy');
  107. if (($sDeletePolicy == 'delete') || ($sDeletePolicy == 'update_then_delete'))
  108. {
  109. $bCanBeDeleted = true;
  110. $sTip .= "<p>The object <b>can be deleted</b> by the external data source $sLink</p>";
  111. }
  112. }
  113. $aMasterSources[$aData['datasource']->GetKey()]['datasource'] = $aData['datasource'];
  114. $aMasterSources[$aData['datasource']->GetKey()]['url'] = $sLink;
  115. }
  116. }
  117. $sSynchroIcon = '';
  118. if ($bSynchronized)
  119. {
  120. $sTip .= "<p><b>List of data sources:</b></p>";
  121. foreach($aMasterSources as $aStruct)
  122. {
  123. $oDataSource = $aStruct['datasource'];
  124. $sLink = $aStruct['url'];
  125. $sTip .= "<p style=\"white-space:nowrap\">".$oDataSource->GetIcon(true, 'style="vertical-align:middle"')."&nbsp;$sLink</p>";
  126. }
  127. $sSynchroIcon = '&nbsp;<img style="vertical-align:middle;" id="synchro_icon" src="../images/locked.png"/>';
  128. $oPage->add_ready_script("$('#synchro_icon').qtip( { content: '$sTip', show: 'mouseover', hide: 'unfocus', style: { name: 'dark', tip: 'leftTop' }, position: { corner: { target: 'rightMiddle', tooltip: 'leftTop' }} } );");
  129. }
  130. $oPage->add("<div class=\"page_header\"><h1>".$this->GetIcon()."&nbsp;\n");
  131. $oPage->add(MetaModel::GetName(get_class($this)).": <span class=\"hilite\">".$this->GetName()."</span>$sSynchroIcon</h1>\n");
  132. $oPage->add("</div>\n");
  133. }
  134. function DisplayBareHistory(WebPage $oPage, $bEditMode = false)
  135. {
  136. // history block (with as a tab)
  137. $oHistoryFilter = new DBObjectSearch('CMDBChangeOp');
  138. $oHistoryFilter->AddCondition('objkey', $this->GetKey(), '=');
  139. $oHistoryFilter->AddCondition('objclass', get_class($this), '=');
  140. $oBlock = new HistoryBlock($oHistoryFilter, 'table', false);
  141. $oBlock->Display($oPage, -1);
  142. }
  143. function DisplayBareProperties(WebPage $oPage, $bEditMode = false)
  144. {
  145. $oPage->add($this->GetBareProperties($oPage, $bEditMode));
  146. }
  147. function DisplayBareRelations(WebPage $oPage, $bEditMode = false)
  148. {
  149. // Related objects: display all the linkset attributes, each as a separate tab
  150. // In the order described by the 'display' ZList
  151. $aList = $this->FlattenZList(MetaModel::GetZListItems(get_class($this), 'details'));
  152. if (count($aList) == 0)
  153. {
  154. // Empty ZList defined, display all the linkedset attributes defined
  155. $aList = array_keys(MetaModel::ListAttributeDefs(get_class($this)));
  156. }
  157. $sClass = get_class($this);
  158. foreach($aList as $sAttCode)
  159. {
  160. $oAttDef = MetaModel::GetAttributeDef(get_class($this), $sAttCode);
  161. // Display mode
  162. if (!$oAttDef->IsLinkset()) continue; // Process only linkset attributes...
  163. $oPage->SetCurrentTab($oAttDef->GetLabel());
  164. if ($bEditMode)
  165. {
  166. $iFlags = $this->GetAttributeFlags($sAttCode);
  167. $sInputId = $this->m_iFormId.'_'.$sAttCode;
  168. if (get_class($oAttDef) == 'AttributeLinkedSet')
  169. {
  170. // 1:n links
  171. $sTargetClass = $oAttDef->GetLinkedClass();
  172. if ($this->IsNew())
  173. {
  174. $oPage->p(Dict::Format('UI:BeforeAdding_Class_ObjectsSaveThisObject', MetaModel::GetName($sTargetClass)));
  175. }
  176. else
  177. {
  178. $oPage->p(MetaModel::GetClassIcon($sTargetClass)."&nbsp;".$oAttDef->GetDescription());
  179. $oFilter = new DBObjectSearch($sTargetClass);
  180. $oFilter->AddCondition($oAttDef->GetExtKeyToMe(), $this->GetKey(),'=');
  181. $aDefaults = array($oAttDef->GetExtKeyToMe() => $this->GetKey());
  182. $oAppContext = new ApplicationContext();
  183. foreach($oAppContext->GetNames() as $sKey)
  184. {
  185. // The linked object inherits the parent's value for the context
  186. if (MetaModel::IsValidAttCode($sClass, $sKey))
  187. {
  188. $aDefaults[$sKey] = $this->Get($sKey);
  189. }
  190. }
  191. $aParams = array(
  192. 'target_attr' => $oAttDef->GetExtKeyToMe(),
  193. 'object_id' => $this->GetKey(),
  194. 'menu' => true,
  195. 'default' => $aDefaults,
  196. );
  197. $oBlock = new DisplayBlock($oFilter, 'list', false);
  198. $oBlock->Display($oPage, $sInputId, $aParams);
  199. }
  200. }
  201. else // get_class($oAttDef) == 'AttributeLinkedSetIndirect'
  202. {
  203. // n:n links
  204. $sLinkedClass = $oAttDef->GetLinkedClass();
  205. $oLinkingAttDef = MetaModel::GetAttributeDef($sLinkedClass, $oAttDef->GetExtKeyToRemote());
  206. $sTargetClass = $oLinkingAttDef->GetTargetClass();
  207. $oPage->p(MetaModel::GetClassIcon($sTargetClass)."&nbsp;".$oAttDef->GetDescription().'<span id="busy_'.$sInputId.'"></span>');
  208. $sValue = $this->Get($sAttCode);
  209. $sDisplayValue = $this->GetEditValue($sAttCode);
  210. $aArgs = array('this' => $this);
  211. $sHTMLValue = "<span id=\"field_{$sInputId}\">".self::GetFormElementForField($oPage, $sClass, $sAttCode, $oAttDef, $sValue, $sDisplayValue, $sInputId, '', $iFlags, $aArgs).'</span>';
  212. $aFieldsMap[$sAttCode] = $sInputId;
  213. $oPage->add($sHTMLValue);
  214. }
  215. }
  216. else
  217. {
  218. // Display mode
  219. if (!$oAttDef->IsIndirect())
  220. {
  221. // 1:n links
  222. $sTargetClass = $oAttDef->GetLinkedClass();
  223. $aDefaults = array($oAttDef->GetExtKeyToMe() => $this->GetKey());
  224. $oAppContext = new ApplicationContext();
  225. foreach($oAppContext->GetNames() as $sKey)
  226. {
  227. // The linked object inherits the parent's value for the context
  228. if (MetaModel::IsValidAttCode($sClass, $sKey))
  229. {
  230. $aDefaults[$sKey] = $this->Get($sKey);
  231. }
  232. }
  233. $aParams = array(
  234. 'target_attr' => $oAttDef->GetExtKeyToMe(),
  235. 'object_id' => $this->GetKey(),
  236. 'menu' => false,
  237. 'default' => $aDefaults,
  238. );
  239. }
  240. else
  241. {
  242. // n:n links
  243. $sLinkedClass = $oAttDef->GetLinkedClass();
  244. $oLinkingAttDef = MetaModel::GetAttributeDef($sLinkedClass, $oAttDef->GetExtKeyToRemote());
  245. $sTargetClass = $oLinkingAttDef->GetTargetClass();
  246. $bMenu = ($this->Get($sAttCode)->Count() > 0); // The menu is enabled only if there are already some elements...
  247. $aParams = array(
  248. 'link_attr' => $oAttDef->GetExtKeyToMe(),
  249. 'object_id' => $this->GetKey(),
  250. 'target_attr' => $oAttDef->GetExtKeyToRemote(),
  251. 'view_link' => false,
  252. 'menu' => false,
  253. 'display_limit' => true, // By default limit the list to speed up the initial load & display
  254. );
  255. }
  256. $oPage->p(MetaModel::GetClassIcon($sTargetClass)."&nbsp;".$oAttDef->GetDescription());
  257. $oBlock = new DisplayBlock($this->Get($sAttCode)->GetFilter(), 'list', false);
  258. $oBlock->Display($oPage, 'rel_'.$sAttCode, $aParams);
  259. }
  260. }
  261. $oPage->SetCurrentTab('');
  262. if (!$bEditMode)
  263. {
  264. // Get the actual class of the current object
  265. // And look for triggers referring to it
  266. // If any trigger has been found then display a tab with notifications
  267. //
  268. $sClass = get_class($this);
  269. $sClassList = implode("', '", MetaModel::EnumParentClasses($sClass, ENUM_PARENT_CLASSES_ALL));
  270. $oTriggerSet = new CMDBObjectSet(DBObjectSearch::FromOQL("SELECT TriggerOnObject AS T WHERE T.target_class IN ('$sClassList')"));
  271. if ($oTriggerSet->Count() > 0)
  272. {
  273. $oPage->SetCurrentTab(Dict::S('UI:NotificationsTab'));
  274. // Display notifications regarding the object
  275. $iId = $this->GetKey();
  276. $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);
  277. $oBlock->Display($oPage, 'notifications', array());
  278. }
  279. }
  280. }
  281. function GetBareProperties(WebPage $oPage, $bEditMode = false)
  282. {
  283. $sHtml = '';
  284. $oAppContext = new ApplicationContext();
  285. $sStateAttCode = MetaModel::GetStateAttributeCode(get_class($this));
  286. $aDetails = array();
  287. $sClass = get_class($this);
  288. $aDetailsList = MetaModel::GetZListItems($sClass, 'details');
  289. $aDetailsStruct = self::ProcessZlist($aDetailsList, array('UI:PropertiesTab' => array()), 'UI:PropertiesTab', 'col1', '');
  290. // Compute the list of properties to display, first the attributes in the 'details' list, then
  291. // all the remaining attributes that are not external fields
  292. $sHtml = '';
  293. $aDetails = array();
  294. $iInputId = 0;
  295. foreach($aDetailsStruct as $sTab => $aCols )
  296. {
  297. $aDetails[$sTab] = array();
  298. ksort($aCols);
  299. $oPage->SetCurrentTab(Dict::S($sTab));
  300. $oPage->add('<table style="vertical-align:top"><tr>');
  301. foreach($aCols as $sColIndex => $aFieldsets)
  302. {
  303. $oPage->add('<td style="vertical-align:top">');
  304. //$aDetails[$sTab][$sColIndex] = array();
  305. $sLabel = '';
  306. $sPreviousLabel = '';
  307. $aDetails[$sTab][$sColIndex] = array();
  308. foreach($aFieldsets as $sFieldsetName => $aFields)
  309. {
  310. if (!empty($sFieldsetName) && ($sFieldsetName[0] != '_'))
  311. {
  312. $sLabel = $sFieldsetName;
  313. }
  314. else
  315. {
  316. $sLabel = '';
  317. }
  318. if ($sLabel != $sPreviousLabel)
  319. {
  320. if (!empty($sPreviousLabel))
  321. {
  322. $oPage->add('<fieldset>');
  323. $oPage->add('<legend>'.Dict::S($sPreviousLabel).'</legend>');
  324. }
  325. $oPage->Details($aDetails[$sTab][$sColIndex]);
  326. if (!empty($sPreviousLabel))
  327. {
  328. $oPage->add('</fieldset>');
  329. }
  330. $aDetails[$sTab][$sColIndex] = array();
  331. $sPreviousLabel = $sLabel;
  332. }
  333. foreach($aFields as $sAttCode)
  334. {
  335. $val = $this->GetFieldAsHtml($sClass, $sAttCode, $sStateAttCode);
  336. if ($val != null)
  337. {
  338. // Check if the attribute is not mastered by a synchro...
  339. $aReasons = array();
  340. $iSynchroFlags = $this->GetSynchroReplicaFlags($sAttCode, $aReasons);
  341. $sSynchroIcon = '';
  342. if ($iSynchroFlags & OPT_ATT_READONLY)
  343. {
  344. $sSynchroIcon = "&nbsp;<img id=\"synchro_$iInputId\" src=\"../images/transp-lock.png\" style=\"vertical-align:middle\"/>";
  345. $sTip = '';
  346. foreach($aReasons as $aRow)
  347. {
  348. $sTip .= "<p>Synchronized with {$aRow['name']} - {$aRow['description']}</p>";
  349. }
  350. $oPage->add_ready_script("$('#synchro_$iInputId').qtip( { content: '$sTip', show: 'mouseover', hide: 'mouseout', style: { name: 'dark', tip: 'leftTop' }, position: { corner: { target: 'rightMiddle', tooltip: 'leftTop' }} } );");
  351. }
  352. $val['value'] .= $sSynchroIcon;
  353. // The field is visible, add it to the current column
  354. $aDetails[$sTab][$sColIndex][] = $val;
  355. $iInputId++;
  356. }
  357. }
  358. }
  359. if (!empty($sPreviousLabel))
  360. {
  361. $oPage->add('<fieldset>');
  362. $oPage->add('<legend>'.Dict::S($sFieldsetName).'</legend>');
  363. }
  364. $oPage->Details($aDetails[$sTab][$sColIndex]);
  365. if (!empty($sPreviousLabel))
  366. {
  367. $oPage->add('</fieldset>');
  368. }
  369. $oPage->add('</td>');
  370. }
  371. $oPage->add('</tr></table>');
  372. }
  373. return $sHtml;
  374. }
  375. function DisplayDetails(WebPage $oPage, $bEditMode = false)
  376. {
  377. $sTemplate = Utils::ReadFromFile(MetaModel::GetDisplayTemplate(get_class($this)));
  378. if (!empty($sTemplate))
  379. {
  380. $oTemplate = new DisplayTemplate($sTemplate);
  381. // Note: to preserve backward compatibility with home-made templates, the placeholder '$pkey$' has been preserved
  382. // but the preferred method is to use '$id$'
  383. $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')));
  384. }
  385. else
  386. {
  387. // Object's details
  388. // template not found display the object using the *old style*
  389. $this->DisplayBareHeader($oPage, $bEditMode);
  390. $oPage->AddTabContainer(OBJECT_PROPERTIES_TAB);
  391. $oPage->SetCurrentTabContainer(OBJECT_PROPERTIES_TAB);
  392. $oPage->SetCurrentTab(Dict::S('UI:PropertiesTab'));
  393. $this->DisplayBareProperties($oPage, $bEditMode);
  394. $this->DisplayBareRelations($oPage, $bEditMode);
  395. $oPage->SetCurrentTab(Dict::S('UI:HistoryTab'));
  396. $this->DisplayBareHistory($oPage, $bEditMode);
  397. }
  398. }
  399. function DisplayPreview(WebPage $oPage)
  400. {
  401. $aDetails = array();
  402. $sClass = get_class($this);
  403. $aList = MetaModel::GetZListItems($sClass, 'preview');
  404. foreach($aList as $sAttCode)
  405. {
  406. $aDetails[] = array('label' => MetaModel::GetLabel($sClass, $sAttCode), 'value' =>$this->GetAsHTML($sAttCode));
  407. }
  408. $oPage->details($aDetails);
  409. }
  410. public static function DisplaySet(WebPage $oPage, CMDBObjectSet $oSet, $aExtraParams = array())
  411. {
  412. $oPage->add(self::GetDisplaySet($oPage, $oSet, $aExtraParams));
  413. }
  414. /**
  415. * Get the HTML fragment corresponding to the display of a table representing a set of objects
  416. * @param WebPage $oPage The page object is used for out-of-band information (mostly scripts) output
  417. * @param CMDBObjectSet The set of objects to display
  418. * @param Hash $aExtraParams Some extra configuration parameters to tweak the behavior of the display
  419. * @return String The HTML fragment representing the table of objects
  420. */
  421. public static function GetDisplaySet(WebPage $oPage, CMDBObjectSet $oSet, $aExtraParams = array())
  422. {
  423. if (empty($aExtraParams['currentId']))
  424. {
  425. $iListId = $oPage->GetUniqueId(); // Works only if not in an Ajax page !!
  426. }
  427. else
  428. {
  429. $iListId = $aExtraParams['currentId'];
  430. }
  431. // Initialize and check the parameters
  432. $bViewLink = isset($aExtraParams['view_link']) ? $aExtraParams['view_link'] : true;
  433. $sLinkageAttribute = isset($aExtraParams['link_attr']) ? $aExtraParams['link_attr'] : '';
  434. $iLinkedObjectId = isset($aExtraParams['object_id']) ? $aExtraParams['object_id'] : 0;
  435. $sTargetAttr = isset($aExtraParams['target_attr']) ? $aExtraParams['target_attr'] : '';
  436. if (!empty($sLinkageAttribute))
  437. {
  438. if($iLinkedObjectId == 0)
  439. {
  440. // if 'links' mode is requested the id of the object to link to must be specified
  441. throw new ApplicationException(Dict::S('UI:Error:MandatoryTemplateParameter_object_id'));
  442. }
  443. if($sTargetAttr == '')
  444. {
  445. // if 'links' mode is requested the d of the object to link to must be specified
  446. throw new ApplicationException(Dict::S('UI:Error:MandatoryTemplateParameter_target_attr'));
  447. }
  448. }
  449. $bDisplayMenu = isset($aExtraParams['menu']) ? $aExtraParams['menu'] == true : true;
  450. $bTruncated = isset($aExtraParams['truncated']) ? $aExtraParams['truncated'] == true : true;
  451. $bSelectMode = isset($aExtraParams['selection_mode']) ? $aExtraParams['selection_mode'] == true : false;
  452. $bSingleSelectMode = isset($aExtraParams['selection_type']) ? ($aExtraParams['selection_type'] == 'single') : false;
  453. $aExtraFields = isset($aExtraParams['extra_fields']) ? explode(',', trim($aExtraParams['extra_fields'])) : array();
  454. $sHtml = '';
  455. $oAppContext = new ApplicationContext();
  456. $sClassName = $oSet->GetFilter()->GetClass();
  457. $aAttribs = array();
  458. $sZListName = isset($aExtraParams['zlist']) ? ($aExtraParams['zlist']) : 'list';
  459. $aList = self::FlattenZList(MetaModel::GetZListItems($sClassName, $sZListName));
  460. $aList = array_merge($aList, $aExtraFields);
  461. // Filter the list to removed linked set since we are not able to display them here
  462. foreach($aList as $index => $sAttCode)
  463. {
  464. $oAttDef = MetaModel::GetAttributeDef($sClassName, $sAttCode);
  465. if ($oAttDef instanceof AttributeLinkedSet)
  466. {
  467. // Removed from the display list
  468. unset($aList[$index]);
  469. }
  470. }
  471. if (!empty($sLinkageAttribute))
  472. {
  473. // The set to display is in fact a set of links between the object specified in the $sLinkageAttribute
  474. // and other objects...
  475. // The display will then group all the attributes related to the link itself:
  476. // | Link_attr1 | link_attr2 | ... || Object_attr1 | Object_attr2 | Object_attr3 | .. | Object_attr_n |
  477. $aDisplayList = array();
  478. $aAttDefs = MetaModel::ListAttributeDefs($sClassName);
  479. assert(isset($aAttDefs[$sLinkageAttribute]));
  480. $oAttDef = $aAttDefs[$sLinkageAttribute];
  481. assert($oAttDef->IsExternalKey());
  482. // First display all the attributes specific to the link record
  483. foreach($aList as $sLinkAttCode)
  484. {
  485. $oLinkAttDef = $aAttDefs[$sLinkAttCode];
  486. if ( (!$oLinkAttDef->IsExternalKey()) && (!$oLinkAttDef->IsExternalField()) )
  487. {
  488. $aDisplayList[] = $sLinkAttCode;
  489. }
  490. }
  491. // Then display all the attributes neither specific to the link record nor to the 'linkage' object (because the latter are constant)
  492. foreach($aList as $sLinkAttCode)
  493. {
  494. $oLinkAttDef = $aAttDefs[$sLinkAttCode];
  495. if (($oLinkAttDef->IsExternalKey() && ($sLinkAttCode != $sLinkageAttribute))
  496. || ($oLinkAttDef->IsExternalField() && ($oLinkAttDef->GetKeyAttCode()!=$sLinkageAttribute)) )
  497. {
  498. $aDisplayList[] = $sLinkAttCode;
  499. }
  500. }
  501. // First display all the attributes specific to the link
  502. // Then display all the attributes linked to the other end of the relationship
  503. $aList = $aDisplayList;
  504. }
  505. if ($bSelectMode)
  506. {
  507. if (!$bSingleSelectMode)
  508. {
  509. $aAttribs['form::select'] = array('label' => "<input type=\"checkbox\" onClick=\"CheckAll('.selectList{$iListId}', this.checked);\"></input>", 'description' => Dict::S('UI:SelectAllToggle+'));
  510. }
  511. else
  512. {
  513. $aAttribs['form::select'] = array('label' => "", 'description' => '');
  514. }
  515. }
  516. if ($bViewLink)
  517. {
  518. $aAttribs['key'] = array('label' => MetaModel::GetName($sClassName), 'description' => '');
  519. }
  520. foreach($aList as $sAttCode)
  521. {
  522. $aAttribs[$sAttCode] = array('label' => MetaModel::GetLabel($sClassName, $sAttCode), 'description' => MetaModel::GetDescription($sClassName, $sAttCode));
  523. }
  524. $aValues = array();
  525. $bDisplayLimit = isset($aExtraParams['display_limit']) ? $aExtraParams['display_limit'] : true;
  526. $iMaxObjects = -1;
  527. if ($bDisplayLimit && $bTruncated)
  528. {
  529. if ($oSet->Count() > MetaModel::GetConfig()->GetMaxDisplayLimit())
  530. {
  531. $iMaxObjects = MetaModel::GetConfig()->GetMinDisplayLimit();
  532. $oSet->SetLimit($iMaxObjects);
  533. }
  534. }
  535. $oSet->Seek(0);
  536. while (($oObj = $oSet->Fetch()) && ($iMaxObjects != 0))
  537. {
  538. $aRow = array();
  539. $sHilightClass = $oObj->GetHilightClass();
  540. if ($sHilightClass != '')
  541. {
  542. $aRow['@class'] = $sHilightClass;
  543. }
  544. if ($bViewLink)
  545. {
  546. $aRow['key'] = $oObj->GetHyperLink();
  547. }
  548. if ($bSelectMode)
  549. {
  550. if ($bSingleSelectMode)
  551. {
  552. $aRow['form::select'] = "<input type=\"radio\" class=\"selectList{$iListId}\" name=\"selectObject\" value=\"".$oObj->GetKey()."\"></input>";
  553. }
  554. else
  555. {
  556. $aRow['form::select'] = "<input type=\"checkBox\" class=\"selectList{$iListId}\" name=\"selectObject[]\" value=\"".$oObj->GetKey()."\"></input>";
  557. }
  558. }
  559. foreach($aList as $sAttCode)
  560. {
  561. $aRow[$sAttCode] = $oObj->GetAsHTML($sAttCode);
  562. }
  563. $aValues[] = $aRow;
  564. $iMaxObjects--;
  565. }
  566. $sHtml .= '<table class="listContainer">';
  567. $sColspan = '';
  568. // if (isset($aExtraParams['block_id']))
  569. // {
  570. // $divId = $aExtraParams['block_id'];
  571. // }
  572. // else
  573. // {
  574. // $divId = 'missingblockid';
  575. // }
  576. $sFilter = $oSet->GetFilter()->serialize();
  577. $iMinDisplayLimit = MetaModel::GetConfig()->GetMinDisplayLimit();
  578. $sCollapsedLabel = Dict::Format('UI:TruncatedResults', $iMinDisplayLimit, $oSet->Count());
  579. $sLinkLabel = Dict::S('UI:DisplayAll');
  580. foreach($oSet->GetFilter()->GetInternalParams() as $sName => $sValue)
  581. {
  582. $aExtraParams['query_params'][$sName] = $sValue;
  583. }
  584. if ($bDisplayLimit && $bTruncated && ($oSet->Count() > MetaModel::GetConfig()->GetMaxDisplayLimit()))
  585. {
  586. // list truncated
  587. $aExtraParams['display_limit'] = true;
  588. $sHtml .= '<tr class="containerHeader"><td><span id="lbl_'.$iListId.'">'.$sCollapsedLabel.'</span>&nbsp;&nbsp;<a class="truncated" id="trc_'.$iListId.'">'.$sLinkLabel.'</a></td><td>';
  589. $oPage->add_ready_script(
  590. <<<EOF
  591. $('#$iListId table.listResults').addClass('truncated');
  592. $('#$iListId table.listResults tr:last td').addClass('truncated');
  593. EOF
  594. );
  595. }
  596. else if ($bDisplayLimit && !$bTruncated && ($oSet->Count() > MetaModel::GetConfig()->GetMaxDisplayLimit()))
  597. {
  598. // Collapsible list
  599. $aExtraParams['display_limit'] = true;
  600. $sHtml .= '<tr class="containerHeader"><td><span id="lbl_'.$iListId.'">'.Dict::Format('UI:CountOfResults', $oSet->Count()).'</span><a class="truncated" id="trc_'.$iListId.'">'.Dict::S('UI:CollapseList').'</a></td><td>';
  601. }
  602. $aExtraParams['truncated'] = false; // To expand the full list when clicked
  603. $sExtraParamsExpand = addslashes(str_replace('"', "'", json_encode($aExtraParams))); // JSON encode, change the style of the quotes and escape them
  604. $oPage->add_ready_script(
  605. <<<EOF
  606. // Handle truncated lists
  607. $('#trc_$iListId').click(function()
  608. {
  609. var state = {};
  610. var currentState = $.bbq.getState( this.id, true ) || 'close';
  611. // Toggle the state!
  612. if (currentState == 'close')
  613. {
  614. state[ this.id ] = 'open';
  615. }
  616. else
  617. {
  618. state[ this.id ] = 'close';
  619. }
  620. $.bbq.pushState( state );
  621. $(this).trigger(state[this.id]);
  622. });
  623. $('#trc_$iListId').unbind('open');
  624. $('#trc_$iListId').bind('open', function()
  625. {
  626. ReloadTruncatedList('$iListId', '$sFilter', '$sExtraParamsExpand');
  627. });
  628. $('#trc_$iListId').unbind('close');
  629. $('#trc_$iListId').bind('close', function()
  630. {
  631. TruncateList('$iListId', $iMinDisplayLimit, '$sCollapsedLabel', '$sLinkLabel');
  632. });
  633. EOF
  634. );
  635. if ($bDisplayMenu)
  636. {
  637. $oMenuBlock = new MenuBlock($oSet->GetFilter());
  638. $sColspan = 'colspan="2"';
  639. $aMenuExtraParams = $aExtraParams;
  640. if (!empty($sLinkageAttribute))
  641. {
  642. //$aMenuExtraParams['linkage'] = $sLinkageAttribute;
  643. $aMenuExtraParams = $aExtraParams;
  644. }
  645. $sHtml .= $oMenuBlock->GetRenderContent($oPage, $aMenuExtraParams, $iListId);
  646. $sHtml .= '</td></tr>';
  647. }
  648. $sHtml .= "<tr><td $sColspan>";
  649. $sHtml .= $oPage->GetTable($aAttribs, $aValues);
  650. $sHtml .= '</td></tr>';
  651. $sHtml .= '</table>';
  652. return $sHtml;
  653. }
  654. public static function GetDisplayExtendedSet(WebPage $oPage, CMDBObjectSet $oSet, $aExtraParams = array())
  655. {
  656. static $iListId = 0;
  657. $iListId++;
  658. $aList = array();
  659. // Initialize and check the parameters
  660. $bViewLink = isset($aExtraParams['view_link']) ? $aExtraParams['view_link'] : true;
  661. $bDisplayMenu = isset($aExtraParams['menu']) ? $aExtraParams['menu'] == true : true;
  662. // Check if there is a list of aliases to limit the display to...
  663. $aDisplayAliases = isset($aExtraParams['display_aliases']) ? explode(',', $aExtraParams['display_aliases']) : array();
  664. $sHtml = '';
  665. $oAppContext = new ApplicationContext();
  666. $aClasses = $oSet->GetFilter()->GetSelectedClasses();
  667. $aAuthorizedClasses = array();
  668. foreach($aClasses as $sAlias => $sClassName)
  669. {
  670. if ( (UserRights::IsActionAllowed($sClassName, UR_ACTION_READ, $oSet) && (UR_ALLOWED_YES || UR_ALLOWED_DEPENDS)) &&
  671. ( (count($aDisplayAliases) == 0) || (in_array($sAlias, $aDisplayAliases))) )
  672. {
  673. $aAuthorizedClasses[$sAlias] = $sClassName;
  674. }
  675. }
  676. $aAttribs = array();
  677. foreach($aAuthorizedClasses as $sAlias => $sClassName) // TO DO: check if the user has enough rights to view the classes of the list...
  678. {
  679. $aList[$sClassName] = MetaModel::GetZListItems($sClassName, 'list');
  680. if ($bViewLink)
  681. {
  682. $aAttribs['key_'.$sAlias] = array('label' => MetaModel::GetName($sClassName), 'description' => '');
  683. }
  684. foreach($aList[$sClassName] as $sAttCode)
  685. {
  686. $aAttribs[$sAttCode.'_'.$sAlias] = array('label' => MetaModel::GetLabel($sClassName, $sAttCode), 'description' => MetaModel::GetDescription($sClassName, $sAttCode));
  687. }
  688. }
  689. $aValues = array();
  690. $oSet->Seek(0);
  691. $bDisplayLimit = isset($aExtraParams['display_limit']) ? $aExtraParams['display_limit'] : true;
  692. $iMaxObjects = -1;
  693. if ($bDisplayLimit)
  694. {
  695. if ($oSet->Count() > MetaModel::GetConfig()->GetMaxDisplayLimit())
  696. {
  697. $iMaxObjects = MetaModel::GetConfig()->GetMinDisplayLimit();
  698. }
  699. }
  700. while (($aObjects = $oSet->FetchAssoc()) && ($iMaxObjects != 0))
  701. {
  702. $aRow = array();
  703. foreach($aAuthorizedClasses as $sAlias => $sClassName) // TO DO: check if the user has enough rights to view the classes of the list...
  704. {
  705. if ($bViewLink)
  706. {
  707. if (is_null($aObjects[$sAlias]))
  708. {
  709. $aRow['key_'.$sAlias] = '';
  710. }
  711. else
  712. {
  713. $aRow['key_'.$sAlias] = $aObjects[$sAlias]->GetHyperLink();
  714. }
  715. }
  716. foreach($aList[$sClassName] as $sAttCode)
  717. {
  718. if (is_null($aObjects[$sAlias]))
  719. {
  720. $aRow[$sAttCode.'_'.$sAlias] = '';
  721. }
  722. else
  723. {
  724. $aRow[$sAttCode.'_'.$sAlias] = $aObjects[$sAlias]->GetAsHTML($sAttCode);
  725. }
  726. }
  727. }
  728. $aValues[] = $aRow;
  729. $iMaxObjects--;
  730. }
  731. $sHtml .= '<table class="listContainer">';
  732. $sColspan = '';
  733. if ($bDisplayMenu)
  734. {
  735. $oMenuBlock = new MenuBlock($oSet->GetFilter());
  736. $sColspan = 'colspan="2"';
  737. $aMenuExtraParams = $aExtraParams;
  738. if (!empty($sLinkageAttribute))
  739. {
  740. $aMenuExtraParams = $aExtraParams;
  741. }
  742. if ($bDisplayLimit && ($oSet->Count() > MetaModel::GetConfig()->GetMaxDisplayLimit()))
  743. {
  744. // list truncated
  745. $divId = $aExtraParams['block_id'];
  746. $sFilter = $oSet->GetFilter()->serialize();
  747. $aExtraParams['display_limit'] = false; // To expand the full list
  748. $sExtraParams = addslashes(str_replace('"', "'", json_encode($aExtraParams))); // JSON encode, change the style of the quotes and escape them
  749. $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>';
  750. $oPage->add_ready_script("$('#{$divId} table.listResults').addClass('truncated');");
  751. $oPage->add_ready_script("$('#{$divId} table.listResults tr:last td').addClass('truncated');");
  752. }
  753. else
  754. {
  755. // Full list
  756. $sHtml .= '<tr class="containerHeader"><td>&nbsp;'.Dict::Format('UI:CountOfResults', $oSet->Count()).'</td><td>';
  757. }
  758. $sHtml .= $oMenuBlock->GetRenderContent($oPage, $aMenuExtraParams, $aMenuExtraParams['currentId']);
  759. $sHtml .= '</td></tr>';
  760. }
  761. $sHtml .= "<tr><td $sColspan>";
  762. $sHtml .= $oPage->GetTable($aAttribs, $aValues);
  763. $sHtml .= '</td></tr>';
  764. $sHtml .= '</table>';
  765. return $sHtml;
  766. }
  767. static function DisplaySetAsCSV(WebPage $oPage, CMDBObjectSet $oSet, $aParams = array())
  768. {
  769. $oPage->add(self::GetSetAsCSV($oSet, $aParams));
  770. }
  771. static function GetSetAsCSV(DBObjectSet $oSet, $aParams = array())
  772. {
  773. $sSeparator = isset($aParams['separator']) ? $aParams['separator'] : ','; // default separator is comma
  774. $sTextQualifier = isset($aParams['text_qualifier']) ? $aParams['text_qualifier'] : '"'; // default text qualifier is double quote
  775. $aList = array();
  776. $oAppContext = new ApplicationContext();
  777. $aClasses = $oSet->GetFilter()->GetSelectedClasses();
  778. $aAuthorizedClasses = array();
  779. foreach($aClasses as $sAlias => $sClassName)
  780. {
  781. if (UserRights::IsActionAllowed($sClassName, UR_ACTION_READ, $oSet) && (UR_ALLOWED_YES || UR_ALLOWED_DEPENDS))
  782. {
  783. $aAuthorizedClasses[$sAlias] = $sClassName;
  784. }
  785. }
  786. $aAttribs = array();
  787. $aHeader = array();
  788. foreach($aAuthorizedClasses as $sAlias => $sClassName)
  789. {
  790. foreach(MetaModel::ListAttributeDefs($sClassName) as $sAttCode => $oAttDef)
  791. {
  792. if ((($oAttDef->IsExternalField()) || ($oAttDef->IsWritable())) && $oAttDef->IsScalar())
  793. {
  794. $aList[$sClassName][$sAttCode] = $oAttDef;
  795. }
  796. }
  797. $aHeader[] = 'id';
  798. foreach($aList[$sClassName] as $sAttCode => $oAttDef)
  799. {
  800. $sStar = '';
  801. if ($oAttDef->IsExternalField())
  802. {
  803. $sExtKeyLabel = MetaModel::GetLabel($sClassName, $oAttDef->GetKeyAttCode());
  804. $oExtKeyAttDef = MetaModel::GetAttributeDef($sClassName, $oAttDef->GetKeyAttCode());
  805. if (!$oExtKeyAttDef->IsNullAllowed() && isset($aParams['showMandatoryFields']))
  806. {
  807. $sStar = '*';
  808. }
  809. $sRemoteAttLabel = MetaModel::GetLabel($oAttDef->GetTargetClass(), $oAttDef->GetExtAttCode());
  810. $oTargetAttDef = MetaModel::GetAttributeDef($oAttDef->GetTargetClass(), $oAttDef->GetExtAttCode());
  811. $sSuffix = '';
  812. if ($oTargetAttDef->IsExternalKey())
  813. {
  814. $sSuffix = '->id';
  815. }
  816. $aHeader[] = $sExtKeyLabel.'->'.$sRemoteAttLabel.$sSuffix.$sStar;
  817. }
  818. else
  819. {
  820. if (!$oAttDef->IsNullAllowed() && isset($aParams['showMandatoryFields']))
  821. {
  822. $sStar = '*';
  823. }
  824. $aHeader[] = MetaModel::GetLabel($sClassName, $sAttCode).$sStar;
  825. }
  826. }
  827. }
  828. $sHtml = implode($sSeparator, $aHeader)."\n";
  829. $oSet->Seek(0);
  830. while ($aObjects = $oSet->FetchAssoc())
  831. {
  832. $aRow = array();
  833. foreach($aAuthorizedClasses as $sAlias => $sClassName)
  834. {
  835. $oObj = $aObjects[$sAlias];
  836. if (is_null($oObj))
  837. {
  838. $aRow[] = '';
  839. }
  840. else
  841. {
  842. $aRow[] = $oObj->GetKey();
  843. }
  844. foreach($aList[$sClassName] as $sAttCode => $oAttDef)
  845. {
  846. if (is_null($oObj))
  847. {
  848. $aRow[] = '';
  849. }
  850. else
  851. {
  852. $aRow[] = $oObj->GetAsCSV($sAttCode, $sSeparator, '\\');
  853. }
  854. }
  855. }
  856. $sHtml .= implode($sSeparator, $aRow)."\n";
  857. }
  858. return $sHtml;
  859. }
  860. static function DisplaySetAsXML(WebPage $oPage, CMDBObjectSet $oSet, $aParams = array())
  861. {
  862. $oAppContext = new ApplicationContext();
  863. $aClasses = $oSet->GetFilter()->GetSelectedClasses();
  864. $aAuthorizedClasses = array();
  865. foreach($aClasses as $sAlias => $sClassName)
  866. {
  867. if (UserRights::IsActionAllowed($sClassName, UR_ACTION_READ, $oSet) && (UR_ALLOWED_YES || UR_ALLOWED_DEPENDS))
  868. {
  869. $aAuthorizedClasses[$sAlias] = $sClassName;
  870. }
  871. }
  872. $aAttribs = array();
  873. $aList = array();
  874. $aList[$sClassName] = MetaModel::GetZListItems($sClassName, 'details');
  875. $oPage->add("<Set>\n");
  876. $oSet->Seek(0);
  877. while ($aObjects = $oSet->FetchAssoc())
  878. {
  879. if (count($aAuthorizedClasses) > 1)
  880. {
  881. $oPage->add("<Row>\n");
  882. }
  883. foreach($aAuthorizedClasses as $sAlias => $sClassName)
  884. {
  885. $oObj = $aObjects[$sAlias];
  886. if (is_null($oObj))
  887. {
  888. $oPage->add("<$sClassName alias=\"$sAlias\" id=\"null\">\n");
  889. }
  890. else
  891. {
  892. $sClassName = get_class($oObj);
  893. $oPage->add("<$sClassName alias=\"$sAlias\" id=\"".$oObj->GetKey()."\">\n");
  894. }
  895. foreach(MetaModel::ListAttributeDefs($sClassName) as $sAttCode=>$oAttDef)
  896. {
  897. if (is_null($oObj))
  898. {
  899. $oPage->add("<$sAttCode>null</$sAttCode>\n");
  900. }
  901. else
  902. {
  903. if (($oAttDef->IsWritable()) && ($oAttDef->IsScalar()))
  904. {
  905. $sValue = $oObj->GetAsXML($sAttCode);
  906. $oPage->add("<$sAttCode>$sValue</$sAttCode>\n");
  907. }
  908. }
  909. }
  910. $oPage->add("</$sClassName>\n");
  911. }
  912. if (count($aAuthorizedClasses) > 1)
  913. {
  914. $oPage->add("</Row>\n");
  915. }
  916. }
  917. $oPage->add("</Set>\n");
  918. }
  919. public static function DisplaySearchForm(WebPage $oPage, CMDBObjectSet $oSet, $aExtraParams = array())
  920. {
  921. $oPage->add(self::GetSearchForm($oPage, $oSet, $aExtraParams));
  922. }
  923. public static function GetSearchForm(WebPage $oPage, CMDBObjectSet $oSet, $aExtraParams = array())
  924. {
  925. static $iSearchFormId = 0;
  926. $oAppContext = new ApplicationContext();
  927. $sHtml = '';
  928. $numCols=4;
  929. $sClassName = $oSet->GetFilter()->GetClass();
  930. // Romain: temporarily removed the tab "OQL query" because it was not finalized
  931. // (especially when used to add a link)
  932. /*
  933. $sHtml .= "<div class=\"mini_tabs\" id=\"mini_tabs{$iSearchFormId}\"><ul>
  934. <li><a href=\"#\" onClick=\"$('div.mini_tab{$iSearchFormId}').toggle();$('#mini_tabs{$iSearchFormId} ul li a').toggleClass('selected');\">".Dict::S('UI:OQLQueryTab')."</a></li>
  935. <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>
  936. </ul></div>\n";
  937. */
  938. // Simple search form
  939. if (isset($aExtraParams['currentId']))
  940. {
  941. $sSearchFormId = $aExtraParams['currentId'];
  942. }
  943. else
  944. {
  945. $iSearchFormId = $oPage->GetUniqueId();
  946. $sSearchFormId = 'SimpleSearchForm'.$iSearchFormId;
  947. $sHtml .= "<div id=\"ds_$sSearchFormId\" class=\"mini_tab{$iSearchFormId}\">\n";
  948. }
  949. // Check if the current class has some sub-classes
  950. if (isset($aExtraParams['baseClass']))
  951. {
  952. $sRootClass = $aExtraParams['baseClass'];
  953. }
  954. else
  955. {
  956. $sRootClass = $sClassName;
  957. }
  958. $aSubClasses = MetaModel::GetSubclasses($sRootClass);
  959. if (count($aSubClasses) > 0)
  960. {
  961. $aOptions = array();
  962. $aOptions[MetaModel::GetName($sRootClass)] = "<option value=\"$sRootClass\">".MetaModel::GetName($sRootClass)."</options>\n";
  963. foreach($aSubClasses as $sSubclassName)
  964. {
  965. $aOptions[MetaModel::GetName($sSubclassName)] = "<option value=\"$sSubclassName\">".MetaModel::GetName($sSubclassName)."</options>\n";
  966. }
  967. $aOptions[MetaModel::GetName($sClassName)] = "<option selected value=\"$sClassName\">".MetaModel::GetName($sClassName)."</options>\n";
  968. ksort($aOptions);
  969. $sContext = $oAppContext->GetForLink();
  970. $sClassesCombo = "<select name=\"class\" onChange=\"ReloadSearchForm('$sSearchFormId', this.value, '$sRootClass', '$sContext')\">\n".implode('', $aOptions)."</select>\n";
  971. }
  972. else
  973. {
  974. $sClassesCombo = MetaModel::GetName($sClassName);
  975. }
  976. $oUnlimitedFilter = new DBObjectSearch($sClassName);
  977. $sHtml .= "<form id=\"fs_{$sSearchFormId}\" action=\"../pages/UI.php\">\n"; // Don't use $_SERVER['SCRIPT_NAME'] since the form may be called asynchronously (from ajax.php)
  978. $sHtml .= "<h2>".Dict::Format('UI:SearchFor_Class_Objects', $sClassesCombo)."</h2>\n";
  979. $index = 0;
  980. $sHtml .= "<p>\n";
  981. $aFilterCriteria = $oSet->GetFilter()->GetCriteria();
  982. $aMapCriteria = array();
  983. foreach($aFilterCriteria as $aCriteria)
  984. {
  985. $aMapCriteria[$aCriteria['filtercode']][] = array('value' => $aCriteria['value'], 'opcode' => $aCriteria['opcode']);
  986. }
  987. $aList = MetaModel::GetZListItems($sClassName, 'standard_search');
  988. foreach($aList as $sFilterCode)
  989. {
  990. //$oAppContext->Reset($sFilterCode); // Make sure the same parameter will not be passed twice
  991. $sHtml .= '<span style="white-space: nowrap;padding:5px;display:inline-block;">';
  992. $sFilterValue = '';
  993. $sFilterValue = utils::ReadParam($sFilterCode, '');
  994. $sFilterOpCode = null; // Use the default 'loose' OpCode
  995. if (empty($sFilterValue))
  996. {
  997. if (isset($aMapCriteria[$sFilterCode]))
  998. {
  999. if (count($aMapCriteria[$sFilterCode]) > 1)
  1000. {
  1001. $sFilterValue = Dict::S('UI:SearchValue:Mixed');
  1002. }
  1003. else
  1004. {
  1005. $sFilterValue = $aMapCriteria[$sFilterCode][0]['value'];
  1006. $sFilterOpCode = $aMapCriteria[$sFilterCode][0]['opcode'];
  1007. }
  1008. if ($sFilterCode != 'company')
  1009. {
  1010. $oUnlimitedFilter->AddCondition($sFilterCode, $sFilterValue, $sFilterOpCode);
  1011. }
  1012. }
  1013. }
  1014. $aAllowedValues = MetaModel::GetAllowedValues_flt($sClassName, $sFilterCode, $aExtraParams);
  1015. if ($aAllowedValues != null)
  1016. {
  1017. //Enum field or external key, display a combo
  1018. $sValue = "<select name=\"$sFilterCode\">\n";
  1019. $sValue .= "<option value=\"\">".Dict::S('UI:SearchValue:Any')."</option>\n";
  1020. foreach($aAllowedValues as $key => $value)
  1021. {
  1022. if ($sFilterValue == $key)
  1023. {
  1024. $sSelected = ' selected';
  1025. }
  1026. else
  1027. {
  1028. $sSelected = '';
  1029. }
  1030. $sValue .= "<option value=\"$key\"$sSelected>$value</option>\n";
  1031. }
  1032. $sValue .= "</select>\n";
  1033. $sHtml .= "<label>".MetaModel::GetFilterLabel($sClassName, $sFilterCode).":</label>&nbsp;$sValue\n";
  1034. unset($aExtraParams[$sFilterCode]);
  1035. }
  1036. else
  1037. {
  1038. // Any value is possible, display an input box
  1039. $sHtml .= "<label>".MetaModel::GetFilterLabel($sClassName, $sFilterCode).":</label>&nbsp;<input class=\"textSearch\" name=\"$sFilterCode\" value=\"$sFilterValue\"/>\n";
  1040. unset($aExtraParams[$sFilterCode]);
  1041. }
  1042. $index++;
  1043. $sHtml .= '</span> ';
  1044. }
  1045. $sHtml .= "</p>\n";
  1046. $sHtml .= "<p align=\"right\"><input type=\"submit\" value=\"".Dict::S('UI:Button:Search')."\"></p>\n";
  1047. foreach($aExtraParams as $sName => $sValue)
  1048. {
  1049. $sHtml .= "<input type=\"hidden\" name=\"$sName\" value=\"$sValue\" />\n";
  1050. }
  1051. $sHtml .= "<input type=\"hidden\" name=\"class\" value=\"$sClassName\" />\n";
  1052. $sHtml .= "<input type=\"hidden\" name=\"dosearch\" value=\"1\" />\n";
  1053. $sHtml .= "<input type=\"hidden\" name=\"operation\" value=\"search_form\" />\n";
  1054. $sHtml .= $oAppContext->GetForForm();
  1055. $sHtml .= "</form>\n";
  1056. if (!isset($aExtraParams['currentId']))
  1057. {
  1058. $sHtml .= "</div><!-- Simple search form -->\n";
  1059. }
  1060. /*
  1061. // OQL query builder
  1062. $sHtml .= "<div id=\"OQLQuery{$iSearchFormId}\" style=\"display:none\" class=\"mini_tab{$iSearchFormId}\">\n";
  1063. $sHtml .= "<h1>".Dict::S('UI:OQLQueryBuilderTitle')."</h1>\n";
  1064. $sHtml .= "<form id=\"formOQL{$iSearchFormId}\"><table style=\"width:80%;\"><tr style=\"vertical-align:top\">\n";
  1065. $sHtml .= "<td style=\"text-align:right\"><label>SELECT&nbsp;</label><select name=\"oql_class\">";
  1066. $aClasses = MetaModel::EnumChildClasses($sClassName, ENUM_CHILD_CLASSES_ALL);
  1067. $sSelectedClass = utils::ReadParam('oql_class', $sClassName);
  1068. $sOQLClause = utils::ReadParam('oql_clause', '');
  1069. asort($aClasses);
  1070. foreach($aClasses as $sChildClass)
  1071. {
  1072. $sSelected = ($sChildClass == $sSelectedClass) ? 'selected' : '';
  1073. $sHtml.= "<option value=\"$sChildClass\" $sSelected>".MetaModel::GetName($sChildClass)."</option>\n";
  1074. }
  1075. $sHtml .= "</select>&nbsp;</td><td>\n";
  1076. $sHtml .= "<textarea name=\"oql_clause\" style=\"width:100%\">$sOQLClause</textarea></td></tr>\n";
  1077. $sHtml .= "<tr><td colspan=\"2\" style=\"text-align:right\"><input type=\"submit\" value=\"".Dict::S('UI:Button:Query')."\"></td></tr>\n";
  1078. $sHtml .= "<input type=\"hidden\" name=\"dosearch\" value=\"1\" />\n";
  1079. foreach($aExtraParams as $sName => $sValue)
  1080. {
  1081. $sHtml .= "<input type=\"hidden\" name=\"$sName\" value=\"$sValue\" />\n";
  1082. }
  1083. $sHtml .= "<input type=\"hidden\" name=\"operation\" value=\"search_oql\" />\n";
  1084. $sHtml .= $oAppContext->GetForForm();
  1085. $sHtml .= "</table></form>\n";
  1086. $sHtml .= "</div><!-- OQL query form -->\n";
  1087. */
  1088. return $sHtml;
  1089. }
  1090. public static function GetFormElementForField($oPage, $sClass, $sAttCode, $oAttDef, $value = '', $sDisplayValue = '', $iId = '', $sNameSuffix = '', $iFlags = 0, $aArgs = array())
  1091. {
  1092. static $iInputId = 0;
  1093. $sFieldPrefix = '';
  1094. $sFormPrefix = isset($aArgs['formPrefix']) ? $aArgs['formPrefix'] : '';
  1095. $sFieldPrefix = isset($aArgs['prefix']) ? $sFormPrefix.$aArgs['prefix'] : $sFormPrefix;
  1096. if (isset($aArgs[$sAttCode]) && empty($value))
  1097. {
  1098. // default value passed by the context (either the app context of the operation)
  1099. $value = $aArgs[$sAttCode];
  1100. }
  1101. if (!empty($iId))
  1102. {
  1103. $iInputId = $iId;
  1104. }
  1105. else
  1106. {
  1107. $iInputId = $oPage->GetUniqueId();
  1108. }
  1109. if (!$oAttDef->IsExternalField())
  1110. {
  1111. $bMandatory = 'false';
  1112. if ( (!$oAttDef->IsNullAllowed()) || ($iFlags & OPT_ATT_MANDATORY))
  1113. {
  1114. $bMandatory = 'true';
  1115. }
  1116. $sValidationField = "<span class=\"form_validation\" id=\"v_{$iId}\"></span>";
  1117. $sHelpText = $oAttDef->GetHelpOnEdition();
  1118. $aEventsList = array();
  1119. switch($oAttDef->GetEditClass())
  1120. {
  1121. case 'Date':
  1122. case 'DateTime':
  1123. $aEventsList[] ='validate';
  1124. $aEventsList[] ='keyup';
  1125. $aEventsList[] ='change';
  1126. $sHTMLValue = "<input title=\"$sHelpText\" class=\"date-pick\" type=\"text\" size=\"20\" name=\"attr_{$sFieldPrefix}{$sAttCode}{$sNameSuffix}\" value=\"$value\" id=\"$iId\"/>&nbsp;{$sValidationField}";
  1127. break;
  1128. case 'Duration':
  1129. $aEventsList[] ='validate';
  1130. $aEventsList[] ='change';
  1131. $oPage->add_ready_script("$('#{$iId}_d').bind('keyup change', function(evt, sFormId) { return UpdateDuration('$iId'); });");
  1132. $oPage->add_ready_script("$('#{$iId}_h').bind('keyup change', function(evt, sFormId) { return UpdateDuration('$iId'); });");
  1133. $oPage->add_ready_script("$('#{$iId}_m').bind('keyup change', function(evt, sFormId) { return UpdateDuration('$iId'); });");
  1134. $oPage->add_ready_script("$('#{$iId}_s').bind('keyup change', function(evt, sFormId) { return UpdateDuration('$iId'); });");
  1135. $aVal = AttributeDuration::SplitDuration($value);
  1136. $sDays = "<input title=\"$sHelpText\" type=\"text\" size=\"3\" name=\"attr_{$sFieldPrefix}{$sAttCode}[d]{$sNameSuffix}\" value=\"{$aVal['days']}\" id=\"{$iId}_d\"/>";
  1137. $sHours = "<input title=\"$sHelpText\" type=\"text\" size=\"2\" name=\"attr_{$sFieldPrefix}{$sAttCode}[h]{$sNameSuffix}\" value=\"{$aVal['hours']}\" id=\"{$iId}_h\"/>";
  1138. $sMinutes = "<input title=\"$sHelpText\" type=\"text\" size=\"2\" name=\"attr_{$sFieldPrefix}{$sAttCode}[m]{$sNameSuffix}\" value=\"{$aVal['minutes']}\" id=\"{$iId}_m\"/>";
  1139. $sSeconds = "<input title=\"$sHelpText\" type=\"text\" size=\"2\" name=\"attr_{$sFieldPrefix}{$sAttCode}[s]{$sNameSuffix}\" value=\"{$aVal['seconds']}\" id=\"{$iId}_s\"/>";
  1140. $sHidden = "<input type=\"hidden\" id=\"{$iId}\" value=\"$value\"/>";
  1141. $sHTMLValue = Dict::Format('UI:DurationForm_Days_Hours_Minutes_Seconds', $sDays, $sHours, $sMinutes, $sSeconds).$sHidden."&nbsp;".$sValidationField;
  1142. break;
  1143. case 'Password':
  1144. $aEventsList[] ='validate';
  1145. $aEventsList[] ='keyup';
  1146. $aEventsList[] ='change';
  1147. $sHTMLValue = "<input title=\"$sHelpText\" type=\"password\" size=\"30\" name=\"attr_{$sFieldPrefix}{$sAttCode}{$sNameSuffix}\" value=\"$value\" id=\"$iId\"/>&nbsp;{$sValidationField}";
  1148. break;
  1149. case 'Text':
  1150. $aEventsList[] ='validate';
  1151. $aEventsList[] ='keyup';
  1152. $aEventsList[] ='change';
  1153. $sEditValue = $oAttDef->GetEditValue($value);
  1154. $sHTMLValue = "<table><tr><td><textarea class=\"resizable\" title=\"$sHelpText\" name=\"attr_{$sFieldPrefix}{$sAttCode}{$sNameSuffix}\" rows=\"8\" cols=\"40\" id=\"$iId\">$sEditValue</textarea></td><td>{$sValidationField}</td></tr></table>";
  1155. break;
  1156. case 'HTML':
  1157. $oWidget = new UIHTMLEditorWidget($iId, $sAttCode, $sNameSuffix, $sFieldPrefix, $sHelpText, $sValidationField, $value, $bMandatory);
  1158. $sHTMLValue = $oWidget->Display($oPage, $aArgs);
  1159. break;
  1160. case 'LinkedSet':
  1161. $aEventsList[] ='validate';
  1162. $aEventsList[] ='change';
  1163. $oWidget = new UILinksWidget($sClass, $sAttCode, $iId, $sNameSuffix, $oAttDef->DuplicatesAllowed());
  1164. $sHTMLValue = $oWidget->Display($oPage, $value);
  1165. break;
  1166. case 'Document':
  1167. $aEventsList[] ='validate';
  1168. $aEventsList[] ='change';
  1169. $oDocument = $value; // Value is an ormDocument object
  1170. $sFileName = '';
  1171. if (is_object($oDocument))
  1172. {
  1173. $sFileName = $oDocument->GetFileName();
  1174. }
  1175. $iMaxFileSize = utils::ConvertToBytes(ini_get('upload_max_filesize'));
  1176. $sHTMLValue = "<input type=\"hidden\" name=\"MAX_FILE_SIZE\" value=\"$iMaxFileSize\" />\n";
  1177. $sHTMLValue .= "<input name=\"attr_{$sFieldPrefix}{$sAttCode}{$sNameSuffix}\" type=\"hidden\" id=\"$iId\" \" value=\"$sFileName\"/>\n";
  1178. $sHTMLValue .= "<span id=\"name_$iInputId\">$sFileName</span><br/>\n";
  1179. $sHTMLValue .= "<input title=\"$sHelpText\" name=\"file_{$sFieldPrefix}{$sAttCode}{$sNameSuffix}\" type=\"file\" id=\"file_$iId\" onChange=\"UpdateFileName('$iId', this.value)\"/>&nbsp;{$sValidationField}\n";
  1180. break;
  1181. case 'List':
  1182. // Not editable for now...
  1183. $sHTMLValue = '';
  1184. break;
  1185. case 'One Way Password':
  1186. $aEventsList[] ='validate';
  1187. $oWidget = new UIPasswordWidget($sAttCode, $iId, $sNameSuffix);
  1188. $sHTMLValue = $oWidget->Display($oPage, $aArgs);
  1189. // Event list & validation is handled directly by the widget
  1190. break;
  1191. case 'ExtKey':
  1192. $aEventsList[] ='validate';
  1193. $aEventsList[] ='change';
  1194. $aAllowedValues = MetaModel::GetAllowedValues_att($sClass, $sAttCode, $aArgs);
  1195. $iFieldSize = $oAttDef->GetMaxSize();
  1196. $iMaxComboLength = $oAttDef->GetMaximumComboLength();
  1197. $oWidget = new UIExtKeyWidget($sAttCode, $sClass, $oAttDef->GetLabel(), $aAllowedValues, $value, $iId, $bMandatory, $sNameSuffix, $sFieldPrefix, $sFormPrefix);
  1198. $sHTMLValue = $oWidget->Display($oPage, $aArgs);
  1199. break;
  1200. case 'String':
  1201. default:
  1202. $aEventsList[] ='validate';
  1203. // #@# todo - add context information (depending on dimensions)
  1204. $aAllowedValues = MetaModel::GetAllowedValues_att($sClass, $sAttCode, $aArgs);
  1205. $iFieldSize = $oAttDef->GetMaxSize();
  1206. if ($aAllowedValues !== null)
  1207. {
  1208. // Discrete list of values, use a SELECT
  1209. $sHTMLValue = "<select title=\"$sHelpText\" name=\"attr_{$sFieldPrefix}{$sAttCode}{$sNameSuffix}\" id=\"$iId\">\n";
  1210. $sHTMLValue .= "<option value=\"\">".Dict::S('UI:SelectOne')."</option>\n";
  1211. foreach($aAllowedValues as $key => $display_value)
  1212. {
  1213. if ((count($aAllowedValues) == 1) && ($bMandatory == 'true') )
  1214. {
  1215. // When there is only once choice, select it by default
  1216. $sSelected = ' selected';
  1217. }
  1218. else
  1219. {
  1220. $sSelected = ($value == $key) ? ' selected' : '';
  1221. }
  1222. $sHTMLValue .= "<option value=\"$key\"$sSelected>$display_value</option>\n";
  1223. }
  1224. $sHTMLValue .= "</select>&nbsp;{$sValidationField}\n";
  1225. $aEventsList[] ='change';
  1226. }
  1227. else
  1228. {
  1229. $sHTMLValue = "<input title=\"$sHelpText\" type=\"text\" size=\"30\" maxlength=\"$iFieldSize\" name=\"attr_{$sFieldPrefix}{$sAttCode}{$sNameSuffix}\" value=\"$value\" id=\"$iId\"/>&nbsp;{$sValidationField}";
  1230. $aEventsList[] ='keyup';
  1231. $aEventsList[] ='change';
  1232. }
  1233. break;
  1234. }
  1235. $sPattern = addslashes($oAttDef->GetValidationPattern()); //'^([0-9]+)$';
  1236. if (!empty($aEventsList))
  1237. {
  1238. $sNullValue = $oAttDef->GetNullValue();
  1239. if (!is_numeric($sNullValue))
  1240. {
  1241. $sNullValue = "'$sNullValue'"; // Add quotes to turn this into a JS string if it's not a number
  1242. }
  1243. $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
  1244. }
  1245. $aDependencies = MetaModel::GetDependentAttributes($sClass, $sAttCode); // List of attributes that depend on the current one
  1246. if (count($aDependencies) > 0)
  1247. {
  1248. $oPage->add_ready_script("$('#$iId').bind('change', function(evt, sFormId) { return oWizardHelper{$sFormPrefix}.UpdateDependentFields(['".implode("','", $aDependencies)."']) } );\n"); // Bind to a custom event: validate
  1249. }
  1250. }
  1251. return "<div>{$sHTMLValue}</div>";
  1252. }
  1253. public function DisplayModifyForm(WebPage $oPage, $aExtraParams = array())
  1254. {
  1255. static $iGlobalFormId = 1;
  1256. $iGlobalFormId++;
  1257. $sPrefix = '';
  1258. if (isset($aExtraParams['formPrefix']))
  1259. {
  1260. $sPrefix = $aExtraParams['formPrefix'];
  1261. }
  1262. $this->m_iFormId = $sPrefix.$iGlobalFormId;
  1263. $sClass = get_class($this);
  1264. $oAppContext = new ApplicationContext();
  1265. $sStateAttCode = MetaModel::GetStateAttributeCode($sClass);
  1266. $iKey = $this->GetKey();
  1267. $aDetails = array();
  1268. $aFieldsMap = array();
  1269. if (!isset($aExtraParams['action']))
  1270. {
  1271. $sFormAction = $_SERVER['SCRIPT_NAME']; // No parameter in the URL, the only parameter will be the ones passed through the form
  1272. }
  1273. else
  1274. {
  1275. $sFormAction = $aExtraParams['action'];
  1276. }
  1277. $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");
  1278. $oPage->AddTabContainer(OBJECT_PROPERTIES_TAB, $sPrefix);
  1279. $oPage->SetCurrentTabContainer(OBJECT_PROPERTIES_TAB);
  1280. $oPage->SetCurrentTab(Dict::S('UI:PropertiesTab'));
  1281. // $aDetailsList = $this->FLattenZList(MetaModel::GetZListItems($sClass, 'details'));
  1282. //$aFullList = MetaModel::ListAttributeDefs($sClass);
  1283. $aList = array();
  1284. // Compute the list of properties to display, first the attributes in the 'details' list, then
  1285. // all the remaining attributes that are not external fields
  1286. // foreach($aDetailsList as $sAttCode)
  1287. // {
  1288. // $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  1289. // if (!$oAttDef->IsExternalField())
  1290. // {
  1291. // $aList[] = $sAttCode;
  1292. // }
  1293. // }
  1294. $aDetailsList = MetaModel::GetZListItems($sClass, 'details');
  1295. $aDetailsStruct = self::ProcessZlist($aDetailsList, array('UI:PropertiesTab' => array()), 'UI:PropertiesTab', 'col1', '');
  1296. $sHtml = '';
  1297. $aDetails = array();
  1298. foreach($aDetailsStruct as $sTab => $aCols )
  1299. {
  1300. $aDetails[$sTab] = array();
  1301. ksort($aCols);
  1302. $oPage->SetCurrentTab(Dict::S($sTab));
  1303. $oPage->add('<table style="vertical-align:top"><tr>');
  1304. foreach($aCols as $sColIndex => $aFieldsets)
  1305. {
  1306. $sLabel = '';
  1307. $sPreviousLabel = '';
  1308. $aDetails[$sTab][$sColIndex] = array();
  1309. $oPage->add('<td style="vertical-align:top">');
  1310. //$aDetails[$sTab][$sColIndex] = array();
  1311. foreach($aFieldsets as $sFieldsetName => $aFields)
  1312. {
  1313. if (!empty($sFieldsetName) && ($sFieldsetName[0]!='_'))
  1314. {
  1315. $sLabel = $sFieldsetName;
  1316. }
  1317. else
  1318. {
  1319. $sLabel = '';
  1320. }
  1321. if ($sLabel != $sPreviousLabel)
  1322. {
  1323. if (!empty($sPreviousLabel))
  1324. {
  1325. $oPage->add('<fieldset>');
  1326. $oPage->add('<legend>'.Dict::S($sPreviousLabel).'</legend>');
  1327. }
  1328. $oPage->Details($aDetails[$sTab][$sColIndex]);
  1329. if (!empty($sPreviousLabel))
  1330. {
  1331. $oPage->add('</fieldset>');
  1332. }
  1333. $aDetails[$sTab][$sColIndex] = array();
  1334. $sPreviousLabel = $sLabel;
  1335. }
  1336. foreach($aFields as $sAttCode)
  1337. {
  1338. $aVal = null;
  1339. $iFlags = $this->GetAttributeFlags($sAttCode);
  1340. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  1341. if ( (!$oAttDef->IsLinkSet()) && (($iFlags & OPT_ATT_HIDDEN) == 0))
  1342. {
  1343. if ($oAttDef->IsWritable())
  1344. {
  1345. if ($sStateAttCode == $sAttCode)
  1346. {
  1347. // State attribute is always read-only from the UI
  1348. $sHTMLValue = $this->GetStateLabel();
  1349. $aVal = array('label' => $oAttDef->GetLabel(), 'value' => $sHTMLValue);
  1350. }
  1351. else
  1352. {
  1353. $sInputId = $this->m_iFormId.'_'.$sAttCode;
  1354. if ($iFlags & OPT_ATT_HIDDEN)
  1355. {
  1356. // Attribute is hidden, add a hidden input
  1357. $oPage->add('<input type="hidden" id="'.$sInputId.'" name="attr_'.$sPrefix.$sAttCode.'" value="'.htmlentities($this->Get($sAttCode), ENT_QUOTES, 'UTF-8').'"/>');
  1358. $aFieldsMap[$sAttCode] = $sInputId;
  1359. }
  1360. else
  1361. {
  1362. if ($iFlags & OPT_ATT_READONLY)
  1363. {
  1364. // Check if the attribute is not read-only becuase of a synchro...
  1365. $aReasons = array();
  1366. $iSynchroFlags = $this->GetSynchroReplicaFlags($sAttCode, $aReasons);
  1367. $sSynchroIcon = '';
  1368. if ($iSynchroFlags & OPT_ATT_READONLY)
  1369. {
  1370. $sSynchroIcon = "&nbsp;<img id=\"synchro_$sInputId\" src=\"../images/transp-lock.png\" style=\"vertical-align:middle\"/>";
  1371. $sTip = '';
  1372. foreach($aReasons as $aRow)
  1373. {
  1374. $sTip .= "<p>Synchronized with {$aRow['name']} - {$aRow['description']}</p>";
  1375. }
  1376. $oPage->add_ready_script("$('#synchro_$sInputId').qtip( { content: '$sTip', show: 'mouseover', hide: 'mouseout', style: { name: 'dark', tip: 'leftTop' }, position: { corner: { target: 'rightMiddle', tooltip: 'leftTop' }} } );");
  1377. }
  1378. // Attribute is read-only
  1379. $sHTMLValue = $this->GetAsHTML($sAttCode).$sSynchroIcon;
  1380. $sHTMLValue .= '<input type="hidden" id="'.$sInputId.'" name="attr_'.$sPrefix.$sAttCode.'" value="'.htmlentities($this->Get($sAttCode), ENT_QUOTES, 'UTF-8').'"/>';
  1381. $aFieldsMap[$sAttCode] = $sInputId;
  1382. }
  1383. else
  1384. {
  1385. $sValue = $this->Get($sAttCode);
  1386. $sDisplayValue = $this->GetEditValue($sAttCode);
  1387. $aArgs = array('this' => $this, 'formPrefix' => $sPrefix);
  1388. $sHTMLValue = "<span id=\"field_{$sInputId}\">".self::GetFormElementForField($oPage, $sClass, $sAttCode, $oAttDef, $sValue, $sDisplayValue, $sInputId, '', $iFlags, $aArgs).'</span>';
  1389. $aFieldsMap[$sAttCode] = $sInputId;
  1390. }
  1391. $aVal = array('label' => '<span title="'.$oAttDef->GetDescription().'">'.$oAttDef->GetLabel().'</span>', 'value' => $sHTMLValue);
  1392. }
  1393. }
  1394. }
  1395. else
  1396. {
  1397. $aVal = array('label' => '<span title="'.$oAttDef->GetDescription().'">'.$oAttDef->GetLabel().'</span>', 'value' => $this->GetAsHTML($sAttCode));
  1398. }
  1399. }
  1400. if ($aVal != null)
  1401. {
  1402. // The field is visible, add it to the current column
  1403. $aDetails[$sTab][$sColIndex][] = $aVal;
  1404. }
  1405. }
  1406. }
  1407. if (!empty($sPreviousLabel))
  1408. {
  1409. $oPage->add('<fieldset>');
  1410. $oPage->add('<legend>'.Dict::S($sPreviousLabel).'</legend>');
  1411. }
  1412. $oPage->Details($aDetails[$sTab][$sColIndex]);
  1413. if (!empty($sPreviousLabel))
  1414. {
  1415. $oPage->add('</fieldset>');
  1416. }
  1417. $oPage->add('</td>');
  1418. }
  1419. $oPage->add('</tr></table>');
  1420. }
  1421. // Now display the relations, one tab per relation
  1422. if (!isset($aExtraParams['noRelations']))
  1423. {
  1424. $this->DisplayBareRelations($oPage, true); // Edit mode
  1425. }
  1426. $oPage->SetCurrentTab('');
  1427. $oPage->add("<input type=\"hidden\" name=\"class\" value=\"$sClass\">\n");
  1428. $oPage->add("<input type=\"hidden\" name=\"transaction_id\" value=\"".utils::GetNewTransactionId()."\">\n");
  1429. foreach($aExtraParams as $sName => $value)
  1430. {
  1431. $oPage->add("<input type=\"hidden\" name=\"$sName\" value=\"$value\">\n");
  1432. }
  1433. $oPage->add($oAppContext->GetForForm());
  1434. if ($iKey > 0)
  1435. {
  1436. // The object already exists in the database, it's modification
  1437. $oPage->add("<input type=\"hidden\" name=\"id\" value=\"$iKey\">\n");
  1438. $oPage->add("<input type=\"hidden\" name=\"operation\" value=\"apply_modify\">\n");
  1439. // $oPage->add("<button type=\"button\" id=\"btn_cancel_{$sPrefix}\" class=\"action\" onClick=\"BackToDetails('$sClass', $iKey)\"><span>".Dict::S('UI:Button:Cancel')."</span></button>&nbsp;&nbsp;&nbsp;&nbsp;\n");
  1440. $oPage->add("<button type=\"button\" class=\"action cancel\"><span>".Dict::S('UI:Button:Cancel')."</span></button>&nbsp;&nbsp;&nbsp;&nbsp;\n");
  1441. $oPage->add("<button type=\"submit\" class=\"action\"><span>".Dict::S('UI:Button:Apply')."</span></button>\n");
  1442. }
  1443. else
  1444. {
  1445. // The object does not exist in the database it's a creation
  1446. $oPage->add("<input type=\"hidden\" name=\"operation\" value=\"apply_new\">\n");
  1447. // $oPage->add("<button type=\"button\" id=\"btn_cancel_{$sPrefix}\" class=\"action\" onClick=\"BackToDetails('$sClass', $iKey)\"><span>".Dict::S('UI:Button:Cancel')."</span></button>&nbsp;&nbsp;&nbsp;&nbsp;\n");
  1448. $oPage->add("<button type=\"button\" class=\"action cancel\">".Dict::S('UI:Button:Cancel')."</button>&nbsp;&nbsp;&nbsp;&nbsp;\n");
  1449. $oPage->add("<button type=\"submit\" class=\"action\"><span>".Dict::S('UI:Button:Create')."</span></button>\n");
  1450. }
  1451. // Hook the cancel button via jQuery so that it can be unhooked easily as well if needed
  1452. $sDefaultUrl = '../pages/UI.php?operation=cancel';
  1453. $oPage->add_ready_script("$('#form_{$this->m_iFormId} button.cancel').click( function() { BackToDetails('$sClass', $iKey, '$sDefaultUrl')} );");
  1454. $oPage->add("</form>\n");
  1455. $iFieldsCount = count($aFieldsMap);
  1456. $sJsonFieldsMap = json_encode($aFieldsMap);
  1457. $oPage->add_script(
  1458. <<<EOF
  1459. // Create the object once at the beginning of the page...
  1460. var oWizardHelper$sPrefix = new WizardHelper('$sClass', '$sPrefix');
  1461. oWizardHelper$sPrefix.SetFieldsMap($sJsonFieldsMap);
  1462. oWizardHelper$sPrefix.SetFieldsCount($iFieldsCount);
  1463. EOF
  1464. );
  1465. $oPage->add_ready_script(
  1466. <<<EOF
  1467. // Starts the validation when the page is ready
  1468. CheckFields('form_{$this->m_iFormId}', false);
  1469. EOF
  1470. );
  1471. }
  1472. public static function DisplayCreationForm(WebPage $oPage, $sClass, $oObjectToClone = null, $aArgs = array(), $aExtraParams = array())
  1473. {
  1474. $oAppContext = new ApplicationContext();
  1475. $sClass = ($oObjectToClone == null) ? $sClass : get_class($oObjectToClone);
  1476. $sStateAttCode = MetaModel::GetStateAttributeCode($sClass);
  1477. $aStates = MetaModel::EnumStates($sClass);
  1478. if ($oObjectToClone == null)
  1479. {
  1480. $oObj = MetaModel::NewObject($sClass);
  1481. if (!empty($sStateAttCode))
  1482. {
  1483. $sTargetState = MetaModel::GetDefaultState($sClass);
  1484. $oObj->Set($sStateAttCode, $sTargetState);
  1485. }
  1486. }
  1487. else
  1488. {
  1489. $oObj = clone $oObjectToClone;
  1490. }
  1491. // Pre-fill the object with default values, when there is only on possible choice
  1492. // AND the field is mandatory (otherwise there is always the possiblity to let it empty)
  1493. $aArgs['this'] = $oObj;
  1494. $aDetailsList = self::FLattenZList(MetaModel::GetZListItems($sClass, 'details'));
  1495. // Order the fields based on their dependencies
  1496. $aDeps = array();
  1497. foreach($aDetailsList as $sAttCode)
  1498. {
  1499. $aDeps[$sAttCode] = MetaModel::GetPrequisiteAttributes($sClass, $sAttCode);
  1500. }
  1501. $aList = self::OrderDependentFields($aDeps);
  1502. // Now fill-in the fields with default/supplied values
  1503. foreach($aList as $sAttCode)
  1504. {
  1505. $aAllowedValues = MetaModel::GetAllowedValues_att($sClass, $sAttCode, $aArgs);
  1506. if (isset($aArgs['default'][$sAttCode]))
  1507. {
  1508. $oObj->Set($sAttCode, $aArgs['default'][$sAttCode]);
  1509. }
  1510. elseif (count($aAllowedValues) == 1)
  1511. {
  1512. // If the field is mandatory, set it to the only possible value
  1513. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  1514. $iFlags = $oObj->GetAttributeFlags($sAttCode);
  1515. if ( (!$oAttDef->IsNullAllowed()) || ($iFlags & OPT_ATT_MANDATORY))
  1516. {
  1517. $aValues = array_keys($aAllowedValues);
  1518. $oObj->Set($sAttCode, $aValues[0]);
  1519. }
  1520. }
  1521. }
  1522. return $oObj->DisplayModifyForm( $oPage, $aExtraParams);
  1523. }
  1524. protected static function ProcessZlist($aList, $aDetails, $sCurrentTab, $sCurrentCol, $sCurrentSet)
  1525. {
  1526. //echo "<pre>ZList: ";
  1527. //print_r($aList);
  1528. //echo "</pre>\n";
  1529. $index = 0;
  1530. foreach($aList as $sKey => $value)
  1531. {
  1532. if (is_array($value))
  1533. {
  1534. if (preg_match('/^(.*):(.*)$/U', $sKey, $aMatches))
  1535. {
  1536. $sCode = $aMatches[1];
  1537. $sName = $aMatches[2];
  1538. switch($sCode)
  1539. {
  1540. case 'tab':
  1541. //echo "<p>Found a tab: $sName ($sKey)</p>\n";
  1542. if(!isset($aDetails[$sName]))
  1543. {
  1544. $aDetails[$sName] = array('col1' => array());
  1545. }
  1546. $aDetails = self::ProcessZlist($value, $aDetails, $sName, 'col1', '');
  1547. break;
  1548. case 'fieldset':
  1549. //echo "<p>Found a fieldset: $sName ($sKey)</p>\n";
  1550. if(!isset($aDetailsStruct[$sCurrentTab][$sCurrentCol][$sName]))
  1551. {
  1552. $aDetails[$sCurrentTab][$sCurrentCol][$sName] = array();
  1553. }
  1554. $aDetails = self::ProcessZlist($value, $aDetails, $sCurrentTab, $sCurrentCol, $sName);
  1555. break;
  1556. default:
  1557. case 'col':
  1558. //echo "<p>Found a column: $sName ($sKey)</p>\n";
  1559. if(!isset($aDetails[$sCurrentTab][$sName]))
  1560. {
  1561. $aDetails[$sCurrentTab][$sName] = array();
  1562. }
  1563. $aDetails = self::ProcessZlist($value, $aDetails, $sCurrentTab, $sName, '');
  1564. break;
  1565. }
  1566. }
  1567. }
  1568. else
  1569. {
  1570. //echo "<p>Scalar value: $value, in [$sCurrentTab][$sCurrentCol][$sCurrentSet][]</p>\n";
  1571. if (empty($sCurrentSet))
  1572. {
  1573. $aDetails[$sCurrentTab][$sCurrentCol]['_'.$index][] = $value;
  1574. }
  1575. else
  1576. {
  1577. $aDetails[$sCurrentTab][$sCurrentCol][$sCurrentSet][] = $value;
  1578. }
  1579. }
  1580. $index++;
  1581. }
  1582. return $aDetails;
  1583. }
  1584. protected static function FlattenZList($aList)
  1585. {
  1586. $aResult = array();
  1587. foreach($aList as $value)
  1588. {
  1589. if (!is_array($value))
  1590. {
  1591. $aResult[] = $value;
  1592. }
  1593. else
  1594. {
  1595. $aResult = array_merge($aResult,self::FlattenZList($value));
  1596. }
  1597. }
  1598. return $aResult;
  1599. }
  1600. protected function GetFieldAsHtml($sClass, $sAttCode, $sStateAttCode)
  1601. {
  1602. $retVal = null;
  1603. $iFlags = $this->GetAttributeFlags($sAttCode);
  1604. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  1605. if ( (!$oAttDef->IsLinkSet()) && (($iFlags & OPT_ATT_HIDDEN) == 0) )
  1606. {
  1607. // The field is visible in the current state of the object
  1608. if ($sStateAttCode == $sAttCode)
  1609. {
  1610. // Special display for the 'state' attribute itself
  1611. $sDisplayValue = $this->GetStateLabel();
  1612. }
  1613. else if ($oAttDef->GetEditClass() == 'Document')
  1614. {
  1615. $oDocument = $this->Get($sAttCode);
  1616. $sDisplayValue = $this->GetAsHTML($sAttCode);
  1617. $sDisplayValue .= "<br/>".Dict::Format('UI:OpenDocumentInNewWindow_', $oDocument->GetDisplayLink(get_class($this), $this->GetKey(), $sAttCode)).", \n";
  1618. $sDisplayValue .= "<br/>".Dict::Format('UI:DownloadDocument_', $oDocument->GetDisplayLink(get_class($this), $this->GetKey(), $sAttCode)).", \n";
  1619. }
  1620. else
  1621. {
  1622. $sDisplayValue = $this->GetAsHTML($sAttCode);
  1623. }
  1624. $retVal = array('label' => '<span title="'.MetaModel::GetDescription($sClass, $sAttCode).'">'.MetaModel::GetLabel($sClass, $sAttCode).'</span>', 'value' => $sDisplayValue);
  1625. }
  1626. return $retVal;
  1627. }
  1628. /**
  1629. * Displays a blob document *inline* (if possible, depending on the type of the document)
  1630. * @return string
  1631. */
  1632. public function DisplayDocumentInline(WebPage $oPage, $sAttCode)
  1633. {
  1634. $oDoc = $this->Get($sAttCode);
  1635. $sClass = get_class($this);
  1636. $Id = $this->GetKey();
  1637. switch ($oDoc->GetMainMimeType())
  1638. {
  1639. case 'text':
  1640. case 'html':
  1641. $data = $oDoc->GetData();
  1642. switch($oDoc->GetMimeType())
  1643. {
  1644. case 'text/html':
  1645. case 'text/xml':
  1646. $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");
  1647. break;
  1648. default:
  1649. $oPage->add("<pre>".htmlentities(MyHelpers::beautifulstr($data, 1000, true), ENT_QUOTES, 'UTF-8')."</pre>\n");
  1650. }
  1651. break;
  1652. case 'application':
  1653. switch($oDoc->GetMimeType())
  1654. {
  1655. case 'application/pdf':
  1656. $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");
  1657. break;
  1658. default:
  1659. $oPage->add(Dict::S('UI:Document:NoPreview'));
  1660. }
  1661. break;
  1662. case 'image':
  1663. $oPage->add("<img src=\"../pages/ajax.render.php?operation=display_document&class=$sClass&id=$Id&field=$sAttCode\" />\n");
  1664. break;
  1665. default:
  1666. $oPage->add(Dict::S('UI:Document:NoPreview'));
  1667. }
  1668. }
  1669. /**
  1670. * This function returns a 'hilight' CSS class, used to hilight a given row in a table
  1671. * There are currently (i.e defined in the CSS) 4 possible values HILIGHT_CLASS_CRITICAL,
  1672. * HILIGHT_CLASS_WARNING, HILIGHT_CLASS_OK, HILIGHT_CLASS_NONE
  1673. * To Be overridden by derived classes
  1674. * @param void
  1675. * @return String The desired higlight class for the object/row
  1676. */
  1677. public function GetHilightClass()
  1678. {
  1679. // Possible return values are:
  1680. // HILIGHT_CLASS_CRITICAL, HILIGHT_CLASS_WARNING, HILIGHT_CLASS_OK, HILIGHT_CLASS_NONE
  1681. return HILIGHT_CLASS_NONE; // Not hilighted by default
  1682. }
  1683. /**
  1684. * Re-order the fields based on their inter-dependencies
  1685. * @params hash @aFields field_code => array_of_depencies
  1686. * @return array Ordered array of fields or throws an exception
  1687. */
  1688. public static function OrderDependentFields($aFields)
  1689. {
  1690. $bCircular = false;
  1691. $aResult = array();
  1692. $iCount = 0;
  1693. do
  1694. {
  1695. $bSet = false;
  1696. $iCount++;
  1697. foreach($aFields as $sFieldCode => $aDeps)
  1698. {
  1699. foreach($aDeps as $key => $sDependency)
  1700. {
  1701. if (in_array($sDependency, $aResult))
  1702. {
  1703. // Dependency is resolved, remove it
  1704. unset($aFields[$sFieldCode][$key]);
  1705. }
  1706. }
  1707. if (count($aFields[$sFieldCode]) == 0)
  1708. {
  1709. // No more pending depencies for this field, add it to the list
  1710. $aResult[] = $sFieldCode;
  1711. unset($aFields[$sFieldCode]);
  1712. $bSet = true;
  1713. }
  1714. }
  1715. }
  1716. while($bSet && (count($aFields) > 0));
  1717. if (count($aFields) > 0)
  1718. {
  1719. $sMessage = "Error: Circular dependencies between the fields ! <pre>".print_r($aFields, true)."</pre>";
  1720. throw(new Exception($sMessage));
  1721. }
  1722. return $aResult;
  1723. }
  1724. /**
  1725. * Maps the given context parameter name to the appropriate filter/search code for this class
  1726. * @param string $sContextParam Name of the context parameter, i.e. 'org_id'
  1727. * @return string Filter code, i.e. 'customer_id'
  1728. */
  1729. public static function MapContextParam($sContextParam)
  1730. {
  1731. if ($sContextParam == 'menu')
  1732. {
  1733. return null;
  1734. }
  1735. else
  1736. {
  1737. return $sContextParam;
  1738. }
  1739. }
  1740. /**
  1741. * Updates the object from the POSTed parameters
  1742. */
  1743. public function UpdateObject($sFormPrefix = '')
  1744. {
  1745. foreach(MetaModel::ListAttributeDefs(get_class($this)) as $sAttCode=>$oAttDef)
  1746. {
  1747. if ($oAttDef->IsLinkSet() && $oAttDef->IsIndirect())
  1748. {
  1749. $aLinks = utils::ReadPostedParam("attr_{$sFormPrefix}{$sAttCode}", '');
  1750. $sLinkedClass = $oAttDef->GetLinkedClass();
  1751. $sExtKeyToRemote = $oAttDef->GetExtKeyToRemote();
  1752. $sExtKeyToMe = $oAttDef->GetExtKeyToMe();
  1753. $oLinkedSet = DBObjectSet::FromScratch($sLinkedClass);
  1754. if (is_array($aLinks))
  1755. {
  1756. foreach($aLinks as $id => $aData)
  1757. {
  1758. if (is_numeric($id))
  1759. {
  1760. if ($id < 0)
  1761. {
  1762. // New link to be created, the opposite of the id (-$id) is the ID of the remote object
  1763. $oLink = MetaModel::NewObject($sLinkedClass);
  1764. $oLink->Set($sExtKeyToRemote, -$id);
  1765. $oLink->Set($sExtKeyToMe, $this->GetKey());
  1766. }
  1767. else
  1768. {
  1769. // Existing link, potentially to be updated...
  1770. $oLink = MetaModel::GetObject($sLinkedClass, $id);
  1771. }
  1772. // Now populate the attributes
  1773. foreach($aData as $sName => $value)
  1774. {
  1775. if (MetaModel::IsValidAttCode($sLinkedClass, $sName))
  1776. {
  1777. $oLinkAttDef = MetaModel::GetAttributeDef($sLinkedClass, $sName);
  1778. if ($oLinkAttDef->IsWritable())
  1779. {
  1780. $oLink->Set($sName, $value);
  1781. }
  1782. }
  1783. }
  1784. $oLinkedSet->AddObject($oLink);
  1785. }
  1786. }
  1787. }
  1788. $this->Set($sAttCode, $oLinkedSet);
  1789. }
  1790. else if ($oAttDef->IsWritable())
  1791. {
  1792. $iFlags = $this->GetAttributeFlags($sAttCode);
  1793. if ($iFlags & (OPT_ATT_HIDDEN | OPT_ATT_READONLY))
  1794. {
  1795. // Non-visible, or read-only attribute, do nothing
  1796. }
  1797. elseif ($oAttDef->GetEditClass() == 'Document')
  1798. {
  1799. // There should be an uploaded file with the named attr_<attCode>
  1800. $oDocument = utils::ReadPostedDocument("file_{$sFormPrefix}{$sAttCode}");
  1801. if (!$oDocument->IsEmpty())
  1802. {
  1803. // A new file has been uploaded
  1804. $this->Set($sAttCode, $oDocument);
  1805. }
  1806. }
  1807. elseif ($oAttDef->GetEditClass() == 'One Way Password')
  1808. {
  1809. // Check if the password was typed/changed
  1810. $bChanged = utils::ReadPostedParam("attr_{$sFormPrefix}{$sAttCode}_changed", false);
  1811. if ($bChanged)
  1812. {
  1813. // The password has been changed or set
  1814. $rawValue = utils::ReadPostedParam("attr_{$sFormPrefix}{$sAttCode}", null);
  1815. $this->Set($sAttCode, $rawValue);
  1816. }
  1817. }
  1818. elseif ($oAttDef->GetEditClass() == 'Duration')
  1819. {
  1820. $rawValue = utils::ReadPostedParam("attr_{$sFormPrefix}{$sAttCode}", null);
  1821. if (!is_array($rawValue))
  1822. {
  1823. $iValue = null;
  1824. }
  1825. else
  1826. {
  1827. $iValue = (((24*$rawValue['d'])+$rawValue['h'])*60 +$rawValue['m'])*60 + $rawValue['s'];
  1828. }
  1829. $this->Set($sAttCode, $iValue);
  1830. $previousValue = $this->Get($sAttCode);
  1831. if ($previousValue !== $iValue)
  1832. {
  1833. $this->Set($sAttCode, $iValue);
  1834. }
  1835. }
  1836. else
  1837. {
  1838. $rawValue = utils::ReadPostedParam("attr_{$sFormPrefix}{$sAttCode}", null);
  1839. if (!is_null($rawValue))
  1840. {
  1841. $aAttributes[$sAttCode] = trim($rawValue);
  1842. $previousValue = $this->Get($sAttCode);
  1843. if ($previousValue !== $aAttributes[$sAttCode])
  1844. {
  1845. $this->Set($sAttCode, $aAttributes[$sAttCode]);
  1846. }
  1847. }
  1848. }
  1849. }
  1850. }
  1851. }
  1852. }
  1853. ?>