tripal_core.chado_nodes.relationships.api.inc 30 KB

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