tripal_core.chado_nodes.relationships.api.inc 31 KB

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