PageRenderTime 26ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/php/lib/forms/metadata_associations_form.class.php

https://bitbucket.org/chamilo/chamilo-app-metadata/
PHP | 212 lines | 159 code | 46 blank | 7 comment | 13 complexity | b8181a08858cea981a75ab5fbfae2584 MD5 | raw file
  1. <?php
  2. namespace metadata;
  3. use common\libraries\FormValidator;
  4. use common\libraries\Translation;
  5. use common\libraries\EqualityCondition;
  6. use common\libraries\Path;
  7. use common\libraries\Utilities;
  8. use common\libraries\ToolbarItem;
  9. use common\libraries\Toolbar;
  10. use common\libraries\Theme;
  11. /**
  12. * This class describes the form for a MetadataAttributeAssociation object.
  13. * @author Sven Vanpoucke
  14. * @author Jens Vanderheyden
  15. **/
  16. class MetadataAssociationsForm extends FormValidator
  17. {
  18. const TYPE_CREATE = 1;
  19. const TYPE_EDIT = 2;
  20. const PARAM_PROPERTY = 'properties';
  21. const PARAM_ATTRIBUTE = 'attributes';
  22. private $user;
  23. private $metadata_element;
  24. private $application;
  25. function __construct($form_type, $metadata_element, $action, $user, $application)
  26. {
  27. parent :: __construct('metadata_attribute_association_settings', 'post', $action);
  28. $this->user = $user;
  29. $this->metadata_element = $metadata_element;
  30. $this->application = $application;
  31. $this->form_type = $form_type;
  32. if ($this->form_type == self :: TYPE_EDIT)
  33. {
  34. $this->build_editing_form();
  35. }
  36. elseif ($this->form_type == self :: TYPE_CREATE)
  37. {
  38. $this->build_creation_form();
  39. }
  40. $this->setDefaults();
  41. }
  42. function build_basic_form()
  43. {
  44. $condition = new EqualityCondition(MetadataAttributeAssociation :: PROPERTY_PARENT_ID, $this->metadata_element->get_id());
  45. $mdm = MetadataDataManager :: get_instance();
  46. $metadata_attribute_associations = $mdm->retrieve_metadata_attribute_associations($condition);
  47. $metadata_element_nestings = $mdm->retrieve_metadata_element_nestings($condition);
  48. $defaults = array();
  49. $attributes = array();
  50. while ($nesting = $metadata_attribute_associations->next_result())
  51. {
  52. $propName = ($nesting->get_child_type() == MetadataManager :: PARAM_METADATA_ELEMENT) ? self :: PARAM_PROPERTY : self :: PARAM_ATTRIBUTE;
  53. $element = array();
  54. $element['classes'] = 'type type_cda_language';
  55. $element['id'] = $propName . '_' . $nesting->get_child_id();
  56. switch ($nesting->get_child_type())
  57. {
  58. case MetadataManager :: PARAM_METADATA_ELEMENT :
  59. $element = $this->application->retrieve_metadata_element($nesting->get_child_id());
  60. $name = $element->get_ns_prefix() . ':' . $element->get_name() . ' (property)';
  61. break;
  62. case MetadataManager :: PARAM_METADATA_ATTRIBUTE :
  63. $attribute = $this->application->retrieve_metadata_attribute($nesting->get_child_id());
  64. $name = $attribute->render_name() . ' (attribute)';
  65. break;
  66. }
  67. $element['title'] = $name;
  68. $defaults[] = $element;
  69. }
  70. $attributes['defaults'] = $defaults;
  71. $attributes['exclude'] = array();
  72. $attributes['nodesSelectable'] = true;
  73. $attributes['locale']['Display'] = Translation :: get('SelectObjects', array('OBJECT' => Translation :: get('MetadataAttributes')), Utilities :: COMMON_LIBRARIES);
  74. $locale['Searching'] = Translation :: get('Searching', null, Utilities :: COMMON_LIBRARIES);
  75. $locale['NoResults'] = Translation :: get('NoResults', null, Utilities :: COMMON_LIBRARIES);
  76. $locale['Error'] = Translation :: get('Error', null, Utilities :: COMMON_LIBRARIES);
  77. //$attributes['options'] = '';
  78. $attributes['search_url'] = Path :: get(WEB_PATH) . 'metadata/php/xml_feeds/xml_attributes_feed.php';
  79. $element_finder = $this->createElement('element_finder', MetadataManager :: PARAM_METADATA_ATTRIBUTE_ASSOCIATION, Translation :: get('SelectObjects', array('OBJECT' => Translation :: get('MetadataAttribute')), Utilities :: COMMON_LIBRARIES), $attributes['search_url'], $attributes['locale'], $attributes['defaults'], $attributes['options']);
  80. $element_finder->excludeElements($attributes['exclude']);
  81. $this->addElement($element_finder);
  82. $properties = array();
  83. $attributes = array();
  84. $defaults = array();
  85. while ($nesting = $metadata_element_nestings->next_result())
  86. {
  87. $propName = ($nesting->get_child_type() == MetadataManager :: PARAM_METADATA_ELEMENT) ? self :: PARAM_PROPERTY : self :: PARAM_ATTRIBUTE;
  88. $element = array();
  89. $element['classes'] = 'type type_cda_language';
  90. $element['id'] = $propName . '_' . $nesting->get_child_id();
  91. switch ($nesting->get_child_type())
  92. {
  93. case MetadataManager :: PARAM_METADATA_ELEMENT :
  94. $element = $this->application->retrieve_metadata_element($nesting->get_child_id());
  95. $name = $element->get_ns_prefix() . ':' . $element->get_name() . ' (property)';
  96. break;
  97. case MetadataManager :: PARAM_METADATA_ATTRIBUTE :
  98. $attribute = $this->application->retrieve_metadata_attribute($nesting->get_child_id());
  99. $name = $attribute->render_name() . ' (attribute)';
  100. break;
  101. }
  102. $element['title'] = $name;
  103. $defaults[] = $element;
  104. }
  105. $attributes['defaults'] = $defaults;
  106. $attributes['nodesSelectable'] = true;
  107. $attributes['exclude'] = array('lala');
  108. $attributes['locale']['Display'] = 'Select attributes';
  109. $attributes['locale']['Error'] = 'Error';
  110. $attributes['locale']['NoResults'] = 'No results found';
  111. $attributes['locale']['Searching'] = 'Searching';
  112. $attributes['search_url'] = Path :: get(WEB_PATH) . 'metadata/php/xml_feeds/xml_attributes_feed.php';
  113. $element_finder = $this->createElement('element_finder', MetadataManager :: PARAM_METADATA_ELEMENT_NESTING, Translation :: get('SelectObjects', array('OBJECT' => Translation :: get('MetadataAttribute')), Utilities :: COMMON_LIBRARIES), $attributes['search_url'], $attributes['locale'], $attributes['defaults'], $attributes['options']);
  114. $this->addElement($element_finder);
  115. }
  116. function build_editing_form()
  117. {
  118. $this->build_basic_form();
  119. //$this->addElement('hidden', MetadataAttributeAssociation :: PROPERTY_ID);
  120. $buttons[] = $this->createElement('style_submit_button', 'submit', Translation :: get('Update', null, Utilities :: COMMON_LIBRARIES), array('class' => 'positive update'));
  121. $buttons[] = $this->createElement('style_reset_button', 'reset', Translation :: get('Reset', null, Utilities :: COMMON_LIBRARIES), array('class' => 'normal empty'));
  122. $this->addGroup($buttons, 'buttons', null, '&nbsp;', false);
  123. }
  124. function build_creation_form()
  125. {
  126. $this->build_basic_form();
  127. $buttons[] = $this->createElement('style_submit_button', 'submit', Translation :: get('Create', null, Utilities :: COMMON_LIBRARIES), array('class' => 'positive'));
  128. $buttons[] = $this->createElement('style_reset_button', 'reset', Translation :: get('Reset', null, Utilities :: COMMON_LIBRARIES), array('class' => 'normal empty'));
  129. $this->addGroup($buttons, 'buttons', null, '&nbsp;', false);
  130. }
  131. function update_associations()
  132. {
  133. $fails = 0;
  134. $values = $this->exportValues();
  135. $this->application->delete_metadata_attribute_associations(Utilities :: camelcase_to_underscores(Utilities :: get_classname_from_namespace(MetadataAttributeAssociation :: CLASS_NAME)), $this->metadata_element);
  136. foreach ($values[MetadataManager :: PARAM_METADATA_ATTRIBUTE_ASSOCIATION] as $type => $elements)
  137. {
  138. foreach ($elements as $id)
  139. {
  140. $metadata_attribute_association = new MetadataAttributeAssociation();
  141. $metadata_attribute_association->set_parent_id($this->metadata_element->get_id());
  142. $metadata_attribute_association->set_child_id($id);
  143. $typeName = ($type == 'attributes') ? MetadataManager :: PARAM_METADATA_ATTRIBUTE : MetadataManager :: PARAM_METADATA_ELEMENT;
  144. $metadata_attribute_association->set_child_type($typeName);
  145. if (! $metadata_attribute_association->create())
  146. $fails ++;
  147. }
  148. }
  149. $this->application->delete_metadata_element_nestings(Utilities :: camelcase_to_underscores(Utilities :: get_classname_from_namespace(MetadataElementNesting :: CLASS_NAME)), $this->metadata_element);
  150. foreach ($values[MetadataManager :: PARAM_METADATA_ELEMENT_NESTING] as $type => $elements)
  151. {
  152. foreach ($elements as $id)
  153. {
  154. $metadata_element_nesting = new MetadataElementNesting();
  155. $metadata_element_nesting->set_parent_id($this->metadata_element->get_id());
  156. $metadata_element_nesting->set_child_id($id);
  157. $typeName = ($type == 'attributes') ? MetadataManager :: PARAM_METADATA_ATTRIBUTE : MetadataManager :: PARAM_METADATA_ELEMENT;
  158. $metadata_element_nesting->set_child_type($typeName);
  159. if (! $metadata_element_nesting->create())
  160. $fails ++;
  161. }
  162. }
  163. return ($fails < 1) ? true : false;
  164. }
  165. }
  166. ?>