tripal_core.chado_nodes.relationships.api.inc 33 KB

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