chado_linker__prop.inc 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. <?php
  2. /**
  3. * Implements hook_info() for fields.
  4. *
  5. * This is a hook provided by the tripal_chado module for offloading the
  6. * hook_field_info() hook for each field to specify.
  7. */
  8. function chado_linker__prop_info() {
  9. return array(
  10. 'label' => t('Add a Property'),
  11. 'description' => t('Add details about this property.'),
  12. 'default_widget' => 'chado_linker__prop_widget',
  13. 'default_formatter' => 'chado_linker__prop_formatter',
  14. 'settings' => array(),
  15. 'storage' => array(
  16. 'type' => 'field_chado_storage',
  17. 'module' => 'tripal_chado',
  18. 'active' => TRUE
  19. ),
  20. );
  21. }
  22. /**
  23. * Implements hook_widget_info.
  24. *
  25. * This is a hook provided by the tripal_chado module for offloading
  26. * the hook_field_widget_info() hook for each field to specify.
  27. */
  28. function chado_linker__prop_widget_info() {
  29. return array(
  30. 'label' => t('Property'),
  31. 'field types' => array('chado_linker__prop'),
  32. );
  33. }
  34. /**
  35. * Implements hook_formatter_info.
  36. *
  37. * This is a hook provided by the tripal_chado module for
  38. * offloading the hook_field_formatter_info() for each field
  39. * to specify.
  40. *
  41. */
  42. function chado_linker__prop_formatter_info() {
  43. return array(
  44. 'label' => t('Property'),
  45. 'field types' => array('chado_linker__prop'),
  46. 'settings' => array(
  47. ),
  48. );
  49. }
  50. /**
  51. *
  52. * @param unknown $entity_type
  53. * @param unknown $entity
  54. * @param unknown $field
  55. * @param unknown $instance
  56. * @param unknown $langcode
  57. * @param unknown $items
  58. * @param unknown $display
  59. */
  60. function chado_linker__prop_formatter(&$element, $entity_type, $entity, $field,
  61. $instance, $langcode, $items, $display) {
  62. $field_name = $field['field_name'];
  63. $chado_table = $field['settings']['chado_table'];
  64. $properties = array();
  65. foreach ($items as $delta => $item) {
  66. $properties[] = $item[$chado_table . '__value'];
  67. }
  68. $content = implode(', ', $properties);
  69. $element[$delta] = array(
  70. '#type' => 'markup',
  71. '#markup' => $content,
  72. );
  73. }
  74. /**
  75. *
  76. * @param unknown $field_name
  77. * @param unknown $widget
  78. * @param unknown $form
  79. * @param unknown $form_state
  80. * @param unknown $field
  81. * @param unknown $instance
  82. * @param unknown $langcode
  83. * @param unknown $items
  84. * @param unknown $delta
  85. * @param unknown $element
  86. */
  87. function chado_linker__prop_widget(&$widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element) {
  88. $entity = $form['#entity'];
  89. $field_name = $field['field_name'];
  90. // Get the record and table mapping info.
  91. $chado_table = $entity->chado_table;
  92. $chado_column = $entity->chado_column;
  93. $chado_record = $entity->chado_record;
  94. $matches = array();
  95. preg_match('/(.*?)__(\d+)/', $field_name, $matches);
  96. // If the field name is not properly formatted then we can't tell what
  97. // table and type this is. So just return.
  98. if (count($matches) != 3) {
  99. return $widget;
  100. }
  101. $table_name = $matches[1];
  102. $cvterm_id = $matches[2];
  103. // Get the name of the pkey field for this property table and the name
  104. // of the FK field that links to the base table.
  105. $schema = chado_get_schema($table_name);
  106. $pkey = $schema['primary key'][0];
  107. $lfkey_field = key($schema['foreign keys'][$chado_table]['columns']);
  108. $rfkey_field = $schema['foreign keys'][$chado_table]['columns'][$lfkey_field];
  109. // Get the field defaults.
  110. $fk_value = '';
  111. $propval = '';
  112. if (array_key_exists($delta, $items)) {
  113. $propval = $items[$delta][$table_name . '__value'];
  114. }
  115. if ($chado_record) {
  116. $fk_value = $chado_record->$rfkey_field;
  117. }
  118. // The group of elements all-together need some extra functionality
  119. // after building up the full list (like draggable table rows).
  120. $widget['#theme'] = 'field_multiple_value_form';
  121. $widget['#title'] = $element['#title'];
  122. $widget['#description'] = $element['#description'];
  123. $widget['#field_name'] = $element['#field_name'];
  124. $widget['#language'] = $element['#language'];
  125. $widget['#weight'] = isset($element['#weight']) ? $element['#weight'] : 0;
  126. $widget['#element_validate'] = array('chado_linker__prop_widget_validate');
  127. $widget['#cardinality'] = 1;
  128. $widget['value'] = array(
  129. '#type' => 'hidden',
  130. '#default_value' => !empty($items[$delta]['value']) ? $items[$delta]['value'] : '',
  131. );
  132. $widget[$table_name . '__' . $lfkey_field] = array(
  133. '#type' => 'hidden',
  134. '#value' => $fk_value,
  135. );
  136. $widget[$table_name . '__value'] = array(
  137. '#type' => 'textfield',
  138. '#default_value' => $propval,
  139. );
  140. $widget[$table_name . '__type_id'] = array(
  141. '#type' => 'hidden',
  142. '#value' => $cvterm_id,
  143. );
  144. $widget[$table_name . '__rank'] = array(
  145. '#type' => 'hidden',
  146. '#value' => $delta,
  147. );
  148. return $widget;
  149. }
  150. /**
  151. *
  152. * @param unknown $form
  153. * @param unknown $form_state
  154. */
  155. function chado_linker__prop_widget_form_ajax_callback($form, $form_state) {
  156. $field_name = $form_state['triggering_element']['#parents'][0];
  157. return $form[$field_name];
  158. }
  159. /**
  160. * Callback function for validating the chado_linker__prop_widget.
  161. */
  162. function chado_linker__prop_widget_validate($element, &$form_state) {
  163. $field_name = $element['#field_name'];
  164. $delta = $element['#delta'];
  165. $entity = $element['#entity'];
  166. $matches = array();
  167. // Get the record and table mapping info.
  168. $chado_table = $entity->chado_table;
  169. $chado_column = $entity->chado_column;
  170. // Get the table name and cvterm_id for this field.
  171. preg_match('/(.*?)__(\d+)/', $field_name, $matches);
  172. $table_name = $matches[1];
  173. $cvterm_id = $matches[2];
  174. // Get the name of the pkey field for this property table and the name
  175. // of the FK field that links to the base table.
  176. $schema = chado_get_schema($table_name);
  177. $pkey = $schema['primary key'][0];
  178. $lfkey_field = key($schema['foreign keys'][$chado_table]['columns']);
  179. // If we don't have a property value then we need to set all other fields
  180. // to be empty so that when the module tries to save the field on the
  181. // entity it won't try to save a partial record.
  182. $pkey_val = tripal_chado_get_field_form_values($field_name, $form_state, $delta);
  183. $prop_value = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $table_name . "__value");
  184. $fk_val = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $table_name . '__' . $lfkey_field);
  185. $type_id = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $table_name . '__type_id');
  186. if (!$prop_value) {
  187. tripal_chado_set_field_form_values($field_name, $form_state, '', $delta);
  188. tripal_chado_set_field_form_values($field_name, $form_state, '', $delta, $table_name . '__' . $lfkey_field);
  189. tripal_chado_set_field_form_values($field_name, $form_state, '', $delta, $table_name . '__value');
  190. tripal_chado_set_field_form_values($field_name, $form_state, '', $delta, $table_name . '__type_id');
  191. tripal_chado_set_field_form_values($field_name, $form_state, '', $delta, $table_name . '__rank');
  192. }
  193. else {
  194. $rank = tripal_chado_get_field_form_values($field_name, $form_state, $delta, '_weight');
  195. tripal_chado_set_field_form_values($field_name, $form_state, $rank, $delta, $table_name . '__rank');
  196. }
  197. // Remove the properties for this record. We will re-add it. Otherwise,
  198. // if we change ranks, we wind up with multiple records in the property table.
  199. if ($pkey_val) {
  200. $match = array(
  201. $pkey => $pkey_val
  202. );
  203. chado_delete_record($table_name, $match);
  204. }
  205. }
  206. /**
  207. * Callback function for submitting the chado_linker__prop_widget.
  208. */
  209. function chado_linker__prop_widget_submit($element, &$form_state) {
  210. }
  211. /**
  212. * Loads the field values with appropriate data.
  213. *
  214. * This function is called by the tripal_chado_field_storage_load() for
  215. * each property managed by the field_chado_storage storage type. This is
  216. * an optional hook function that is only needed if the field has
  217. * multiple form elements.
  218. *
  219. * @param $field
  220. * @param $entity
  221. * @param $base_table
  222. * @param $record
  223. */
  224. function chado_linker__prop_load($field, $entity, $base_table, $record) {
  225. $field_name = $field['field_name'];
  226. $field_type = $field['type'];
  227. $field_table = $field['settings']['chado_table'];
  228. $field_column = $field['settings']['chado_column'];
  229. $matches = array();
  230. preg_match('/(.*?)__(\d+)/', $field_name, $matches);
  231. $table_name = $matches[1];
  232. $cvterm_id = $matches[2];
  233. // Get the FK that links to the base record.
  234. $schema = chado_get_schema($field_table);
  235. $pkey = $schema['primary key'][0];
  236. $fkey_lcolumn = key($schema['foreign keys'][$base_table]['columns']);
  237. $fkey_rcolumn = $schema['foreign keys'][$base_table]['columns'][$fkey_lcolumn];
  238. // Set some defaults for the empty record.
  239. $entity->{$field_name}['und'][0] = array(
  240. 'value' => '',
  241. $field_table . '__' . $fkey_lcolumn => '',
  242. $field_table . '__value' => '',
  243. $field_table . '__type_id' => '',
  244. $field_table . '__rank' => '',
  245. );
  246. // Get the properties associated with this base record for this fields
  247. // given type.
  248. $columns = array('*');
  249. $match = array(
  250. $fkey_lcolumn => $record->$fkey_rcolumn,
  251. 'type_id' => $cvterm_id,
  252. );
  253. $options = array(
  254. 'return_array' => TRUE,
  255. 'order_by' => array('rank' => 'ASC')
  256. );
  257. $properties = chado_select_record($field_table, $columns, $match, $options);
  258. for ($i = 0; $i < count($properties); $i++) {
  259. $property = $properties[$i];
  260. foreach ($schema['fields'] as $fname => $details) {
  261. $entity->{$field_name}['und'][$i] = array(
  262. 'value' => $property->$pkey,
  263. $field_table . '__' . $fkey_lcolumn => $property->$fkey_lcolumn,
  264. $field_table . '__value' => $property->value,
  265. $field_table . '__type_id' => $property->type_id,
  266. $field_table . '__rank' => $property->rank,
  267. );
  268. }
  269. }
  270. }