chado_linker__prop.inc 9.6 KB

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