ui.linksdirectwidget.class.inc.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. $oPage->add_ready_script("$('#{$this->sInputid}').directlinks({class_name: '$this->sClass', att_code: '$this->sAttCode', input_name:'$sInputName' });");
  131. }
  132. public function GetObjectCreationDlg(WebPage $oPage, $sProposedRealClass = '')
  133. {
  134. // For security reasons: check that the "proposed" class is actually a subclass of the linked class
  135. // and that the current user is allowed to create objects of this class
  136. $sRealClass = '';
  137. $oPage->add('<div class="wizContainer" style="vertical-align:top;"><div>');
  138. $aSubClasses = MetaModel::EnumChildClasses($this->sLinkedClass, ENUM_CHILD_CLASSES_ALL); // Including the specified class itself
  139. $aPossibleClasses = array();
  140. foreach($aSubClasses as $sCandidateClass)
  141. {
  142. if (!MetaModel::IsAbstract($sCandidateClass) && (UserRights::IsActionAllowed($sCandidateClass, UR_ACTION_MODIFY) == UR_ALLOWED_YES))
  143. {
  144. if ($sCandidateClass == $sProposedRealClass)
  145. {
  146. $sRealClass = $sProposedRealClass;
  147. }
  148. $aPossibleClasses[$sCandidateClass] = MetaModel::GetName($sCandidateClass);
  149. }
  150. }
  151. // Only one of the subclasses can be instantiated...
  152. if (count($aPossibleClasses) == 1)
  153. {
  154. $aKeys = array_keys($aPossibleClasses);
  155. $sRealClass = $aKeys[0];
  156. }
  157. if ($sRealClass != '')
  158. {
  159. $oPage->add("<h1>".MetaModel::GetClassIcon($sRealClass)."&nbsp;".Dict::Format('UI:CreationTitle_Class', MetaModel::GetName($sRealClass))."</h1>\n");
  160. $oLinksetDef = MetaModel::GetAttributeDef($this->sClass, $this->sAttCode);
  161. $sExtKeyToMe = $oLinksetDef->GetExtKeyToMe();
  162. $aFieldFlags = array( $sExtKeyToMe => OPT_ATT_HIDDEN);
  163. cmdbAbstractObject::DisplayCreationForm($oPage, $sRealClass, null, array(), array('formPrefix' => $this->sInputid, 'noRelations' => true, 'fieldsFlags' => $aFieldFlags));
  164. }
  165. else
  166. {
  167. $sClassLabel = MetaModel::GetName($this->sLinkedClass);
  168. $oPage->add('<p>'.Dict::Format('UI:SelectTheTypeOf_Class_ToCreate', $sClassLabel));
  169. $oPage->add('<nobr><select name="class">');
  170. asort($aPossibleClasses);
  171. foreach($aPossibleClasses as $sClassName => $sClassLabel)
  172. {
  173. $oPage->add("<option value=\"$sClassName\">$sClassLabel</option>");
  174. }
  175. $oPage->add('</select>');
  176. $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>');
  177. }
  178. $oPage->add('</div></div>');
  179. }
  180. public function GetObjectModificationDlg()
  181. {
  182. }
  183. protected function GetTableConfig()
  184. {
  185. $aAttribs = array();
  186. $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+'));
  187. foreach($this->aZlist as $sLinkedAttCode)
  188. {
  189. $oAttDef = MetaModel::GetAttributeDef($this->sLinkedClass, $sLinkedAttCode);
  190. $aAttribs[$sLinkedAttCode] = array('label' => MetaModel::GetLabel($this->sLinkedClass, $sLinkedAttCode), 'description' => $oAttDef->GetOrderByHint());
  191. }
  192. return $aAttribs;
  193. }
  194. public function GetRow($oPage, $sRealClass, $aValues, $iTempId)
  195. {
  196. $aAttribs = $this->GetTableConfig();
  197. if ($sRealClass == '')
  198. {
  199. $sRealClass = $this->sLinkedClass;
  200. }
  201. $oLinkObj = new $sRealClass();
  202. $oLinkObj->UpdateObjectFromPostedForm($this->sInputid);
  203. $aRow = array();
  204. $aRow['form::select'] = '<input type="checkbox" class="selectList'.$this->sInputid.'" value="'.(-$iTempId).'"/>';
  205. foreach($this->aZlist as $sLinkedAttCode)
  206. {
  207. $aRow[$sLinkedAttCode] = $oLinkObj->GetAsHTML($sLinkedAttCode);
  208. }
  209. return $oPage->GetTableRow($aRow, $aAttribs);
  210. }
  211. public function UpdateFromArray($oObj, $aData)
  212. {
  213. }
  214. }