sbo__relationship_widget.inc 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  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. // PROTECTED CLASS MEMBERS -- DO NOT OVERRIDE
  9. // --------------------------------------------------------------------------
  10. // This field depends heavily on the schema of the relationship and base
  11. // table. The following variables cache the schema to greatly speed up
  12. // this field.
  13. // Note: both are ChadoSchema objects.
  14. protected $schema;
  15. protected $base_schema;
  16. // The column which indicated the subject/object_id in the current
  17. // relationship table. This allows us to support exceptions in the common
  18. // chado naming conventions.
  19. protected $subject_id_column;
  20. protected $object_id_column;
  21. // An array of columns to use as the "name" of the subject and object.
  22. // For example, for the feature table, this will be the name,
  23. // whereas, for the organism table this will be the genus & species.
  24. protected $base_name_columns;
  25. // One of 'type_id', or 'table_name'. Not all base tables have a type_id so
  26. // this setting allows us to better handle these cases.
  27. protected $base_type_column;
  28. // The field instance for this widget. This allows us to use some of the
  29. // field methods and info in the widget.
  30. protected $field_instance;
  31. /**
  32. * Extends TripalField::__construct().
  33. */
  34. public function __construct($field, $instance) {
  35. parent::__construct($field, $instance);
  36. module_load_include('inc', 'tripal_chado', 'includes/TripalFields/sbo__relationship/sbo__relationship');
  37. $this->field_instance = new sbo__relationship($field, $instance);
  38. // Retrieve the schema's.
  39. $this->schema = $this->field_instance->getRelTableSchema();
  40. $this->base_schema = $this->field_instance->getBaseTableSchema();
  41. // Retrieve the subject/object column names.
  42. $this->subject_id_column = $this->field_instance->getSubjectIdColumn();
  43. $this->object_id_column = $this->field_instance->getObjectIdColumn();
  44. // Retrieve the columns to use for name/type.
  45. $this->base_name_columns = $this->field_instance->getBaseNameColumns();
  46. $this->base_type_column = $this->field_instance->getBaseTypeColumn();
  47. }
  48. /**
  49. *
  50. * @see TripalFieldWidget::form()
  51. */
  52. public function form(&$widget, &$form, &$form_state, $langcode, $items, $delta, $element) {
  53. parent::form($widget, $form, $form_state, $langcode, $items, $delta, $element);
  54. // Get the field settings.
  55. $field_name = $this->field['field_name'];
  56. $field_type = $this->field['type'];
  57. $field_table = $this->instance['settings']['chado_table'];
  58. $field_column = $this->instance['settings']['chado_column'];
  59. $base_table = $this->instance['settings']['base_table'];
  60. // @debug dpm($this, 'this');
  61. // Get the primary key of the relationship table
  62. $pkey = $this->schema['primary key'][0];
  63. // 'nd_reagent_relationship' and 'project_relationship' have different column names from
  64. // subject_id/object_id. Retrieve those determined in the constructor.
  65. $subject_id_key = $this->subject_id_column;
  66. $object_id_key = $this->object_id_column;
  67. // And save them in the widget for use in testing/debugging.
  68. $widget['#subject_id_key'] = $subject_id_key;
  69. $widget['#object_id_key'] = $object_id_key;
  70. // Default Values:
  71. //----------------
  72. $record_id = '';
  73. $subject_id = '';
  74. $object_id = '';
  75. $type_id = '';
  76. $value = '';
  77. $rank = '';
  78. $subject_label = '';
  79. $object_label = '';
  80. $type = '';
  81. // If the field already has a value then it will come through the $items
  82. // array. This happens when editing an existing record.
  83. if (count($items) > 0 and array_key_exists($delta, $items)) {
  84. // Sometimes empty/initialized items are getting through.
  85. // To determine if it this one of them, the type_id must always be there.
  86. $type_id = tripal_get_field_item_keyval($items, $delta, 'chado-' . $field_table . '__type_id', $type_id);
  87. if (!empty($type_id)) {
  88. // Check for element values that correspond to fields in the Chado table.
  89. $record_id = tripal_get_field_item_keyval($items, $delta, 'chado-' . $field_table . '__' . $pkey, $record_id);
  90. $subject_id = tripal_get_field_item_keyval($items, $delta, 'chado-' . $field_table . '__' . $subject_id_key, $subject_id);
  91. $object_id = tripal_get_field_item_keyval($items, $delta, 'chado-' . $field_table . '__' . $object_id_key, $object_id);
  92. $type_id = tripal_get_field_item_keyval($items, $delta, 'chado-' . $field_table . '__type_id', $type_id);
  93. // Not all Chado tables have a value and rank. So we'll only get
  94. // those if applicable.
  95. if (array_key_exists('value', $this->schema['fields'])) {
  96. $value = tripal_get_field_item_keyval($items, $delta, 'chado-' . $field_table . '__value', $value);
  97. }
  98. if (array_key_exists('rank', $this->schema['fields'])) {
  99. $rank = tripal_get_field_item_keyval($items, $delta, 'chado-' . $field_table . '__rank', $rank);
  100. }
  101. // Get element values added to help support insert/updates.
  102. $object_label = tripal_get_field_item_keyval($items, $delta, 'object_name', $object_label);
  103. $subject_label = tripal_get_field_item_keyval($items, $delta, 'subject_name', $subject_label);
  104. $type = tripal_get_field_item_keyval($items, $delta, 'type_name', $type);
  105. //@debug dpm(array($subject_id, $type_id, $object_id), 'have an item!');
  106. }
  107. }
  108. // Check $form_state['values'] to see if an AJAX call set the values.
  109. if (array_key_exists('values', $form_state) and
  110. array_key_exists($field_name, $form_state['values'])) {
  111. $record_id = $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__' . $pkey];
  112. $subject_id = $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__' . $subject_id_key];
  113. $object_id = $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__' . $object_id_key];
  114. $type_id = $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__type_id'];
  115. if (array_key_exists('value', $this->schema['fields'])) {
  116. $value = $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__value'];
  117. }
  118. if (array_key_exists('rank', $this->schema['fields'])) {
  119. $rank = $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__rank'];
  120. }
  121. $object_label = $form_state['values'][$field_name]['und'][$delta]['object_name'];
  122. $subject_label = $form_state['values'][$field_name]['und'][$delta]['subject_name'];
  123. $type = $form_state['values'][$field_name]['und'][$delta]['type_name'];
  124. //@debug dpm(array($subject_id, $type_id, $object_id), 'have an item (AJAX)!');
  125. }
  126. // Getting default values for the relationship type element.
  127. $default_voc = '';
  128. if (isset($form_state['field'][$field_name]['und']['instance']['default_value'][0]['vocabulary'])) {
  129. $default_voc = $form_state['field'][$field_name]['und']['instance']['default_value'][0]['vocabulary'];
  130. }
  131. $default_term = '';
  132. if (isset($form_state['field'][$field_name]['und']['instance']['default_value'][0]['type_name'])) {
  133. $default_term = $form_state['field'][$field_name]['und']['instance']['default_value'][0]['type_name'];
  134. }
  135. $default_type_id = $type_id;
  136. if (!$type_id && isset($form_state['field'][$field_name]['und']['instance']['default_value'][0]['type_id'])) {
  137. $default_type_id = $form_state['field'][$field_name]['und']['instance']['default_value'][0]['type_id'];
  138. }
  139. // Check if we have autocomplete available for this base table
  140. $autocomplete_path = "admin/tripal/storage/chado/auto_name/$base_table";
  141. $has_autocomplete = db_query('SELECT 1 FROM menu_router WHERE path=:path',
  142. array(':path' => $autocomplete_path.'/%'))->fetchField();
  143. // Save some values for later...
  144. $widget['#table_name'] = $field_table;
  145. $widget['#fkeys'] = $this->schema['foreign keys'];
  146. $widget['#base_table'] = $base_table;
  147. $widget['#chado_record_id'] = isset($form['#entity']) ? $form['#entity']->chado_record_id : '';
  148. //$widget['#element_validate'] = array('sbo__relationship_validate');
  149. $widget['#prefix'] = "<span id='$field_table-$delta'>";
  150. $widget['#suffix'] = "</span>";
  151. // Save the values needed by the Chado Storage API.
  152. //-------------------------------------------------
  153. $widget['value'] = array(
  154. '#type' => 'value',
  155. '#value' => array_key_exists($delta, $items) ? $items[$delta]['value'] : '',
  156. );
  157. $widget['chado-' . $field_table . '__' . $pkey] = array(
  158. '#type' => 'value',
  159. '#default_value' => $record_id,
  160. );
  161. $widget['chado-' . $field_table . '__' . $subject_id_key] = array(
  162. '#type' => 'value',
  163. '#default_value' => $subject_id,
  164. );
  165. $widget['chado-' . $field_table . '__type_id'] = array(
  166. '#type' => 'value',
  167. '#default_value' => $type_id,
  168. );
  169. $widget['chado-' . $field_table . '__' . $object_id_key] = array(
  170. '#type' => 'value',
  171. '#default_value' => $object_id,
  172. );
  173. if (array_key_exists('value', $this->schema['fields'])) {
  174. $widget['chado-' . $field_table . '__value'] = array(
  175. '#type' => 'value',
  176. '#default_value' => $value,
  177. );
  178. }
  179. if (array_key_exists('rank', $this->schema['fields'])) {
  180. $widget['chado-' . $field_table . '__rank'] = array(
  181. '#type' => 'value',
  182. '#default_value' => $rank,
  183. );
  184. }
  185. // Subject:
  186. //----------
  187. $widget['subject_name'] = array(
  188. '#type' => 'textfield',
  189. '#title' => t('Subject'),
  190. '#default_value' => $subject_label,
  191. '#required' => $element['#required'],
  192. '#maxlength' => array_key_exists($subject_id_key, $this->schema['fields']) && array_key_exists('length', $this->schema['fields'][$subject_id_key]) ? $this->schema['fields'][$subject_id_key]['length'] : 255,
  193. '#size' => 35,
  194. );
  195. // Add autocomplete if we have one for this base table.
  196. if ($has_autocomplete) {
  197. $widget['subject_name']['#autocomplete_path'] = $autocomplete_path;
  198. }
  199. // Type:
  200. //-------
  201. $rtype_options = $this->get_rtype_select_options();
  202. if ($rtype_options) {
  203. $widget['type_id'] = array(
  204. '#type' => 'select',
  205. '#title' => t('Relationship Type'),
  206. '#options' => $rtype_options,
  207. '#empty_option' => 'Select a Type',
  208. '#default_value' => $default_type_id,
  209. );
  210. if ($type_id && !key_exists($type_id, $rtype_options)) {
  211. 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');
  212. }
  213. }
  214. // Default option:
  215. // If we were determine an type_id option set...
  216. // then we will need to provide a cv + type autocomplete.
  217. else {
  218. // Set up available cvterms for selection
  219. $vocs = array();
  220. $vocs = chado_get_cv_select_options();
  221. unset($vocs[0]);
  222. $cv_id = isset($form_state['values'][$field_name][$langcode][$delta]['vocabulary']) ? $form_state['values'][$field_name][$langcode][$delta]['vocabulary'] : 0;
  223. // Try getting the cv_id from cvterm for existing records
  224. if (!$cv_id && $type_id) {
  225. $cvterm = chado_get_cvterm(array('cvterm_id' => $type_id));
  226. if (isset($cvterm->cv_id->cv_id)) {
  227. $cv_id = $cvterm->cv_id->cv_id;
  228. $default_term = $cvterm->name;
  229. }
  230. }
  231. if (!$cv_id) {
  232. $cv_id = $default_voc;
  233. }
  234. $widget['vocabulary'] = array(
  235. '#type' => 'select',
  236. '#title' => t('Vocabulary'),
  237. '#options' => $vocs,
  238. '#required' => $element['#required'],
  239. '#default_value' => $cv_id,
  240. '#empty_option' => 'Select a Vocabulary',
  241. '#ajax' => array(
  242. 'callback' => "sbo__relationship_widget_form_ajax_callback",
  243. 'wrapper' => "$field_table-$delta",
  244. 'effect' => 'fade',
  245. 'method' => 'replace'
  246. ),
  247. );
  248. $widget['type_name'] = array(
  249. '#type' => 'textfield',
  250. '#title' => t('Relationship Type'),
  251. '#size' => 15,
  252. '#default_value' => $default_term,
  253. '#disabled' => TRUE,
  254. '#autocomplete_path' => "admin/tripal/storage/chado/auto_name/cvterm/$cv_id"
  255. );
  256. if ($cv_id) {
  257. $widget['type_name']['#disabled'] = FALSE;
  258. }
  259. }
  260. // Object:
  261. //--------
  262. $widget['object_name'] = array(
  263. '#type' => 'textfield',
  264. '#title' => t('Object'),
  265. '#default_value' => $object_label,
  266. '#required' => $element['#required'],
  267. '#maxlength' => array_key_exists($object_id_key, $this->schema['fields']) && array_key_exists('length', $this->schema['fields'][$object_id_key]) ? $this->schema['fields'][$object_id_key]['length'] : 255,
  268. '#size' => 35,
  269. );
  270. // Add autocomplete if we have one for this base table.
  271. if ($has_autocomplete) {
  272. $widget['object_name']['#autocomplete_path'] = $autocomplete_path;
  273. }
  274. }
  275. /**
  276. *
  277. * @see TripalFieldWidget::validate()
  278. */
  279. public function validate($element, $form, &$form_state, $langcode, $delta) {
  280. $field_name = $this->field['field_name'];
  281. $field_type = $this->field['type'];
  282. $field_table = $this->instance['settings']['chado_table'];
  283. $field_column = $this->instance['settings']['chado_column'];
  284. $base_table = $this->instance['settings']['base_table'];
  285. $chado_record_id = array_key_exists('#entity', $element)? $element['#entity']->chado_record_id : NULL;
  286. // @debug ddl(array_keys($form[$field_name][$langcode][$delta]), "widget form keys: $delta");
  287. $fkeys = $this->schema['foreign keys'];
  288. // 'nd_reagent_relationship' and 'project_relationship' have different column names from
  289. // subject_id/object_id. Retrieve the column names determined in the form.
  290. $subject_id_key = $this->subject_id_column;
  291. $object_id_key = $this->object_id_column;
  292. // Retrieve the values from the form for the current $delta.
  293. $voc_id = array_key_exists('vocabulary', $form_state['values'][$field_name][$langcode][$delta]) ? $form_state['values'][$field_name][$langcode][$delta]['vocabulary'] : '';
  294. $type_name = array_key_exists('type_name', $form_state['values'][$field_name][$langcode][$delta]) ? $form_state['values'][$field_name][$langcode][$delta]['type_name'] : '';
  295. $subject_id = isset($form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__' . $subject_id_key]) ? $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__' . $subject_id_key] : '';
  296. $object_id = isset($form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__' . $object_id_key]) ? $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__' . $object_id_key]: '';
  297. $type_id = $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__type_id'];
  298. $subject_name = $form_state['values'][$field_name][$langcode][$delta]['subject_name'];
  299. $object_name = $form_state['values'][$field_name][$langcode][$delta]['object_name'];
  300. // Validation:
  301. //------------
  302. // If the row is empty then skip this one, there's nothing to validate.
  303. if (!($type_id OR $type_name) and !$subject_name and !$object_name) {
  304. return;
  305. }
  306. // Do not proceed if subject ID or object ID does not exist
  307. if (!key_exists($subject_id_key, $fkeys[$base_table]['columns']) ||
  308. !key_exists($object_id_key, $fkeys[$base_table]['columns'])) {
  309. return;
  310. }
  311. // Validation is occuring in the field::validate() but we need to know if it finds errors.
  312. // As such, I'm calling it here to check.
  313. // Also, field::validate() doesn't seem to always show it's errors
  314. // OR stop form submission? so we need to ensure that happens here.
  315. // sbo__relationship::validate($entity_type, $entity, $langcode, $items, &$errors)
  316. $errors = $this->field_instance->validateItem($form_state['values'][$field_name][$langcode][$delta], $element['#chado_record_id']);
  317. if ($errors) {
  318. foreach ($errors as $error) {
  319. switch ($error['element']) {
  320. case 'subject':
  321. form_set_error('sbo__relationship]['.$langcode.']['.$delta.'][subject_name', $error['message']);
  322. break;
  323. case 'type':
  324. if (isset($element['vocabulary'])) {
  325. form_set_error('sbo__relationship]['.$langcode.']['.$delta.'][type_name', $error['message']);
  326. form_set_error('sbo__relationship]['.$langcode.']['.$delta.'][vocabulary', '');
  327. }
  328. elseif (isset($element['type_id'])) {
  329. form_set_error('sbo__relationship]['.$langcode.']['.$delta.'][type_id', $error['message']);
  330. }
  331. break;
  332. case 'object':
  333. form_set_error('sbo__relationship]['.$langcode.']['.$delta.'][object_name', $error['message']);
  334. break;
  335. default:
  336. form_set_error('sbo__relationship]['.$langcode.']['.$delta, $error['message']);
  337. }
  338. }
  339. // Ensure data is prepared for the storage backend:
  340. //-------------------------------------------------
  341. }
  342. else {
  343. if ($type_name && $voc_id) {
  344. $val = array(
  345. 'cv_id' => $voc_id,
  346. 'name' => $type_name
  347. );
  348. $cvterm = chado_generate_var('cvterm', $val);
  349. if (isset($cvterm->cvterm_id)) {
  350. $type_id = $cvterm->cvterm_id;
  351. }
  352. }
  353. // Get the subject ID.
  354. $subject_id = '';
  355. $fkey_rcolumn = $fkeys[$base_table]['columns'][$subject_id_key];
  356. $matches = array();
  357. // First check if it's in the textfield due to use of the autocomplete.
  358. if (preg_match('/\[id: (\d+)\]/', $subject_name, $matches)) {
  359. $subject_id = $matches[1];
  360. }
  361. // Otherwise we need to look it up using the name field determined in the
  362. // constructor for the current field. There may be more then one name field
  363. // (e.g. organism: genus + species) so we want to check both.
  364. else {
  365. $sql = 'SELECT ' . $fkey_rcolumn . ' FROM {' . $base_table . '} WHERE ' . implode('||', $this->base_name_columns) . '=:keyword';
  366. $subject = chado_query($sql, [':keyword' => $subject_name])->fetchAll();
  367. if(count($subject) > 0) {
  368. $subject_id = $subject[0]->$fkey_rcolumn;
  369. }
  370. }
  371. // Get the object ID.
  372. $object_id = '';
  373. $fkey_rcolumn = $fkeys[$base_table]['columns'][$object_id_key];
  374. $matches = array();
  375. // First check if it's in the textfield due to use of the autocomplete.
  376. if (preg_match('/\[id: (\d+)\]/', $object_name, $matches)) {
  377. $object_id = $matches[1];
  378. }
  379. // Otherwise we need to look it up using the name field determined in the
  380. // constructor for the current field. There may be more then one name field
  381. // (e.g. organism: genus + species) so we want to check both.
  382. else {
  383. $sql = 'SELECT ' . $fkey_rcolumn . ' FROM {' . $base_table . '} WHERE ' . implode('||', $this->base_name_columns) . '=:keyword';
  384. $object = chado_query($sql, [':keyword' => $object_name])->fetchAll();
  385. if (count($object) > 0) {
  386. $object_id = $object[0]->$fkey_rcolumn;
  387. }
  388. }
  389. // If we have all three values required for a relationship...
  390. // Then set them as the chado field storage expects them to be set.
  391. if ($subject_id && $object_id && $type_id) {
  392. // Set the IDs according to the values that were determined above.
  393. $form_state['values'][$field_name][$langcode][$delta]['value'] = 'value must be set but is not used';
  394. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__' . $subject_id_key] = $subject_id;
  395. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__' . $object_id_key] = $object_id;
  396. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__type_id'] = $type_id;
  397. if (array_key_exists('rank', $this->schema['fields'])) {
  398. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__rank'] = $form_state['values'][$field_name][$langcode][$delta]['_weight'];
  399. }
  400. }
  401. // Otherwise, leave them blank?
  402. else {
  403. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__' . $subject_id_key] = '';
  404. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__' . $object_id_key] = '';
  405. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__type_id'] = '';
  406. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__value'] = '';
  407. if (array_key_exists('rank', $this->schema['fields'])) {
  408. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__rank'] = '';
  409. }
  410. }
  411. }
  412. // @debug
  413. dpm($form_state['values'][$field_name][$langcode][$delta], "form state values: $delta");
  414. }
  415. /**
  416. * Theme function for the sbo__relationship_widget.
  417. */
  418. public function theme($element) {
  419. $layout = "
  420. <div class=\"chado-linker--relationship-widget\">
  421. <div class=\"chado-linker--relationship-widget-item\">" .
  422. drupal_render($element['subject_name']) . "
  423. </div>
  424. <div class=\"chado-linker--relationship-widget-item\">" .
  425. drupal_render($element['vocabulary']) . "
  426. </div>
  427. <div class=\"chado-linker--relationship-widget-item\">" .
  428. drupal_render($element['type_name']) . "
  429. </div>
  430. <div class=\"chado-linker--relationship-widget-item\">" .
  431. drupal_render($element['type_id']) . "
  432. </div>
  433. <div>" .
  434. drupal_render($element['object_name']) . "
  435. </div>
  436. </div>
  437. ";
  438. return $layout;
  439. }
  440. /**
  441. * Retrieve options for the type drop-down for the relationship widget.
  442. */
  443. public function get_rtype_select_options() {
  444. // Get the instance settings. There are three options for how this widget
  445. // will be displayed. Those are controlled in the instance settings
  446. // of the field.
  447. // Option 1: relationship types are limited to a specific vocabulary.
  448. // Option 2: relationship types are limited to a subset of one vocabulary.
  449. // Option 3: relationship types are limited to a predefined set.
  450. $instance = $this->instance;
  451. $settings = '';
  452. $option1_vocabs = '';
  453. $option2_parent = '';
  454. $option2_vocab = '';
  455. $option3_rtypes = '';
  456. if (array_key_exists('relationships', $instance)) {
  457. $settings = $instance['settings']['relationships'];
  458. $option1_vocabs = $settings['option1_vocabs'];
  459. $option2_vocab = $settings['option2_vocab'];
  460. $option2_parent = $settings['option2_parent'];
  461. $option3_rtypes = $settings['relationship_types'];
  462. }
  463. // For testing if there are selected vocabs for option1 we'll copy the
  464. // contents in a special variable for later.
  465. $option1_test = $option1_vocabs;
  466. // Option 3: Custom list of Relationship Types
  467. $rtype_options = array();
  468. if ($option3_rtypes) {
  469. $rtypes = explode(PHP_EOL, $option3_rtypes);
  470. foreach($rtypes AS $rtype) {
  471. // Ignore empty lines
  472. if (trim($rtype) == '') {
  473. continue;
  474. }
  475. $term = chado_get_cvterm(array('name' => trim($rtype)));
  476. // Try to get term with vocabulary specified
  477. if (!$term) {
  478. $tmp = explode('|', trim($rtype), 2);
  479. $cv = chado_get_cv(array('name' => trim($tmp[0])));
  480. $rtype = trim($tmp[1]);
  481. $term = chado_get_cvterm(array('name' => $rtype, 'cv_id' => $cv->cv_id));
  482. }
  483. $rtype_options[$term->cvterm_id] = $term->name;
  484. }
  485. return $rtype_options;
  486. }
  487. // Option 2: Child terms of a selected cvterm
  488. else if ($option2_vocab) {
  489. $values = array(
  490. 'cv_id' => $option2_vocab,
  491. 'name' => $option2_parent
  492. );
  493. $parent_term = chado_get_cvterm($values);
  494. // If the term wasn't found then see if it's a synonym.
  495. if(!$parent_term) {
  496. $values = array(
  497. 'synonym' => array(
  498. 'name' => trim($option2_parent),
  499. )
  500. );
  501. $synonym = chado_get_cvterm($values);
  502. if ($synonym && $synonym->cv_id->cv_id == $option2_vocab) {
  503. $parent_term = $synonym;
  504. }
  505. }
  506. // Get the child terms of the parent term found above.
  507. $sql = "
  508. SELECT subject_id,
  509. (SELECT name from {cvterm} where cvterm_id = subject_id) AS name
  510. FROM {cvtermpath}
  511. WHERE
  512. object_id = :parent_cvterm_id AND
  513. cv_id = :parent_cv_id
  514. ORDER BY name
  515. ";
  516. $args = array(
  517. ':parent_cvterm_id' => $parent_term->cvterm_id,
  518. ':parent_cv_id' => $parent_term->cv_id->cv_id
  519. );
  520. $results = chado_query($sql, $args);
  521. while($child = $results->fetchObject()) {
  522. $rtype_options[$child->subject_id] = $child->name;
  523. }
  524. return $rtype_options;
  525. }
  526. // Option 1: All terms of selected vocabularies
  527. else if ($option1_test && array_pop($option1_test)) {
  528. $sql = "SELECT cvterm_id, name FROM {cvterm} WHERE cv_id IN (:cv_id) ORDER BY name";
  529. $results = chado_query($sql, array(':cv_id' => $option1_vocabs));
  530. while ($obj = $results->fetchObject()) {
  531. $rtype_options[$obj->cvterm_id] = $obj->name;
  532. }
  533. return $rtype_options;
  534. }
  535. // Default option:
  536. // Let the form deal with this by providing a type autocomplete?
  537. else {
  538. return FALSE;
  539. }
  540. }
  541. }
  542. function theme_sbo__relationship_instance_settings ($variables) {
  543. $element = $variables['element'];
  544. $option1 = $element['option1'];
  545. $option1_vocabs = $element['option1_vocabs'];
  546. $option2 = $element['option2'];
  547. $option2_vocab = $element['option2_vocab'];
  548. $option2_parent = $element['option2_parent'];
  549. $option3 = $element['option3'];
  550. $rtype = $element['relationship_types'];
  551. $layout = drupal_render($option1);
  552. $layout .= drupal_render($option1_vocabs);
  553. $layout .=
  554. drupal_render($option2) .
  555. "<div class=\"chado-linker--relationship-instance-settings-option2\">" .
  556. "<div class=\"chado-linker--relationship-instance-settings-option2-item\">" .
  557. drupal_render($option2_vocab) .
  558. "</div>" .
  559. "<div class=\"chado-linker--relationship-instance-settings-option2-item\">" .
  560. drupal_render($option2_parent) .
  561. "</div>" .
  562. "</div>";
  563. $layout .= drupal_render($option3);
  564. $layout .= drupal_render($rtype);
  565. return $layout;
  566. }
  567. /**
  568. * An Ajax callback for the relationshp widget.
  569. */
  570. function sbo__relationship_widget_form_ajax_callback(&$form, $form_state) {
  571. // Get the triggering element
  572. $form_element_name = $form_state['triggering_element']['#name'];
  573. preg_match('/(.+?)\[(.+?)\]\[(.+?)\]/', $form_element_name, $matches);
  574. $field = $matches[1];
  575. $lang = $matches[2];
  576. $delta = $matches[3];
  577. // Return the widget that triggered the AJAX call
  578. if (isset($form[$field][$lang][$delta])) {
  579. return $form[$field][$lang][$delta];
  580. }
  581. // Alternatively, return the default value widget for the widget setting form
  582. else {
  583. return $form['instance']['default_value_widget'][$field];
  584. }
  585. }
  586. /**
  587. * An Ajax callback for the relationshp instance setting form.
  588. */
  589. function sbo__relationship_instance_settings_form_ajax_callback(&$form, &$form_state) {
  590. $acpath = $form['instance']['settings']['relationships']['option2_parent']['#autocomplete_path'];
  591. $acpath .= $form_state['values']['instance']['settings']['relationships']['option2_vocab'] . '/';
  592. $urlval = $form['instance']['settings']['relationships']['option2_parent']['#autocomplete_input']['#url_value'];
  593. $urlval .= $form_state['values']['instance']['settings']['relationships']['option2_vocab'];
  594. // Reset value if a different vocabulary is selected
  595. $form['instance']['settings']['relationships']['option2_parent']['#value'] = NULL;
  596. $form_state['values']['instance']['settings']['relationships']['option2_parent'] = NULL;
  597. $form['instance']['settings']['relationships']['option2_parent']['#autocomplete_path'] = $acpath;
  598. $form['instance']['settings']['relationships']['option2_parent']['#autocomplete_input']['#url_value'] = $urlval;
  599. return $form['instance']['settings']['relationships']['option2_parent'];
  600. }