tripal_api.chado_node.relationships.inc 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  1. <?php
  2. /**
  3. * @file
  4. * API to manage the chado _relationship table for various Tripal Node Types
  5. *
  6. * How To Use:
  7. * @code
  8. function chado_example_form($form, $form_state) {
  9. // Default values for form elements can come in the following ways:
  10. //
  11. // 1) as elements of the $node object. This occurs when editing an existing node
  12. // 2) in the $form_state['values'] array which occurs on a failed validation or
  13. // ajax callbacks when the ajax call originates from non-submit fields other
  14. // than button
  15. // 3) in the $form_state['input'] array which occurs on ajax callbacks from submit
  16. // form elements (e.g. buttons) and the form is being rebuilt but has not yet
  17. // been validated
  18. //
  19. // The reference elements added by this function do use AJAX calls from buttons,
  20. // therefore, it is important to check for form values in the $form_state['values']
  21. // for case #2 above, and in the $form_state['input'] for case #3.
  22. // See the chado analysis node form for an example.
  23. // Next, add in all the form array definition particular to your node type
  24. // To add in the relationship form elements, you first need to prepare the arguments
  25. // for the function call.
  26. $details = array(
  27. 'relationship_table' => 'example_relationship', // the name of the table linking additional dbxrefs to this node
  28. 'base_table' => 'example', // the name of the chado table this node links to
  29. 'base_foreign_key' => 'example_id', // key to link to the chado content created by this node
  30. 'base_key_value' => $example_id, // the value of the above key
  31. 'fieldset_title' => 'Relationships', // the non-translated title for this fieldset
  32. 'additional_instructions' => '' // a non-stranslated string providing additional instructions
  33. );
  34. // Finally, and add the additional form elements to the form
  35. tripal_api_chado_node_relationships_form($form, $form_state, $details);
  36. return $form;
  37. }
  38. function chado_example_insert($node) {
  39. // if there is an example_id in the $node object then this must be a sync so
  40. // we can skip adding the chado_example as it is already there, although
  41. // we do need to proceed with the rest of the insert
  42. if (!property_exists($node, 'example_id')) {
  43. // Add record to chado example table
  44. // Add to any other tables needed
  45. // Add all relationships
  46. // Existing _relationship links with the current example as either the subject_id
  47. // or object_id will be cleared and then re-added
  48. tripal_api_chado_node_relationships_form_update_relationships(
  49. $node,
  50. 'example_relationship',
  51. $node->example_id
  52. );
  53. }
  54. // Add record to chado_example linking example_id to new node
  55. }
  56. function chado_example_update($node) {
  57. // Update record in chado example table
  58. // Update any other tables needed
  59. // Update all additional database references
  60. // Existing _relationship links with the current example as either the subject_id
  61. // or object_id will be cleared and then re-added
  62. tripal_api_chado_node_relationships_form_update_relationships(
  63. $node,
  64. 'example_relationship',
  65. $node->example_id
  66. );
  67. // Don't need to update chado_example linking table since niether example_id or nid can be changed in update
  68. }
  69. * @endcode
  70. */
  71. /**
  72. * Provides a form for adding to BASE_relationship and relationship tables
  73. *
  74. * @param $form
  75. * The Drupal form array into which the relationship elements will be added
  76. * @param $form_state
  77. * The corresponding form_state array for the form
  78. * @param $details
  79. * An array defining details needed by this form. Required Keys are:
  80. * - relationship_table: the name of the relationship table (ie: feature_relationship)
  81. * - base_table: the name of the base table (ie: feature)
  82. * - base_foreign_key: the name of the foreign key linking this table to the non-relationship table (ie: feature_id)
  83. * - base_key_value: the value of the base_foreign_key for the current form (ie: 999 if the feature_id=999)
  84. * - nodetype: the non-translated singular title of this node type
  85. * One of the following:
  86. * - cv_id: the id of the ontology to supply terms for the type dropdown
  87. * - cv_name: the name of the ontology to supply terms for the type dropdown
  88. * Optional keys include:
  89. * - fieldset_title: the non-translated title for this fieldset
  90. * - additional_instructions: a non-translated string providing additional instructions
  91. * - nodetype_plural: the non-translated plural title of this node type
  92. *
  93. * @ingroup tripal_relationships_api
  94. */
  95. function tripal_api_chado_node_relationships_form(&$form, &$form_state, $details) {
  96. $form_state['rebuild'] = TRUE;
  97. // Set Defaults for optional fields
  98. $details['fieldset_title'] = (isset($details['fieldset_title'])) ? $details['fieldset_title'] : 'Relationships';
  99. $details['additional_instructions'] = (isset($details['additional_instructions'])) ? $details['additional_instructions'] : '';
  100. $details['nodetype_plural'] = (isset($details['nodetype_plural'])) ? $details['nodetype_plural'] : $details['nodetype'] . 's';
  101. // Add defaults into form_state to be used elsewhere
  102. $form['rel_details'] = array(
  103. '#type' => 'hidden',
  104. '#value' => serialize($details)
  105. );
  106. // Get relationship type options
  107. if (isset($details['cv_id'])) {
  108. $query = "SELECT cvterm_id, name FROM {cvterm} cvterm WHERE cv_id = :cv_id";
  109. $result = chado_query($query, array(':cv_id' => $details['cv_id']));
  110. } elseif (isset($details['cv_name'])) {
  111. $query = "SELECT cvterm_id, name FROM {cvterm} cvterm WHERE cv_id IN (SELECT cv_id FROM cv WHERE name = :cv_name)";
  112. $result = chado_query($query, array(':cv_name' => $details['cv_name']));
  113. }
  114. $type_options = array(0 => 'Select a Type');
  115. foreach ($result as $cvterm) {
  116. $type_options[ $cvterm->cvterm_id ] = $cvterm->name;
  117. }
  118. $form['relationships'] = array(
  119. '#type' => 'fieldset',
  120. '#title' => t($details['fieldset_title']),
  121. '#description' => t('You may add relationships between this %nodetype and other
  122. %nodetype_plural by entering the details below. You may add
  123. as many relationships as desired by clicking the add button on the right. To
  124. remove a relationship, click the remove button. ' . $details['additional_instructions'],
  125. array('%nodetype' => $details['nodetype'], '%nodetype_plural' => $details['nodetype_plural'])),
  126. '#prefix' => "<div id='relationships-fieldset'>",
  127. '#suffix' => '</div>'
  128. );
  129. // this form element is a tree, so that we don't puke all of the values into then node variable
  130. // it is set as a tree, and keeps them in the $form_state['values']['relationship_table'] heading.
  131. $form['relationships']['relationship_table'] = array(
  132. '#type' => 'markup',
  133. '#tree' => TRUE,
  134. '#prefix' => '<div id="tripal-generic-edit-relationships-table">',
  135. '#suffix' => '</div>',
  136. '#theme' => 'tripal_api_chado_node_relationships_form_table'
  137. );
  138. // Add relationships already attached to the node
  139. //---------------------------------------------
  140. /* Relationships can come to us in two ways:
  141. *
  142. * 1) In the form state in the $form_state['chado_relationships']. Data is in this field
  143. * when an AJAX call updates the form state or a validation error.
  144. *
  145. * 2) Directly from the database if the record already has _relationships associated. This
  146. * data is only used the first time the form is loaded. On AJAX calls or validation
  147. * errors the fields on the form are populated from the $form_state['chado_relationships']
  148. * entry.
  149. */
  150. if (isset($form_state['chado_relationships'])) {
  151. $existing_rels = $form_state['chado_relationships'];
  152. }
  153. else {
  154. $existing_rels = chado_query(
  155. "SELECT rel.*, base1.uniquename as object_name, base2.uniquename as subject_name, cvterm.name as type_name
  156. FROM {".$details['relationship_table']."} rel
  157. LEFT JOIN {".$details['base_table']."} base1 ON base1.".$details['base_foreign_key']." = rel.object_id
  158. LEFT JOIN {".$details['base_table']."} base2 ON base2.".$details['base_foreign_key']." = rel.subject_id
  159. LEFT JOIN {cvterm} cvterm ON cvterm.cvterm_id = rel.type_id
  160. WHERE rel.object_id = :base_key_value OR rel.subject_id = :base_key_value",
  161. array(':base_key_value' => $details['base_key_value'])
  162. );
  163. }
  164. /* The format of the $existing_rels' array is either:
  165. *
  166. * From the chado_relationships array:
  167. * $form_state['chado_relationships'] = array(
  168. * '[type_id]-[rank]' => array(
  169. * 'object_id' => [the _relationship.object_id value],
  170. * 'object_name' => [the base_table.uniquename value linked on base_foreign_key=object_id],
  171. * 'subject_id' => [the _relationship.subject_id value],
  172. * 'subject_name' => [the base_table.uniquename value linked on base_foreign_key=subject_id],
  173. * 'type_id' => [the _relationship.type_id value],
  174. * 'type_name' => [the cvterm.name value linked on type_id],
  175. * 'rank' => [the _relationship.rank value],
  176. * ),
  177. * );
  178. *
  179. * OR
  180. * Populated from the database:
  181. * $existing_rels = array(
  182. * 0 => array(
  183. * 'relationship_id' => [the _relationship.relationship_id value],
  184. * 'object_id' => [the _relationship.object_id value],
  185. * 'object_name' => [the base_table.uniquename value linked on base_foreign_key=object_id],
  186. * 'subject_id' => [the _relationship.subject_id value],
  187. * 'subject_name' => [the base_table.uniquename value linked on base_foreign_key=subject_id],
  188. * 'type_id' => [the _relationship.type_id value],
  189. * 'type_name' => [the cvterm.name value linked on type_id],
  190. * 'rank' => [the _relationship.rank value],
  191. * ),
  192. * );
  193. *
  194. * NOTE: The main difference is the key
  195. *
  196. * Loop on the array elements of the $existing_rels array and add
  197. * an element to the form for each one.
  198. */
  199. foreach ($existing_rels as $relationship) {
  200. $form['relationships']['relationship_table'][$relationship->type_id]['#type'] = 'markup';
  201. $form['relationships']['relationship_table'][$relationship->type_id]['#type'] = '';
  202. $form['relationships']['relationship_table'][$relationship->type_id][$relationship->rank]['#type'] = 'markup';
  203. $form['relationships']['relationship_table'][$relationship->type_id][$relationship->rank]['#value'] = '';
  204. $form['relationships']['relationship_table'][$relationship->type_id][$relationship->rank]['object_id'] = array(
  205. '#type' => 'hidden',
  206. '#value' => $relationship->object_id
  207. );
  208. $form['relationships']['relationship_table'][$relationship->type_id][$relationship->rank]['subject_id'] = array(
  209. '#type' => 'hidden',
  210. '#value' => $relationship->subject_id
  211. );
  212. $form['relationships']['relationship_table'][$relationship->type_id][$relationship->rank]['type_id'] = array(
  213. '#type' => 'hidden',
  214. '#value' => $relationship->type_id
  215. );
  216. $form['relationships']['relationship_table'][$relationship->type_id][$relationship->rank]['object_name'] = array(
  217. '#type' => 'markup',
  218. '#markup' => $relationship->object_name
  219. );
  220. $form['relationships']['relationship_table'][$relationship->type_id][$relationship->rank]['type_name'] = array(
  221. '#type' => 'markup',
  222. '#markup' => $relationship->type_name
  223. );
  224. $form['relationships']['relationship_table'][$relationship->type_id][$relationship->rank]['subject_name'] = array(
  225. '#type' => 'markup',
  226. '#markup' => $relationship->subject_name
  227. );
  228. $form['relationships']['relationship_table'][$relationship->type_id][$relationship->rank]['rank'] = array(
  229. '#type' => 'markup',
  230. '#markup' => $relationship->rank
  231. );
  232. $form['relationships']['relationship_table'][$relationship->type_id][$relationship->rank]['rel_action'] = array(
  233. '#type' => 'submit',
  234. '#value' => t('Remove'),
  235. '#name' => "rel_remove-".$relationship->type_id.'-'.$relationship->rank,
  236. '#ajax' => array(
  237. 'callback' => 'tripal_api_chado_node_relationships_form_ajax_update',
  238. 'wrapper' => 'tripal-generic-edit-relationships-table',
  239. 'effect' => 'fade',
  240. 'method' => 'replace',
  241. 'prevent' => 'click'
  242. ),
  243. // When this button is clicked, the form will be validated and submitted.
  244. // Therefore, we set custom submit and validate functions to override the
  245. // default node form submit. In the validate function we validate only the
  246. // relationship fields and in the submit we remove the indicated relationship
  247. // from the chado_relationships array. In order to keep validate errors
  248. // from the node form validate and Drupal required errors for non-relationship fields
  249. // preventing the user from removing relationships we set the #limit_validation_errors below
  250. '#validate' => array('tripal_api_chado_node_relationships_form_remove_button_validate'),
  251. '#submit' => array('tripal_api_chado_node_relationships_form_remove_button_submit'),
  252. // Limit the validation of the form upon clicking this button to the relationship_table tree
  253. // No other fields will be validated (ie: no fields from the main form or any other api
  254. // added form).
  255. '#limit_validation_errors' => array(
  256. array('relationship_table') // Validate all fields within $form_state['values']['relationship_table']
  257. )
  258. );
  259. }
  260. $form['relationships']['relationship_table']['new']['object_name'] = array(
  261. '#type' => 'textfield',
  262. );
  263. $form['relationships']['relationship_table']['new']['object_is_current'] = array(
  264. '#type' => 'checkbox',
  265. '#title' => t('Current '.$details['nodetype']),
  266. );
  267. $form['relationships']['relationship_table']['new']['type_name'] = array(
  268. '#type' => 'select',
  269. '#options' => $type_options,
  270. );
  271. $form['relationships']['relationship_table']['new']['subject_name'] = array(
  272. '#type' => 'textfield',
  273. );
  274. $form['relationships']['relationship_table']['new']['subject_is_current'] = array(
  275. '#type' => 'checkbox',
  276. '#title' => t('Current '.$details['nodetype']),
  277. );
  278. $form['relationships']['relationship_table']['new']['rank'] = array(
  279. '#type' => 'markup',
  280. '#markup' => ''
  281. );
  282. $form['relationships']['relationship_table']['new']['rel_action'] = array(
  283. '#type' => 'submit',
  284. '#value' => t('Add'),
  285. '#name' => 'rel_add',
  286. '#ajax' => array(
  287. 'callback' => 'tripal_api_chado_node_relationships_form_ajax_update',
  288. 'wrapper' => 'tripal-generic-edit-relationships-table',
  289. 'effect' => 'fade',
  290. 'method' => 'replace',
  291. 'prevent' => 'click'
  292. ),
  293. // When this button is clicked, the form will be validated and submitted.
  294. // Therefore, we set custom submit and validate functions to override the
  295. // default node form submit. In the validate function we validate only the
  296. // relationship fields and in the submit we add them to the chado_relationships
  297. // array. In order to keep validate errors from the node form validate and Drupal
  298. // required errors for non-relationship fields preventing the user from adding relationships we
  299. // set the #limit_validation_errors below
  300. '#validate' => array('tripal_api_chado_node_relationships_form_add_button_validate'),
  301. '#submit' => array('tripal_api_chado_node_relationships_form_add_button_submit'),
  302. // Limit the validation of the form upon clicking this button to the relationship_table tree
  303. // No other fields will be validated (ie: no fields from the main form or any other api
  304. // added form).
  305. '#limit_validation_errors' => array(
  306. array('relationship_table') // Validate all fields within $form_state['values']['relationship_table']
  307. )
  308. );
  309. }
  310. /**
  311. * Validate the user input for creating a new relationship
  312. * Called by the add button in tripal_api_chado_node_relationships_form
  313. *
  314. * @ingroup tripal_relationships_api
  315. */
  316. function tripal_api_chado_node_relationships_form_add_button_validate($form, &$form_state) {
  317. $details = unserialize($form_state['values']['rel_details']);
  318. // At least one of the participants must be the current node
  319. if (!($form_state['values']['relationship_table']['new']['subject_is_current'] OR $form_state['values']['relationship_table']['new']['object_is_current'])) {
  320. // If the checkbox isn't set then check to see if either has the same uniquename as the node
  321. if ($form_state['values']['relationship_table']['new']['subject_name'] == $form_state['values']['uniquename']) {
  322. $form_state['values']['relationship_table']['new']['subject_is_current'] = 1;
  323. form_set_error('subject_is_current', 'To set the current '.$details['nodetype'].', select the
  324. checkbox. You entered the unique name of the current '.$details['nodetype'].' as the subject,
  325. is this what you meant to do?');
  326. }
  327. elseif ($form_state['values']['relationship_table']['new']['subject_name'] == $form_state['values']['uniquename']) {
  328. $form_state['values']['relationship_table']['new']['object_is_current'] = 1;
  329. form_set_error('subject_is_current', 'To set the current '.$details['nodetype'].', select the
  330. checkbox. You entered the unique name of the current '.$details['nodetype'].' as the subject,
  331. is this what you meant to do?');
  332. }
  333. else {
  334. form_set_error('object_is_current', 'At least one member of the relationship must be
  335. the current '.$details['nodetype'].'. This is specified by checking the "Current '.$details['nodetype'].'"
  336. checkbox for either the subject or object.');
  337. }
  338. }
  339. // The non-current uniquename must be exist in the base table (subject)
  340. if (!($form_state['values']['relationship_table']['new']['subject_is_current'])) {
  341. $result = tripal_core_chado_select(
  342. $details['base_table'],
  343. array($details['base_foreign_key']),
  344. array('uniquename' => $form_state['values']['relationship_table']['new']['subject_name'])
  345. );
  346. if (!isset($result[0])) {
  347. form_set_error('subject_name', 'The subject must be the unique name of an
  348. existing '.$details['nodetype'].' unless the "Current '.$details['nodetype'].'" checkbox is selected');
  349. }
  350. else {
  351. $form_state['values']['relationship_table']['new']['subject_id'] = $result[0]->{$details['base_foreign_key']};
  352. }
  353. }
  354. // The non-current uniquename must exist in the base table (object)
  355. if (!($form_state['values']['relationship_table']['new']['object_is_current'])) {
  356. $result = tripal_core_chado_select(
  357. $details['base_table'],
  358. array($details['base_foreign_key']),
  359. array('uniquename' => $form_state['values']['relationship_table']['new']['object_name'])
  360. );
  361. if (!isset($result[0])) {
  362. form_set_error('object_name', 'The object must be the unique name of an
  363. existing '.$details['nodetype'].' unless the "Current '.$details['nodetype'].'" checkbox is selected');
  364. }
  365. else {
  366. $form_state['values']['relationship_table']['new']['object_id'] = $result[0]->{$details['base_foreign_key']};
  367. }
  368. }
  369. // The type must be a valid cvterm
  370. if ($form_state['values']['relationship_table']['new']['type_name']) {
  371. $form_state['values']['relationship_table']['new']['type_id'] = $form_state['values']['relationship_table']['new']['type_name'];
  372. $result = tripal_core_chado_select(
  373. 'cvterm',
  374. array('name'),
  375. array('cvterm_id' => $form_state['values']['relationship_table']['new']['type_id'])
  376. );
  377. if (!isset($result[0])) {
  378. form_set_error('type_id', 'The select type is not a valid controlled vocabulary term.');
  379. }
  380. else {
  381. $form_state['values']['relationship_table']['new']['type_name'] = $result[0]->name;
  382. }
  383. }
  384. else {
  385. form_set_error('type_id', 'Please select a type of relationship');
  386. }
  387. }
  388. /**
  389. * Called by the add button in tripal_api_chado_node_relationships_form
  390. *
  391. * Create an array of additional relationships in the form state. This array will then be
  392. * used to rebuild the form in subsequent builds
  393. *
  394. * @ingroup tripal_relationships_api
  395. */
  396. function tripal_api_chado_node_relationships_form_add_button_submit(&$form, &$form_state) {
  397. $details = unserialize($form_state['values']['rel_details']);
  398. // if the chado_relationships array is not set then this is the first time modifying the
  399. // relationship table. this means we need to include all the relationships from the db
  400. if (!isset($form_state['chado_relationships'])) {
  401. tripal_api_chado_node_relationships_form_create_relationship_formstate_array($form, $form_state);
  402. }
  403. // get details for the new relationship
  404. if ($form_state['values']['relationship_table']['new']['subject_is_current']) {
  405. $relationship = array(
  406. 'type_id' => $form_state['values']['relationship_table']['new']['type_id'],
  407. 'object_id' => $form_state['values']['relationship_table']['new']['object_id'],
  408. 'subject_id' => $form_state['values'][ $details['base_foreign_key'] ],
  409. 'type_name' => $form_state['values']['relationship_table']['new']['type_name'],
  410. 'object_name' => $form_state['values']['relationship_table']['new']['object_name'],
  411. 'subject_name' => $form_state['values']['uniquename'],
  412. 'rank' => '0'
  413. );
  414. }
  415. else {
  416. $relationship = array(
  417. 'type_id' => $form_state['values']['relationship_table']['new']['type_id'],
  418. 'object_id' => $form_state['values'][ $details['base_foreign_key'] ],
  419. 'subject_id' => $form_state['values']['relationship_table']['new']['subject_id'],
  420. 'type_name' => $form_state['values']['relationship_table']['new']['type_name'],
  421. 'object_name' => $form_state['values']['uniquename'],
  422. 'subject_name' => $form_state['values']['relationship_table']['new']['subject_name'],
  423. 'rank' => '0'
  424. );
  425. }
  426. // get max rank
  427. $rank = tripal_core_get_max_chado_rank(
  428. $details['relationship_table'],
  429. array(
  430. 'subject_id' => $relationship['subject_id'],
  431. 'type_id' => $relationship['type_id'],
  432. 'object_id' => $relationship['object_id'],
  433. )
  434. );
  435. $relationship['rank'] = strval($rank + 1);
  436. $key = $relationship['type_id'] . '-' . $relationship['rank'];
  437. $form_state['chado_relationships'][$key] = (object) $relationship;
  438. $form_state['rebuild'] = TRUE;
  439. }
  440. /**
  441. * There is no user input for the remove buttons so there is no need to validate
  442. * However, both a submit & validate need to be specified so this is just a placeholder
  443. *
  444. * Called by the many remove buttons in tripal_api_chado_node_relationships_form
  445. *
  446. * @ingroup tripal_relationships_api
  447. */
  448. function tripal_api_chado_node_relationships_form_remove_button_validate($form, $form_state) {
  449. // No Validation needed for remove
  450. }
  451. /**
  452. * Remove the correct relationship from the form
  453. * Called by the many remove buttons in tripal_api_chado_node_relationships_form
  454. *
  455. * @ingroup tripal_relationships_api
  456. */
  457. function tripal_api_chado_node_relationships_form_remove_button_submit(&$form, &$form_state) {
  458. // if the chado_relationships array is not set then this is the first time modifying the
  459. // relationship table. this means we need to include all the relationships from the db
  460. if (!isset($form_state['chado_relationships'])) {
  461. tripal_api_chado_node_relationships_form_create_relationship_formstate_array($form, $form_state);
  462. }
  463. // @TODO: Test that this actually works
  464. // remove the specified relationship from the form relationship table
  465. if(preg_match('/rel_remove-([^-]+-[^-]+)/',$form_state['triggering_element']['#name'],$match)) {
  466. $key = $match[1];
  467. if (array_key_exists($key, $form_state['chado_relationships'])) {
  468. unset($form_state['chado_relationships'][$key]);
  469. }
  470. }
  471. $form_state['rebuild'] = TRUE;
  472. }
  473. /**
  474. * Ajax function which returns the section of the form to be re-rendered
  475. *
  476. * @ingroup tripal_relationships_api
  477. */
  478. function tripal_api_chado_node_relationships_form_ajax_update($form, $form_state) {
  479. return $form['relationships']['relationship_table'];
  480. }
  481. /**
  482. * Creates an array in form_state containing the existing relationships. This array is
  483. * then modified by the add/remove buttons and used as a source for rebuilding the form.
  484. *
  485. * $form_state['chado_relationships'] = array(
  486. * '[type_id]-[rank]' => array(
  487. * 'object_id' => [the _relationship.object_id value],
  488. * 'object_name' => [the base_table.uniquename value linked on base_foreign_key=object_id],
  489. * 'subject_id' => [the _relationship.subject_id value],
  490. * 'subject_name' => [the base_table.uniquename value linked on base_foreign_key=subject_id],
  491. * 'type_id' => [the _relationship.type_id value],
  492. * 'type_name' => [the cvterm.name value linked on type_id],
  493. * 'rank' => [the _relationship.rank value],
  494. * ),
  495. * );
  496. *
  497. * @ingroup tripal_relationships_api
  498. */
  499. function tripal_api_chado_node_relationships_form_create_relationship_formstate_array($form, &$form_state) {
  500. $form_state['chado_relationships'] = array();
  501. foreach (element_children($form['relationships']['relationship_table']) as $type_id) {
  502. if ($type_id != 'new') {
  503. foreach (element_children($form['relationships']['relationship_table'][$type_id]) as $rank) {
  504. $element = $form['relationships']['relationship_table'][$type_id][$rank];
  505. $rel = array(
  506. 'type_id' => $element['type_id']['#value'],
  507. 'object_id' => $element['object_id']['#value'],
  508. 'subject_id' => $element['subject_id']['#value'],
  509. 'type_name' => $element['type_name']['#markup'],
  510. 'object_name' => $element['object_name']['#markup'],
  511. 'subject_name' => $element['subject_name']['#markup'],
  512. 'rank' => $element['rank']['#markup']
  513. );
  514. $key = $rel['type_id'] . '-' . $rel['rank'];
  515. $form_state['chado_relationships'][$key] = (object) $rel;
  516. }
  517. }
  518. }
  519. }
  520. /**
  521. * Function to theme the add/remove relationships form into a table
  522. *
  523. * @ingroup tripal_relationships_api
  524. */
  525. function theme_tripal_api_chado_node_relationships_form_table($variables) {
  526. $element = $variables['element'];
  527. $header = array(
  528. 'object_name' => t('Object Unique Name'),
  529. 'type_name' => t('Type'),
  530. 'subject_name' => t('Subject Unique Name'),
  531. 'rank' => t('Rank'),
  532. 'rel_action' => t('Action')
  533. );
  534. $rows = array();
  535. foreach (element_children($element) as $type_id) {
  536. if ($type_id == 'new') {
  537. $row = array();
  538. $row['data'] = array();
  539. foreach ($header as $fieldname => $title) {
  540. if ($fieldname == 'subject_name') {
  541. $row['data'][] = drupal_render($element[$type_id][$fieldname]) . drupal_render($element[$type_id]['subject_is_current']);
  542. }
  543. elseif ($fieldname == 'object_name') {
  544. $row['data'][] = drupal_render($element[$type_id][$fieldname]) . drupal_render($element[$type_id]['object_is_current']);
  545. }
  546. else {
  547. $row['data'][] = drupal_render($element[$type_id][$fieldname]);
  548. }
  549. }
  550. $rows[] = $row;
  551. }
  552. else {
  553. foreach (element_children($element[$type_id]) as $rank) {
  554. $row = array();
  555. $row['data'] = array();
  556. foreach ($header as $fieldname => $title) {
  557. $row['data'][] = drupal_render($element[$type_id][$rank][$fieldname]);
  558. }
  559. $rows[] = $row;
  560. }
  561. }
  562. }
  563. return theme('table', array(
  564. 'header' => $header,
  565. 'rows' => $rows
  566. ));
  567. }
  568. /**
  569. * This function is used in a hook_insert, hook_update for a node form
  570. * when the relationships form has been added to the form. It retrieves all of the relationships
  571. * and returns them in an array of the format:
  572. *
  573. * $relationships[<type_id>][<rank>] = array(
  574. * 'subject_id' => <subject_id>,
  575. * 'object_id' => <object_id>,
  576. * );
  577. *
  578. * This array can then be used for inserting or updating relationships manually
  579. *
  580. * @param $node
  581. *
  582. * @return
  583. * A relationship array
  584. *
  585. * @ingroup tripal_relationships_api
  586. */
  587. function tripal_api_chado_node_relationships_form_retreive($node) {
  588. $rels = array();
  589. foreach ($node->rel_table as $type_id => $elements) {
  590. if ($type_id != 'new') {
  591. foreach ($elements as $rank => $relationships) {
  592. $rels[$type_id][$rank]['subject_id'] = $relationships['subject_id'];
  593. $rels[$type_id][$rank]['object_id'] = $relationships['object_id'];
  594. }
  595. }
  596. }
  597. return $rels;
  598. }
  599. /**
  600. * This function is used in hook_insert or hook_update and handles inserting of
  601. * relationships between the current nodetype and other memebers of the same nodetype
  602. *
  603. * @param $node
  604. * The node passed into hook_insert & hook_update
  605. * @param $relationship_table
  606. * The name of the _relationship linking table (ie: feature_relationship)
  607. * @param $current_id
  608. * The value of the foreign key (ie: 445, if there exists a feature where feature_id=445)
  609. *
  610. * @ingroup tripal_relationships_api
  611. */
  612. function tripal_api_chado_node_relationships_form_update_relationships($node, $relationship_table, $current_id) {
  613. // First remove existing relationships links
  614. tripal_core_chado_delete($relationship_table, array('subject_id' => $current_id));
  615. tripal_core_chado_delete($relationship_table, array('object_id' => $current_id));
  616. // Add back in dbxref links and insert dbxrefs as needed
  617. $relationships = tripal_api_chado_node_relationships_form_retreive($node);
  618. foreach ($relationships as $type_id => $ranks) {
  619. foreach ($ranks as $rank => $element) {
  620. // add relationship
  621. $success_link = tripal_core_chado_insert(
  622. $relationship_table,
  623. array(
  624. 'subject_id' => $element['subject_id'],
  625. 'type_id' => $type_id,
  626. 'object_id' => $element['object_id'],
  627. 'rank' => $rank
  628. )
  629. );
  630. }
  631. }
  632. }