ui.linksdirectwidget.class.inc.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <?php
  2. // Copyright (C) 2010-2012 Combodo SARL
  3. //
  4. // This file is part of iTop.
  5. //
  6. // iTop is free software; you can redistribute it and/or modify
  7. // it under the terms of the GNU Affero General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // iTop is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Affero General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Affero General Public License
  17. // along with iTop. If not, see <http://www.gnu.org/licenses/>
  18. /**
  19. * Class UILinksWidgetDirect
  20. *
  21. * @copyright Copyright (C) 2010-2012 Combodo SARL
  22. * @license http://opensource.org/licenses/AGPL-3.0
  23. */
  24. class UILinksWidgetDirect
  25. {
  26. protected $sClass;
  27. protected $sAttCode;
  28. protected $sInputid;
  29. protected $sNameSuffix;
  30. protected $sLinkedClass;
  31. public function __construct($sClass, $sAttCode, $sInputId, $sNameSuffix = '')
  32. {
  33. $this->sClass = $sClass;
  34. $this->sAttCode = $sAttCode;
  35. $this->sInputid = $sInputId;
  36. $this->sNameSuffix = $sNameSuffix;
  37. $this->aZlist = array();
  38. $this->sLinkedClass = '';
  39. // Compute the list of attributes visible from the given objet:
  40. // All the attributes from the "list" Zlist of the Link class except
  41. // the ExternalKey that points to the current object and its related external fields
  42. $oLinksetDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  43. $this->sLinkedClass = $oLinksetDef->GetLinkedClass();
  44. $sExtKeyToMe = $oLinksetDef->GetExtKeyToMe();
  45. $aZList = MetaModel::FlattenZList(MetaModel::GetZListItems($this->sLinkedClass, 'list'));
  46. foreach($aZList as $sLinkedAttCode)
  47. {
  48. if ($sLinkedAttCode != $sExtKeyToMe)
  49. {
  50. $oAttDef = MetaModel::GetAttributeDef($this->sLinkedClass, $sLinkedAttCode);
  51. if (!$oAttDef->IsExternalField() || ($oAttDef->GetKeyAttCode() != $sExtKeyToMe) )
  52. {
  53. $this->aZlist[] = $sLinkedAttCode;
  54. }
  55. }
  56. }
  57. }
  58. public function Display(WebPage $oPage, DBObjectSet $oValue, $aArgs = array(), $sFormPrefix, $oCurrentObj)
  59. {
  60. $oLinksetDef = MetaModel::GetAttributeDef($this->sClass, $this->sAttCode);
  61. switch($oLinksetDef->GetEditMode())
  62. {
  63. case LINKSET_EDITMODE_NONE: // The linkset is read-only
  64. $this->DisplayAsBlock($oPage, $oValue, $aArgs = array(), $sFormPrefix, $oCurrentObj, false /* bDisplayMenu*/);
  65. break;
  66. case LINKSET_EDITMODE_ADDONLY: // The only possible action is to open (in a new window) the form to create a new object
  67. if ($oCurrentObj && !$oCurrentObj->IsNew())
  68. {
  69. $sTargetClass = $oLinksetDef->GetLinkedClass();
  70. $sExtKeyToMe = $oLinksetDef->GetExtKeyToMe();
  71. $sDefault = "default[$sExtKeyToMe]=".$oCurrentObj->GetKey();
  72. $oAppContext = new ApplicationContext();
  73. $sParams = $oAppContext->GetForLink();
  74. $oPage->p("<a target=\"_blank\" href=\"".utils::GetAbsoluteUrlAppRoot()."pages/UI.php?operation=new&class=$sTargetClass&$sParams{$sDefault}\">".Dict::Format('UI:ClickToCreateNew', Metamodel::GetName($sTargetClass))."</a>\n");
  75. }
  76. $this->DisplayAsBlock($oPage, $oValue, $aArgs = array(), $sFormPrefix, $oCurrentObj, false /* bDisplayMenu*/);
  77. break;
  78. case LINKSET_EDITMODE_INPLACE: // The whole linkset can be edited 'in-place'
  79. $this->DisplayEditInPlace($oPage, $oValue, $aArgs, $sFormPrefix, $oCurrentObj);
  80. break;
  81. case LINKSET_EDITMODE_ACTIONS:
  82. default:
  83. $this->DisplayAsBlock($oPage, $oValue, $aArgs = array(), $sFormPrefix, $oCurrentObj, true /* bDisplayMenu*/);
  84. }
  85. }
  86. protected function DisplayAsBlock(WebPage $oPage, DBObjectSet $oValue, $aArgs = array(), $sFormPrefix, $oCurrentObj, $bDisplayMenu)
  87. {
  88. $oLinksetDef = MetaModel::GetAttributeDef($this->sClass, $this->sAttCode);
  89. $sTargetClass = $oLinksetDef->GetLinkedClass();
  90. if ($oCurrentObj && $oCurrentObj->IsNew() && $bDisplayMenu)
  91. {
  92. $oPage->p(Dict::Format('UI:BeforeAdding_Class_ObjectsSaveThisObject', MetaModel::GetName($sTargetClass)));
  93. }
  94. else
  95. {
  96. $oFilter = new DBObjectSearch($sTargetClass);
  97. $oFilter->AddCondition($oLinksetDef->GetExtKeyToMe(), $oCurrentObj->GetKey(),'=');
  98. $aDefaults = array($oLinksetDef->GetExtKeyToMe() => $oCurrentObj->GetKey());
  99. $oAppContext = new ApplicationContext();
  100. foreach($oAppContext->GetNames() as $sKey)
  101. {
  102. // The linked object inherits the parent's value for the context
  103. if (MetaModel::IsValidAttCode($this->sClass, $sKey) && $oCurrentObj)
  104. {
  105. $aDefaults[$sKey] = $oCurrentObj->Get($sKey);
  106. }
  107. }
  108. $aParams = array(
  109. 'target_attr' => $oLinksetDef->GetExtKeyToMe(),
  110. 'object_id' => $oCurrentObj ? $oCurrentObj->GetKey() : null,
  111. 'menu' => $bDisplayMenu,
  112. 'default' => $aDefaults,
  113. 'table_id' => $this->sClass.'_'.$this->sAttCode,
  114. );
  115. $oBlock = new DisplayBlock($oFilter, 'list', false);
  116. $oBlock->Display($oPage, $this->sInputid, $aParams);
  117. }
  118. }
  119. protected function DisplayEditInPlace(WebPage $oPage, DBObjectSet $oValue, $aArgs = array(), $sFormPrefix, $oCurrentObj)
  120. {
  121. $aAttribs = $this->GetTableConfig();
  122. $oValue->Rewind();
  123. $oPage->add('<table class="listContainer" id="'.$this->sInputid.'"><tr><td>');
  124. $aData = array();
  125. while($oLinkObj = $oValue->Fetch())
  126. {
  127. $aRow = array();
  128. $aRow['form::select'] = '<input type="checkbox" class="selectList'.$this->sInputid.'" value="'.$oLinkObj->GetKey().'"/>';
  129. foreach($this->aZlist as $sLinkedAttCode)
  130. {
  131. $aRow[$sLinkedAttCode] = $oLinkObj->GetAsHTML($sLinkedAttCode);
  132. }
  133. $aData[] = $aRow;
  134. }
  135. $oPage->table($aAttribs, $aData);
  136. $oPage->add('</td></tr></table>'); //listcontainer
  137. $sInputName = $sFormPrefix.'attr_'.$this->sAttCode;
  138. $aLabels = array(
  139. 'delete' => Dict::S('UI:Button:Delete'),
  140. // 'modify' => 'Modify...' ,
  141. 'creation_title' => Dict::Format('UI:CreationTitle_Class', MetaModel::GetName($this->sLinkedClass)),
  142. 'create' => Dict::Format('UI:ClickToCreateNew', MetaModel::GetName($this->sLinkedClass)),
  143. );
  144. $sSubmitUrl = utils::GetAbsoluteUrlAppRoot().'pages/ajax.render.php';
  145. $sJSONLabels = json_encode($aLabels);
  146. $oPage->add_ready_script("$('#{$this->sInputid}').directlinks({class_name: '$this->sClass', att_code: '$this->sAttCode', input_name:'$sInputName', labels: $sJSONLabels, sumit_to: '$sSubmitUrl' });");
  147. }
  148. public function GetObjectCreationDlg(WebPage $oPage, $sProposedRealClass = '')
  149. {
  150. // For security reasons: check that the "proposed" class is actually a subclass of the linked class
  151. // and that the current user is allowed to create objects of this class
  152. $sRealClass = '';
  153. $oPage->add('<div class="wizContainer" style="vertical-align:top;"><div>');
  154. $aSubClasses = MetaModel::EnumChildClasses($this->sLinkedClass, ENUM_CHILD_CLASSES_ALL); // Including the specified class itself
  155. $aPossibleClasses = array();
  156. foreach($aSubClasses as $sCandidateClass)
  157. {
  158. if (!MetaModel::IsAbstract($sCandidateClass) && (UserRights::IsActionAllowed($sCandidateClass, UR_ACTION_MODIFY) == UR_ALLOWED_YES))
  159. {
  160. if ($sCandidateClass == $sProposedRealClass)
  161. {
  162. $sRealClass = $sProposedRealClass;
  163. }
  164. $aPossibleClasses[$sCandidateClass] = MetaModel::GetName($sCandidateClass);
  165. }
  166. }
  167. // Only one of the subclasses can be instantiated...
  168. if (count($aPossibleClasses) == 1)
  169. {
  170. $aKeys = array_keys($aPossibleClasses);
  171. $sRealClass = $aKeys[0];
  172. }
  173. if ($sRealClass != '')
  174. {
  175. $oPage->add("<h1>".MetaModel::GetClassIcon($sRealClass)."&nbsp;".Dict::Format('UI:CreationTitle_Class', MetaModel::GetName($sRealClass))."</h1>\n");
  176. $oLinksetDef = MetaModel::GetAttributeDef($this->sClass, $this->sAttCode);
  177. $sExtKeyToMe = $oLinksetDef->GetExtKeyToMe();
  178. $aFieldFlags = array( $sExtKeyToMe => OPT_ATT_HIDDEN);
  179. cmdbAbstractObject::DisplayCreationForm($oPage, $sRealClass, null, array(), array('formPrefix' => $this->sInputid, 'noRelations' => true, 'fieldsFlags' => $aFieldFlags));
  180. }
  181. else
  182. {
  183. $sClassLabel = MetaModel::GetName($this->sLinkedClass);
  184. $oPage->add('<p>'.Dict::Format('UI:SelectTheTypeOf_Class_ToCreate', $sClassLabel));
  185. $oPage->add('<nobr><select name="class">');
  186. asort($aPossibleClasses);
  187. foreach($aPossibleClasses as $sClassName => $sClassLabel)
  188. {
  189. $oPage->add("<option value=\"$sClassName\">$sClassLabel</option>");
  190. }
  191. $oPage->add('</select>');
  192. $oPage->add('&nbsp; <button type="button" onclick="$(\'#'.$this->sInputid.'\').directlinks(\'subclassSelected\');">'.Dict::S('UI:Button:Apply').'</button><span class="indicator" style="display:inline-block;width:16px"></span></nobr></p>');
  193. }
  194. $oPage->add('</div></div>');
  195. }
  196. public function GetObjectModificationDlg()
  197. {
  198. }
  199. protected function GetTableConfig()
  200. {
  201. $aAttribs = array();
  202. $aAttribs['form::select'] = array('label' => "<input type=\"checkbox\" onClick=\"CheckAll('.selectList{$this->sInputid}:not(:disabled)', this.checked);\" class=\"checkAll\"></input>", 'description' => Dict::S('UI:SelectAllToggle+'));
  203. foreach($this->aZlist as $sLinkedAttCode)
  204. {
  205. $oAttDef = MetaModel::GetAttributeDef($this->sLinkedClass, $sLinkedAttCode);
  206. $aAttribs[$sLinkedAttCode] = array('label' => MetaModel::GetLabel($this->sLinkedClass, $sLinkedAttCode), 'description' => $oAttDef->GetOrderByHint());
  207. }
  208. return $aAttribs;
  209. }
  210. public function GetRow($oPage, $sRealClass, $aValues, $iTempId)
  211. {
  212. $aAttribs = $this->GetTableConfig();
  213. if ($sRealClass == '')
  214. {
  215. $sRealClass = $this->sLinkedClass;
  216. }
  217. $oLinkObj = new $sRealClass();
  218. $oLinkObj->UpdateObjectFromPostedForm($this->sInputid);
  219. $aRow = array();
  220. $aRow['form::select'] = '<input type="checkbox" class="selectList'.$this->sInputid.'" value="'.(-$iTempId).'"/>';
  221. foreach($this->aZlist as $sLinkedAttCode)
  222. {
  223. $aRow[$sLinkedAttCode] = $oLinkObj->GetAsHTML($sLinkedAttCode);
  224. }
  225. return $oPage->GetTableRow($aRow, $aAttribs);
  226. }
  227. public function UpdateFromArray($oObj, $aData)
  228. {
  229. }
  230. }