ui.linksdirectwidget.class.inc.php 9.6 KB

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