chado_linker__prop_adder.inc 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. <?php
  2. class chado_linker__prop_adder extends TripalField {
  3. /**
  4. * @see TripalField::fieldInfo()
  5. */
  6. static function fieldInfo() {
  7. return array(
  8. 'label' => t('Add a Property Type'),
  9. 'description' => t('This record may have any number of properties. Use
  10. this field to first add the type.'),
  11. 'default_widget' => 'chado_linker__prop_adder_widget',
  12. 'default_formatter' => 'hidden',
  13. 'settings' => array(),
  14. 'storage' => array(
  15. 'type' => 'field_chado_storage',
  16. 'module' => 'tripal_chado',
  17. 'active' => TRUE
  18. ),
  19. 'no_ui' => TRUE
  20. );
  21. }
  22. /**
  23. * @see TripalField::can_attach()
  24. */
  25. protected function setCanAttach() {
  26. $table_name = $this->details['chado_table'];
  27. $type_table = $this->details['chado_type_table'];
  28. $type_field = $this->details['chado_type_column'];
  29. $cv_id = $this->details['chado_cv_id'];
  30. $cvterm_id = $this->details['chado_cvterm_id'];
  31. // If the linker table does not exist then we don't want to add attach.
  32. $prop_table = $table_name . 'prop';
  33. if (chado_table_exists($prop_table)) {
  34. $this->can_attach = TRUE;
  35. return;
  36. }
  37. $this->can_attach = FALSE;
  38. }
  39. /**
  40. * @see TripalField::setFieldName()
  41. */
  42. protected function setFieldName() {
  43. $table_name = $this->details['chado_table'];
  44. $type_table = $this->details['chado_type_table'];
  45. $type_field = $this->details['chado_type_column'];
  46. $cv_id = $this->details['chado_cv_id'];
  47. $cvterm_id = $this->details['chado_cvterm_id'];
  48. $this->field_name = $table_name . 'prop';
  49. }
  50. /**
  51. * @see TripalField::create_info()
  52. */
  53. function createInfo() {
  54. if (!$this->can_attach) {
  55. return;
  56. }
  57. $table_name = $this->details['chado_table'];
  58. $type_table = $this->details['chado_type_table'];
  59. $type_field = $this->details['chado_type_column'];
  60. $cv_id = $this->details['chado_cv_id'];
  61. $cvterm_id = $this->details['chado_cvterm_id'];
  62. $prop_table = $table_name . 'prop';
  63. return array(
  64. 'field_name' => $this->field_name,
  65. 'type' => 'chado_linker__prop_adder',
  66. 'cardinality' => 1,
  67. 'locked' => FALSE,
  68. 'storage' => array(
  69. 'type' => 'field_chado_storage',
  70. ),
  71. 'settings' => array(
  72. ),
  73. );
  74. }
  75. /**
  76. * @see TripalField::createInstanceInfo()
  77. */
  78. function createInstanceInfo() {
  79. if (!$this->canAttach()) {
  80. return;
  81. }
  82. $table_name = $this->details['chado_table'];
  83. $type_table = $this->details['chado_type_table'];
  84. $type_field = $this->details['chado_type_column'];
  85. $cv_id = $this->details['chado_cv_id'];
  86. $cvterm_id = $this->details['chado_cvterm_id'];
  87. $prop_table = $table_name . 'prop';
  88. return array(
  89. 'field_name' => $this->field_name,
  90. 'entity_type' => $this->entity_type,
  91. 'bundle' => $this->bundle->name,
  92. 'label' => 'Add Properties',
  93. 'description' => 'Add additional property types to this record.',
  94. 'required' => FALSE,
  95. 'settings' => array(
  96. 'auto_attach' => FALSE,
  97. ),
  98. 'widget' => array(
  99. 'type' => 'chado_linker__prop_adder_widget',
  100. 'settings' => array(
  101. 'display_label' => 1,
  102. ),
  103. ),
  104. 'display' => array(
  105. 'default' => array(
  106. 'label' => 'above',
  107. 'type' => 'chado_linker__prop_adder_formatter',
  108. 'settings' => array(),
  109. ),
  110. ),
  111. );
  112. }
  113. /**
  114. * @see TripalField::widgetInfo()
  115. */
  116. public static function widgetInfo() {
  117. return array(
  118. 'chado_linker__prop_adder_widget' => array(
  119. 'label' => t('Add a Property'),
  120. 'field types' => array('chado_linker__prop_adder'),
  121. ),
  122. );
  123. }
  124. /**
  125. * @see TripalField::widgetForm()
  126. */
  127. static function widgetForm(&$widget, &$form, &$form_state,
  128. $field, $instance, $langcode, $items, $delta, $element) {
  129. $widget['#type'] = 'fieldset';
  130. $widget['#title'] = $element['#title'];
  131. $widget['#description'] = $element['#description'];
  132. $widget['#group'] = 'entity_form_vtabs';
  133. $widget['kvproperty_instructions'] = array(
  134. '#type' => 'item',
  135. '#markup' => t('You may add additional properties to this form by
  136. providing a property name (from a vocabulary) in the field below
  137. and clicking the "Add Property" button. This will add a
  138. new field to the form above for the property you entered.
  139. In the future, this field will be present for all records
  140. of this type.'),
  141. );
  142. $widget['value'] = array(
  143. '#title' => t('Property Type'),
  144. '#type' => 'textfield',
  145. '#description' => t("Please enter the type of property that you want to
  146. add. As you type, suggestions will be provided."),
  147. '#autocomplete_path' => "admin/tripal/storage/chado/auto_name/cvterm/",
  148. );
  149. $widget['kvproperty_adder_link'] = array(
  150. '#type' => 'item',
  151. '#markup' => '<span class="kvproperty-adder-link">' . l('Add a term', 'admin/tripal/vocab/cvterm/add', array('attributes' => array('target' => '_blank'))) . '</span>',
  152. );
  153. // When this button is clicked, the form will be validated and submitted.
  154. // Therefore, we set custom submit and validate functions to override the
  155. // default form submit. In the validate function we set the form_state
  156. // to rebuild the form so the submit function never actually gets called,
  157. // but we need it or Drupal will run the default validate anyway.
  158. // we also set #limit_validation_errors to empty so fields that
  159. // are required that don't have values won't generate warnings.
  160. $widget['kvproperty_adder_button'] = array(
  161. '#value' => t('Add Property'),
  162. '#type' => 'submit',
  163. '#name' => 'kvproperty_adder_button',
  164. '#limit_validation_errors' => array(array($field['field_name'])),
  165. );
  166. }
  167. /**
  168. * @see TripalField::widgetFormValidate
  169. */
  170. public static function widgetFormValidate($entity_type, $entity, $field, $instance, $langcode,
  171. $items, &$errors) {
  172. }
  173. }
  174. /**
  175. * Callback function for validating the chado_linker__prop_adder_widget.
  176. */
  177. function chado_linker__prop_adder_widget_validate($element, &$form_state) {
  178. // Add the new field to the entity
  179. if (array_key_exists('triggering_element', $form_state) and
  180. $form_state['triggering_element']['#name'] == 'kvproperty_adder_button') {
  181. $form_state['rebuild'] = TRUE;
  182. $field_name = $element['#field_name'];
  183. $entity_type = $element['#entity']->type;
  184. $bundle = $element['#entity']->bundle;
  185. // Get the base table name from the field properties.
  186. $field = field_info_field($field_name);
  187. $base_table = $field['settings']['base_table'];
  188. // Get the term for the property
  189. $kvproperty = tripal_chado_get_field_form_values($field_name, $form_state);
  190. $term = chado_generate_var('cvterm', array('name' => $kvproperty), $options = array('return_array' => TRUE));
  191. if (count($term) == 1) {
  192. $prop_field_name = $field_name . '__' . $term[0]->cvterm_id;
  193. // The field name is the table name in this case. We want to get the
  194. // primary key as this should be the field that maps th the value.
  195. $schema = chado_get_schema($field_name);
  196. $pkey = $schema['primary key'][0];
  197. // Add the field if it doesn't already exists.
  198. $field = field_info_field('cvterm');
  199. if (!$field) {
  200. $create_info = array(
  201. 'field_name' => 'property-' . $term[0]->cvterm_id,
  202. 'type' => 'tripal_chado_kvproperty_widget',
  203. 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
  204. 'locked' => FALSE,
  205. 'storage' => array(
  206. 'type' => 'field_chado_storage',
  207. ),
  208. 'settings' => array(
  209. 'chado_table' => $field_name,
  210. 'chado_column' => $pkey,
  211. 'base_table' => $base_table,
  212. 'semantic_web' => '',
  213. ),
  214. );
  215. $field = field_create_field($create_info);
  216. }
  217. // Attach the field to the bundle if it isn't already.
  218. if (!$field and array_key_exists('bundles', $field) or
  219. !array_key_exists('TripalEntity', $field['bundles']) or
  220. !in_array($bundle_name, $field['bundles']['TripalEntity'])) {
  221. $createInstanceInfo = array(
  222. 'field_name' => 'property-' . $term[0]->cvterm_id,
  223. 'entity_type' => 'TripalEntity',
  224. 'bundle' => $this->bundle->name,
  225. 'label' => ucfirst(preg_replace('/_/', ' ', $term[0]->name)),
  226. 'description' => $term[0]->definition ? $term[0]->definition : '',
  227. 'required' => FALSE,
  228. 'settings' => array(),
  229. 'widget' => array(
  230. 'type' => 'tripal_chado_kvproperty_widget',
  231. 'settings' => array(
  232. 'display_label' => 1,
  233. ),
  234. ),
  235. 'display' => array(
  236. 'default' => array(
  237. 'label' => 'inline',
  238. 'type' => 'tripal_chado_kvproperty_formatter',
  239. 'settings' => array(),
  240. ),
  241. ),
  242. );
  243. $instance = field_create_instance($createInstanceInfo);
  244. }
  245. }
  246. else if (count($term) > 1) {
  247. form_set_error(implode('][', $element ['#parents']) . '][value', t("This term is present in multiple vocabularies. Please select the appropriate one."));
  248. }
  249. else {
  250. form_set_error(implode('][', $element ['#parents']) . '][value', t("Please provide a property type to add."));
  251. }
  252. }
  253. }