sbo__relationship_widget.inc 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. <?php
  2. class sbo__relationship_widget extends ChadoFieldWidget {
  3. // The default lable for this field.
  4. public static $default_label = 'Relationship';
  5. // The list of field types for which this formatter is appropriate.
  6. public static $field_types = array('sbo__relationship');
  7. /**
  8. *
  9. * @see TripalFieldWidget::form()
  10. */
  11. public function form(&$widget, &$form, &$form_state, $langcode, $items, $delta, $element) {
  12. parent::form($widget, $form, $form_state, $langcode, $items, $delta, $element);
  13. // Get the field settings.
  14. $entity = $form['#entity'];
  15. $field_name = $this->field['field_name'];
  16. $field_type = $this->field['type'];
  17. $field_table = $this->instance['settings']['chado_table'];
  18. $field_column = $this->instance['settings']['chado_column'];
  19. $base_table = $this->instance['settings']['base_table'];
  20. // Get the FK column that links to the base table.
  21. $base_table = $this->instance['settings']['base_table'];
  22. $schema = chado_get_schema($field_table);
  23. $pkey = $schema['primary key'][0];
  24. $fkeys = array_values($schema['foreign keys'][$base_table]['columns']);
  25. $fkey = $fkeys[0];
  26. // Get the instance settings. There are three options for how this widget
  27. // will be displayed. Those are controlled in the instance settings
  28. // of the field.
  29. // Option 1: relationship types are limited to a specific vocabulary.
  30. // Option 2: relationship types are limited to a subset of one vocabulary.
  31. // Option 3: relationship types are limited to a predefined set.
  32. $instance = $this->instance;
  33. $settings = '';
  34. $option1_vocabs = '';
  35. $option2_parent = '';
  36. $option2_vocab = '';
  37. $option3_rtypes = '';
  38. if (array_key_exists('relationships', $instance)) {
  39. $settings = $instance['settings']['relationships'];
  40. $option1_vocabs = $settings['option1_vocabs'];
  41. $option2_vocab = $settings['option2_vocab'];
  42. $option2_parent = $settings['option2_parent'];
  43. $option3_rtypes = $settings['relationship_types'];
  44. }
  45. // For testing if there are selected vocabs for option1 we'll copy the
  46. // contents in a special variable for later.
  47. $option1_test = $option1_vocabs;
  48. // Get the field defaults.
  49. $record_id = '';
  50. $subject_id = '';
  51. $object_id = '';
  52. $type_id = '';
  53. $value = '';
  54. $rank = '';
  55. $subject_uniquename = '';
  56. $object_uniquename = '';
  57. $type = '';
  58. // Handle special cases
  59. $subject_id_key = 'subject_id';
  60. $object_id_key = 'object_id';
  61. if ($field_table == 'nd_reagent_relationship') {
  62. $subject_id_key = 'subject_reagent_id';
  63. $object_id_key = 'object_reagent_id';
  64. }
  65. else if ($field_table == 'project_relationship') {
  66. $subject_id_key = 'subject_project_id';
  67. $object_id_key = 'object_project_id';
  68. }
  69. // If the field already has a value then it will come through the $items
  70. // array. This happens when editing an existing record.
  71. if (count($items) > 0 and array_key_exists($delta, $items)) {
  72. // Check for element values that correspond to fields in the Chado table.
  73. $record_id = tripal_get_field_item_keyval($items, $delta, 'chado-' . $field_table . '__' . $pkey, $record_id);
  74. $subject_id = tripal_get_field_item_keyval($items, $delta, 'chado-' . $field_table . '__' . $subject_id_key, $subject_id);
  75. $object_id = tripal_get_field_item_keyval($items, $delta, 'chado-' . $field_table . '__' . $object_id_key, $object_id);
  76. $type_id = tripal_get_field_item_keyval($items, $delta, 'chado-' . $field_table . '__type_id', $type_id);
  77. // Not all Chado tables have a value and rank. So we'll only get
  78. // those if applicable.
  79. if (array_key_exists('value', $schema['fields'])) {
  80. $value = tripal_get_field_item_keyval($items, $delta, 'chado-' . $field_table . '__value', $value);
  81. }
  82. if (array_key_exists('rank', $schema['fields'])) {
  83. $rank = tripal_get_field_item_keyval($items, $delta, 'chado-' . $field_table . '__rank', $rank);
  84. }
  85. // Get element values added to help support insert/updates.
  86. $object_uniquename = tripal_get_field_item_keyval($items, $delta, 'object_name', $object_uniquename);
  87. $subject_uniquename = tripal_get_field_item_keyval($items, $delta, 'subject_name', $subject_uniquename);
  88. $type = tripal_get_field_item_keyval($items, $delta, 'type_name', $type);
  89. }
  90. // Check $form_state['values'] to see if an AJAX call set the values.
  91. if (array_key_exists('values', $form_state) and array_key_exists($delta, $form_state['values'])) {
  92. $record_id = $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__' . $pkey];
  93. $subject_id = $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__' . $subject_id_key];
  94. $object_id = $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__' . $object_id_key];
  95. $type_id = $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__type_id'];
  96. if (array_key_exists('value', $schema['fields'])) {
  97. $value = $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__value'];
  98. }
  99. if (array_key_exists('rank', $schema['fields'])) {
  100. $rank = $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__rank'];
  101. }
  102. $object_uniquename = $form_state['values'][$field_name]['und'][$delta]['object_name'];
  103. $subject_uniquename = $form_state['values'][$field_name]['und'][$delta]['subject_name'];
  104. $type = $form_state['values'][$field_name]['und'][$delta]['type_name'];
  105. }
  106. $widget['#table_name'] = $chado_table;
  107. $widget['#fkeys'] = $schema['foreign keys'];
  108. $widget['#base_table'] = $base_table;
  109. $widget['#chado_record_id'] = isset($form['#entity']) ? $form['#entity']->chado_record_id : '';
  110. //$widget['#element_validate'] = array('sbo__relationship_validate');
  111. $widget['#prefix'] = "<span id='$field_table-$delta'>";
  112. $widget['#suffix'] = "</span>";
  113. $widget['value'] = array(
  114. '#type' => 'value',
  115. '#value' => array_key_exists($delta, $items) ? $items[$delta]['value'] : '',
  116. );
  117. $widget['chado-' . $field_table . '__' . $pkey] = array(
  118. '#type' => 'value',
  119. '#default_value' => $record_id,
  120. );
  121. $widget['chado-' . $field_table . '__' . $subject_id_key] = array(
  122. '#type' => 'value',
  123. '#default_value' => $subject_id,
  124. );
  125. $widget['chado-' . $field_table . '__type_id'] = array(
  126. '#type' => 'value',
  127. '#default_value' => $type_id,
  128. );
  129. $widget['chado-' . $field_table . '__' . $object_id_key] = array(
  130. '#type' => 'value',
  131. '#default_value' => $object_id,
  132. );
  133. if (array_key_exists('value', $schema['fields'])) {
  134. $widget['chado-' . $field_table . '__value'] = array(
  135. '#type' => 'value',
  136. '#default_value' => $value,
  137. );
  138. }
  139. if (array_key_exists('rank', $schema['fields'])) {
  140. $widget['chado-' . $field_table . '__rank'] = array(
  141. '#type' => 'value',
  142. '#default_value' => $rank,
  143. );
  144. }
  145. $widget['subject_name'] = array(
  146. '#type' => 'textfield',
  147. '#title' => t('Subject'),
  148. '#default_value' => $subject_uniquename,
  149. '#required' => $element['#required'],
  150. '#maxlength' => array_key_exists('length', $schema['fields'][$subject_id_key]) ? $schema['fields'][$subject_id_key]['length'] : 255,
  151. '#size' => 35,
  152. '#autocomplete_path' => "admin/tripal/storage/chado/auto_name/$base_table",
  153. );
  154. // Getting default values for the relationship type element.
  155. $default_voc = '';
  156. if (isset($form_state['field'][$field_name]['und']['instance']['default_value'][0]['vocabulary'])) {
  157. $default_voc = $form_state['field'][$field_name]['und']['instance']['default_value'][0]['vocabulary'];
  158. }
  159. $default_term = '';
  160. if (isset($form_state['field'][$field_name]['und']['instance']['default_value'][0]['type_name'])) {
  161. $default_term = $form_state['field'][$field_name]['und']['instance']['default_value'][0]['type_name'];
  162. }
  163. $default_type_id = $type_id;
  164. if (!$type_id && isset($form_state['field'][$field_name]['und']['instance']['default_value'][0]['type_id'])) {
  165. $default_type_id = $form_state['field'][$field_name]['und']['instance']['default_value'][0]['type_id'];
  166. }
  167. // Option 3: Custom list of Relationship Types
  168. $rtype_options = array();
  169. $rtype_options[] = 'Select a Type';
  170. if ($option3_rtypes) {
  171. $rtypes = explode(PHP_EOL, $option3_rtypes);
  172. foreach($rtypes AS $rtype) {
  173. // Ignore empty lines
  174. if (trim($rtype) == '') {
  175. continue;
  176. }
  177. $term = tripal_get_cvterm(array('name' => trim($rtype)));
  178. // Try to get term with vocabulary specified
  179. if (!$term) {
  180. $tmp = explode('|', trim($rtype), 2);
  181. $cv = tripal_get_cv(array('name' => trim($tmp[0])));
  182. $rtype = trim($tmp[1]);
  183. $term = tripal_get_cvterm(array('name' => $rtype, 'cv_id' => $cv->cv_id));
  184. }
  185. $rtype_options[$term->cvterm_id] = $term->name;
  186. }
  187. $widget['type_id'] = array(
  188. '#type' => 'select',
  189. '#title' => t('Relationship Type'),
  190. '#options' => $rtype_options,
  191. '#default_value' => $default_type_id,
  192. );
  193. if ($type_id && !key_exists($type_id, $rtype_options)) {
  194. form_set_error($this->field['field_name'] . '[' . $langcode . '][' . $delta . '][type_id]', 'Illegal option detected for Relationship Type. Please contact site administrator to fix the problem');
  195. }
  196. }
  197. // Option 2: Child terms of a selected cvterm
  198. else if ($option2_vocab) {
  199. $values = array(
  200. 'cv_id' => $option2_vocab,
  201. 'name' => $option2_parent
  202. );
  203. $parent_term = tripal_get_cvterm($values);
  204. // If the term wasn't found then see if it's a synonym.
  205. if(!$parent_term) {
  206. $values = array(
  207. 'synonym' => array(
  208. 'name' => trim($option2_parent),
  209. )
  210. );
  211. $synonym = tripal_get_cvterm($values);
  212. if ($synonym && $synonym->cv_id->cv_id == $option2_vocab) {
  213. $parent_term = $synonym;
  214. }
  215. }
  216. // Get the child terms of the parent term found above.
  217. $sql = "
  218. SELECT subject_id,
  219. (SELECT name from {cvterm} where cvterm_id = subject_id) AS name
  220. FROM {cvtermpath}
  221. WHERE
  222. object_id = :parent_cvterm_id AND
  223. cv_id = :parent_cv_id
  224. ORDER BY name
  225. ";
  226. $args = array(
  227. ':parent_cvterm_id' => $parent_term->cvterm_id,
  228. ':parent_cv_id' => $parent_term->cv_id->cv_id
  229. );
  230. $results = chado_query($sql, $args);
  231. while($child = $results->fetchObject()) {
  232. $rtype_options[$child->subject_id] = $child->name;
  233. }
  234. $widget['type_id'] = array(
  235. '#type' => 'select',
  236. '#title' => t('Relationship Type'),
  237. '#options' => $rtype_options,
  238. '#default_value' => $default_type_id,
  239. );
  240. if ($type_id && !key_exists($type_id, $rtype_options)) {
  241. form_set_error($this->field['field_name'] . '[' . $langcode . '][' . $delta . '][type_id]', 'Illegal option detected for Relationship Type. Please contact site administrator to fix the problem');
  242. }
  243. }
  244. // Option 1: All terms of selected vocabularies
  245. else if ($option1_test && array_pop($option1_test)) {
  246. $sql = "SELECT cvterm_id, name FROM {cvterm} WHERE cv_id IN (:cv_id) ORDER BY name";
  247. $results = chado_query($sql, array(':cv_id' => $option1_vocabs));
  248. while ($obj = $results->fetchObject()) {
  249. $rtype_options[$obj->cvterm_id] = $obj->name;
  250. }
  251. $widget['type_id'] = array(
  252. '#type' => 'select',
  253. '#title' => t('Relationship Type'),
  254. '#options' => $rtype_options,
  255. '#default_value' => $default_type_id,
  256. );
  257. if ($type_id && !key_exists($type_id, $rtype_options)) {
  258. form_set_error($this->field['field_name'] . '[' . $langcode . '][' . $delta . '][type_id]', 'Illegal option detected for Relationship Type. Please contact site administrator to fix the problem');
  259. }
  260. }
  261. // Default option:
  262. else {
  263. // Set up available cvterms for selection
  264. $vocs = array(0 => 'Select a vocabulary');
  265. $vocs = tripal_get_cv_select_options();
  266. $cv_id = isset($form_state['values'][$field_name]['und'][0]['vocabulary']) ? $form_state['values'][$field_name]['und'][0]['vocabulary'] : 0;
  267. // Try getting the cv_id from cvterm for existing records
  268. if (!$cv_id && $type_id) {
  269. $cvterm = tripal_get_cvterm(array('cvterm_id' => $type_id));
  270. if (isset($cvterm->cv_id->cv_id)) {
  271. $cv_id = $cvterm->cv_id->cv_id;
  272. $default_term = $cvterm->name;
  273. }
  274. }
  275. if (!$cv_id) {
  276. $cv_id = $default_voc;
  277. }
  278. $widget['vocabulary'] = array(
  279. '#type' => 'select',
  280. '#title' => t('Vocabulary'),
  281. '#options' => $vocs,
  282. '#required' => $element['#required'],
  283. '#default_value' => $cv_id,
  284. '#ajax' => array(
  285. 'callback' => "sbo__relationship_widget_form_ajax_callback",
  286. 'wrapper' => "$field_table-$delta",
  287. 'effect' => 'fade',
  288. 'method' => 'replace'
  289. ),
  290. );
  291. if ($cv_id) {
  292. $options = array();
  293. $widget['type_name'] = array(
  294. '#type' => 'textfield',
  295. '#title' => t('Relationship Type'),
  296. '#size' => 15,
  297. '#default_value' => $default_term,
  298. '#autocomplete_path' => "admin/tripal/storage/chado/auto_name/cvterm/$cv_id"
  299. );
  300. }
  301. }
  302. $widget['object_name'] = array(
  303. '#type' => 'textfield',
  304. '#title' => t('Object'),
  305. '#default_value' => $object_uniquename,
  306. '#required' => $element['#required'],
  307. '#maxlength' => array_key_exists('length', $schema['fields'][$object_id_key]) ? $schema['fields'][$object_id_key]['length'] : 255,
  308. '#size' => 35,
  309. '#autocomplete_path' => "admin/tripal/storage/chado/auto_name/$base_table",
  310. );
  311. }
  312. /**
  313. *
  314. * @see TripalFieldWidget::submit()
  315. */
  316. public function submit($form, &$form_state, $entity_type, $entity, $langcode, $delta) {
  317. $field_name = $this->field['field_name'];
  318. $field_type = $this->field['type'];
  319. $field_table = $this->instance['settings']['chado_table'];
  320. $field_column = $this->instance['settings']['chado_column'];
  321. $base_table = $this->instance['settings']['base_table'];
  322. $chado_record_id = $entity->chado_record_id;
  323. $schema = chado_get_schema($field_table);
  324. $fkeys = $schema['foreign keys'];
  325. // Handle special cases
  326. $subject_id_key = 'subject_id';
  327. $object_id_key = 'object_id';
  328. if ($field_table == 'nd_reagent_relationship') {
  329. $subject_id_key = 'subject_reagent_id';
  330. $object_id_key = 'object_reagent_id';
  331. }
  332. else if ($field_table == 'project_relationship') {
  333. $subject_id_key = 'subject_project_id';
  334. $object_id_key = 'object_project_id';
  335. }
  336. $type_name = array_key_exists('type_name', $item) ? $item['type_name'] : '';
  337. $subject_id = $form_state['values'][$field_name][$langcode][$delta]['chado' . $field_table . '__' . $subject_id_key];
  338. $object_id = $form_state['values'][$field_name][$langcode][$delta]['chado' . $field_table . '__' . $object_id_key];
  339. $type_id = $form_state['values'][$field_name][$langcode][$delta]['chado' . $field_table . '__type_id'];
  340. $subject_name = $form_state['values'][$field_name][$langcode][$delta]['subject_name'];
  341. $object_name = $form_state['values'][$field_name][$langcode][$delta]['object_name'];
  342. // If the row is empty then skip this one, there's nothing to validate.
  343. if (!($type_id or !$type_name) and !$subject_name and !$object_name) {
  344. return;
  345. }
  346. // Get the subject ID.
  347. $subject_id = '';
  348. $fkey_rcolumn = $fkeys[$base_table]['columns']['subject_id'];
  349. $matches = array();
  350. if (preg_match('/\[id: (\d+)\]/', $subject_name, $matches)) {
  351. $subject_id = $matches[1];
  352. }
  353. else {
  354. $values = array('uniquename' => $subject_name);
  355. $subject = chado_select_record($base_table, array($fkey_rcolumn), $values);
  356. $subject_id = $subject[0]->$fkey_rcolumn;
  357. }
  358. // Get the object ID.
  359. $object_id = '';
  360. $fkey_rcolumn = $fkeys[$base_table]['columns']['object_id'];
  361. $matches = array();
  362. if (preg_match('/\[id: (\d+)\]/', $object_name, $matches)) {
  363. $object_id = $matches[1];
  364. }
  365. else {
  366. $values = array('uniquename' => $object_name);
  367. $object = chado_select_record($base_table, array($fkey_rcolumn), $values);
  368. $object_id = $object[0]->$fkey_rcolumn;
  369. }
  370. // Set the IDs according to the values that were determined above.
  371. $form_state['values'][$field_name][$langcode][$delta]['chado' . $field_table . '__' . $subject_id_key] = $subject_id;
  372. $form_state['values'][$field_name][$langcode][$delta]['chado' . $field_table . '__' . $object_id_key] = $object_id;
  373. $form_state['values'][$field_name][$langcode][$delta]['chado' . $field_table . '__type_id'] = $type_name;
  374. $form_state['values'][$field_name][$langcode][$delta]['chado' . $field_table . '__rank'] = $item['_weight'];
  375. }
  376. /**
  377. * Theme function for the sbo__relationship_widget.
  378. */
  379. public function theme($element) {
  380. $layout = "
  381. <div class=\"chado-linker--relationship-widget\">
  382. <div class=\"chado-linker--relationship-widget-item\">" .
  383. drupal_render($element['subject_name']) . "
  384. </div>
  385. <div class=\"chado-linker--relationship-widget-item\">" .
  386. drupal_render($element['vocabulary']) . "
  387. </div>
  388. <div class=\"chado-linker--relationship-widget-item\">" .
  389. drupal_render($element['type_name']) . "
  390. </div>
  391. <div class=\"chado-linker--relationship-widget-item\">" .
  392. drupal_render($element['type_id']) . "
  393. </div>
  394. <div>" .
  395. drupal_render($element['object_name']) . "
  396. </div>
  397. </div>
  398. ";
  399. return $layout;
  400. }
  401. }
  402. function theme_sbo__relationship_instance_settings ($variables) {
  403. $element = $variables['element'];
  404. $option1 = $element['option1'];
  405. $option1_vocabs = $element['option1_vocabs'];
  406. $option2 = $element['option2'];
  407. $option2_vocab = $element['option2_vocab'];
  408. $option2_parent = $element['option2_parent'];
  409. $option3 = $element['option3'];
  410. $rtype = $element['relationship_types'];
  411. $layout = drupal_render($option1);
  412. $layout .= drupal_render($option1_vocabs);
  413. $layout .=
  414. drupal_render($option2) .
  415. "<div class=\"chado-linker--relationship-instance-settings-option2\">" .
  416. "<div class=\"chado-linker--relationship-instance-settings-option2-item\">" .
  417. drupal_render($option2_vocab) .
  418. "</div>" .
  419. "<div class=\"chado-linker--relationship-instance-settings-option2-item\">" .
  420. drupal_render($option2_parent) .
  421. "</div>" .
  422. "</div>";
  423. $layout .= drupal_render($option3);
  424. $layout .= drupal_render($rtype);
  425. return $layout;
  426. }
  427. /**
  428. * An Ajax callback for the relationshp widget.
  429. */
  430. function sbo__relationship_widget_form_ajax_callback(&$form, $form_state) {
  431. // Get the triggering element
  432. $form_element_name = $form_state['triggering_element']['#name'];
  433. preg_match('/(.+?)\[(.+?)\]\[(.+?)\]/', $form_element_name, $matches);
  434. $field = $matches[1];
  435. $lang = $matches[2];
  436. $delta = $matches[3];
  437. // Return the widget that triggered the AJAX call
  438. if (isset($form[$field][$lang][$delta])) {
  439. return $form[$field][$lang][$delta];
  440. }
  441. // Alternatively, return the default value widget for the widget setting form
  442. else {
  443. return $form['instance']['default_value_widget'][$field];
  444. }
  445. }
  446. /**
  447. * An Ajax callback for the relationshp instance setting form.
  448. */
  449. function sbo__relationship_instance_settings_form_ajax_callback(&$form, &$form_state) {
  450. $acpath = $form['instance']['settings']['relationships']['option2_parent']['#autocomplete_path'];
  451. $acpath .= $form_state['values']['instance']['settings']['relationships']['option2_vocab'] . '/';
  452. $urlval = $form['instance']['settings']['relationships']['option2_parent']['#autocomplete_input']['#url_value'];
  453. $urlval .= $form_state['values']['instance']['settings']['relationships']['option2_vocab'];
  454. // Reset value if a different vocabulary is selected
  455. $form['instance']['settings']['relationships']['option2_parent']['#value'] = NULL;
  456. $form_state['values']['instance']['settings']['relationships']['option2_parent'] = NULL;
  457. $form['instance']['settings']['relationships']['option2_parent']['#autocomplete_path'] = $acpath;
  458. $form['instance']['settings']['relationships']['option2_parent']['#autocomplete_input']['#url_value'] = $urlval;
  459. return $form['instance']['settings']['relationships']['option2_parent'];
  460. }