tripal_analysis.chado_node.inc 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  1. <?php
  2. /**
  3. * @file
  4. * Implements Drupal Node hooks to create the chado_analysis node content type.
  5. *
  6. * @ingroup tripal_legacy_analysis
  7. */
  8. /**
  9. * Implements hook_node_info().
  10. *
  11. * Provide information to drupal about the node types that we're creating
  12. * in this module
  13. *
  14. * @ingroup tripal_legacy_analysis
  15. */
  16. function tripal_analysis_node_info() {
  17. $nodes = [];
  18. $nodes['chado_analysis'] = [
  19. 'name' => t('Analysis (Tripal v2 legacy)'),
  20. 'base' => 'chado_analysis',
  21. 'description' => t('An analysis'),
  22. 'has_title' => TRUE,
  23. 'locked' => TRUE,
  24. 'chado_node_api' => [
  25. 'base_table' => 'analysis',
  26. 'hook_prefix' => 'chado_analysis',
  27. 'record_type_title' => [
  28. 'singular' => t('Analysis'),
  29. 'plural' => t('Analyses'),
  30. ],
  31. 'sync_filters' => [
  32. 'type_id' => FALSE,
  33. 'organism_id' => FALSE,
  34. 'checkboxes' => ['name'],
  35. ],
  36. ],
  37. ];
  38. return $nodes;
  39. }
  40. /**
  41. * Implements hook_chado_node_sync_form()
  42. */
  43. function chado_analysis_chado_node_sync_form($form, &$form_state) {
  44. if (array_key_exists('sync', $form) and array_key_exists('ids', $form['sync'])) {
  45. $form['sync']['ids']['#prefix'] .= t('Please note that if analyses exist
  46. that were created by other Tripal modules (e.g. Tripal Analysis Blast)
  47. then you should sync those using that module\'s sync interface. Otherwise
  48. they may not have all of the proper functionality.');
  49. }
  50. return $form;
  51. }
  52. /**
  53. * Implements hook_form().
  54. * When editing or creating a new node of type 'chado_analysis' we need
  55. * a form. This function creates the form that will be used for this.
  56. *
  57. * @ingroup tripal_legacy_analysis
  58. */
  59. function chado_analysis_form($node, &$form_state) {
  60. $form = [];
  61. // Default values can come in the following ways:
  62. //
  63. // 1) as elements of the $node object. This occurs when editing an existing analysis
  64. // 2) in the $form_state['values'] array which occurs on a failed validation or
  65. // ajax callbacks from non submit form elements
  66. // 3) in the $form_state['input'[ array which occurs on ajax callbacks from submit
  67. // form elements and the form is being rebuilt
  68. //
  69. // set form field defaults
  70. $analysis_id = NULL;
  71. $analysisname = '';
  72. $program = '';
  73. $programversion = '';
  74. $algorithm = '';
  75. $sourcename = '';
  76. $sourceversion = '';
  77. $sourceuri = '';
  78. $timeexecuted = '';
  79. $description = '';
  80. $d_removed = []; // lists removed properties
  81. $num_new = 0; // the number of new rows
  82. // if we are editing an existing node then the analysis is already part of the node
  83. if (property_exists($node, 'analysis')) {
  84. $analysis = $node->analysis;
  85. $analysis = chado_expand_var($analysis, 'field', 'analysis.description');
  86. $analysis_id = $analysis->analysis_id;
  87. // get form defaults
  88. $analysisname = $analysis->name;
  89. $program = $analysis->program;
  90. $programversion = $analysis->programversion;
  91. $algorithm = $analysis->algorithm;
  92. $sourcename = $analysis->sourcename;
  93. $sourceversion = $analysis->sourceversion;
  94. $sourceuri = $analysis->sourceuri;
  95. $timeexecuted = $analysis->timeexecuted;
  96. $description = $analysis->description;
  97. $analysis_type = $node->type;
  98. // set the analysis_id in the form
  99. $form['analysis_id'] = [
  100. '#type' => 'value',
  101. '#value' => $analysis->analysis_id,
  102. ];
  103. }
  104. // if we are re constructing the form from a failed validation or ajax callback
  105. // then use the $form_state['values'] values
  106. if (array_key_exists('values', $form_state)) {
  107. $analysisname = $form_state['values']['analysisname'];
  108. $program = $form_state['values']['program'];
  109. $programversion = $form_state['values']['programversion'];
  110. $algorithm = $form_state['values']['algorithm'];
  111. $sourcename = $form_state['values']['sourcename'];
  112. $sourceversion = $form_state['values']['sourceversion'];
  113. $sourceuri = $form_state['values']['sourceuri'];
  114. $timeexecuted = $form_state['values']['timeexecuted'];
  115. $description = $form_state['values']['description'];
  116. $d_removed = $form_state['values']['removed'];
  117. $num_new = $form_state['values']['num_new'] ? $form_state['values']['num_new'] : 0;
  118. $analysis_type = $form_state['values']['analysis_type'];
  119. }
  120. // if we are re building the form from after submission (from ajax call) then
  121. // the values are in the $form_state['input'] array
  122. if (array_key_exists('input', $form_state) and !empty($form_state['input'])) {
  123. $analysisname = $form_state['input']['analysisname'];
  124. $program = $form_state['input']['program'];
  125. $programversion = $form_state['input']['programversion'];
  126. $algorithm = $form_state['input']['algorithm'];
  127. $sourcename = $form_state['input']['sourcename'];
  128. $sourceversion = $form_state['input']['sourceversion'];
  129. $sourceuri = $form_state['input']['sourceuri'];
  130. $timeexecuted = $form_state['input']['timeexecuted'];
  131. $description = $form_state['input']['description'];
  132. $d_removed = isset($form_state['input']['removed']) ? $form_state['input']['removed'] : [];
  133. $num_new = isset($form_state['input']['num_new']) ? $form_state['input']['num_new'] : 0;
  134. $analysis_type = isset($form_state['input']['analysis_type']) ? $form_state['input']['analysis_type'] : '';
  135. }
  136. $form['title'] = [
  137. '#type' => 'value',
  138. '#default_value' => $node->title,
  139. ];
  140. $form['instructions'] = [
  141. '#markup' => t('When adding any type of data it is good to associate it with
  142. an analysis so that site visitors can identify the source of the data including
  143. necessary materials and methods. The fields below imply that all analyses
  144. are derived from some software package. But, data can also be derived via retreival
  145. from an external source or an analysis pipeline with multipel software components.
  146. In these cases, provide values for the fields below that best makes sense
  147. '),
  148. ];
  149. $form['analysisname'] = [
  150. '#type' => 'textfield',
  151. '#title' => t('Analysis Name'),
  152. '#required' => TRUE,
  153. '#default_value' => $analysisname,
  154. '#description' => t("This should be a brief name that
  155. describes the analysis succintly. This name will helps the user find analyses."),
  156. ];
  157. $form['program'] = [
  158. '#type' => 'textfield',
  159. '#title' => t('Program, Pipeline Name or Method Name'),
  160. '#required' => TRUE,
  161. '#default_value' => $program,
  162. '#description' => t("Program name, e.g. blastx, blastp, sim4, genscan. If the analysis was not derived from a software package, provide a very brief description of the pipeline or method."),
  163. ];
  164. $form['programversion'] = [
  165. '#type' => 'textfield',
  166. '#title' => t('Program, Pipeline or Method Version'),
  167. '#required' => TRUE,
  168. '#default_value' => $programversion,
  169. '#description' => t("Version description, e.g. TBLASTX 2.0MP-WashU [09-Nov-2000]. Enter 'n/a' if no version is available or applicable."),
  170. ];
  171. $form['algorithm'] = [
  172. '#type' => 'textfield',
  173. '#title' => t('Algorithm'),
  174. '#required' => FALSE,
  175. '#default_value' => $algorithm,
  176. '#description' => t("Algorithm name, e.g. blast."),
  177. ];
  178. $form['sourcename'] = [
  179. '#type' => 'textfield',
  180. '#title' => t('Source Name'),
  181. '#required' => TRUE,
  182. '#default_value' => $sourcename,
  183. '#description' => t('The name of the source data. This could be a file name, data set name or a
  184. small description for how the data was collected. For long descriptions use the description field below'),
  185. ];
  186. $form['sourceversion'] = [
  187. '#type' => 'textfield',
  188. '#title' => t('Source Version'),
  189. '#required' => FALSE,
  190. '#default_value' => $sourceversion,
  191. '#description' => t('If the source dataset has a version, include it here'),
  192. ];
  193. $form['sourceuri'] = [
  194. '#type' => 'textfield',
  195. '#title' => t('Source URI'),
  196. '#required' => FALSE,
  197. '#default_value' => $sourceuri,
  198. '#description' => t("This is a permanent URL or URI for the source of the analysis.
  199. Someone could recreate the analysis directly by going to this URI and
  200. fetching the source data (e.g. the blast database, or the training model)."),
  201. ];
  202. // Get time saved in chado
  203. $default_time = $timeexecuted;
  204. $year = preg_replace("/^(\d+)-\d+-\d+ .*/", "$1", $default_time);
  205. $month = preg_replace("/^\d+-0?(\d+)-\d+ .*/", "$1", $default_time);
  206. $day = preg_replace("/^\d+-\d+-0?(\d+) .*/", "$1", $default_time);
  207. // If the time is not set, use current time
  208. if (!$default_time) {
  209. $default_time = REQUEST_TIME;
  210. $year = format_date($default_time, 'custom', 'Y');
  211. $month = format_date($default_time, 'custom', 'n');
  212. $day = format_date($default_time, 'custom', 'j');
  213. }
  214. $form['timeexecuted'] = [
  215. '#type' => 'date',
  216. '#title' => t('Time Executed'),
  217. '#required' => TRUE,
  218. '#default_value' => [
  219. 'year' => $year,
  220. 'month' => $month,
  221. 'day' => $day,
  222. ],
  223. ];
  224. $form['description'] = [
  225. '#type' => 'text_format',
  226. '#rows' => 15,
  227. '#title' => t('Materials & Methods (Description and/or Program Settings)'),
  228. '#required' => FALSE,
  229. '#default_value' => $description,
  230. '#description' => t('Please provide all necessary information to allow
  231. someone to recreate the analysis, including materials and methods
  232. for collection of the source data and performing the analysis'),
  233. ];
  234. /*
  235. // get node types from analysis extension modules
  236. $sql = "SELECT modulename FROM {tripal_analysis}";
  237. $modules = db_query($sql);
  238. $node_types = array();
  239. $node_types['chado_analysis'] = 'Analysis';
  240. foreach($modules as $module) {
  241. $mtypes = call_user_func($module->modulename . "_node_info");
  242. foreach ($mtypes as $mtypename => $mtype) {
  243. $node_types[$mtypename] = $mtype['name'];
  244. }
  245. }
  246. if (count($node_types) > 0) {
  247. $form['analysis_type'] = array(
  248. '#title' => t('Analysis Type'),
  249. '#type' => t('select'),
  250. '#description' => t("You can change this analysis type to be any other analysis type currently supported by Tripal. Simply change this value and click 'Save'. Then click 'Edit' again to supply additional values."),
  251. '#required' => TRUE,
  252. '#default_value' => $analysis_type,
  253. '#options' => $node_types,
  254. );
  255. } */
  256. // Properties Form
  257. // ----------------------------------
  258. $instructions = t('To add additional properties to the drop down. ' . l("Add terms to the analysis_property vocabulary", "admin/tripal/loaders/chado_cv/cvterm/add") . ".");
  259. $prop_cv = tripal_get_default_cv('analysisprop', 'type_id');
  260. $cv_id = $prop_cv ? $prop_cv->cv_id : NULL;
  261. $details = [
  262. 'property_table' => 'analysisprop',
  263. // the name of the prop table
  264. 'chado_id' => $analysis_id,
  265. // the value of analysis_id for this record
  266. 'cv_id' => $cv_id,
  267. // the cv.cv_id of the cv governing analysisprop.type_id
  268. 'fieldset_title' => 'Properties',
  269. 'additional_instructions' => $instructions,
  270. ];
  271. chado_add_node_form_properties($form, $form_state, $details);
  272. return $form;
  273. }
  274. /**
  275. * Implements hook_validate().
  276. * Validates the user input before creating an analysis node
  277. *
  278. * @ingroup tripal_legacy_analysis
  279. */
  280. function chado_analysis_validate($node, $form, &$form_state) {
  281. // use the analysis parent to validate the node
  282. tripal_analysis_validate($node, $form, $form_state);
  283. }
  284. /**
  285. * This validation is being used for three activities:
  286. * CASE A: Update a node that exists in both drupal and chado
  287. * CASE B: Synchronizing a node from chado to drupal
  288. * CASE C: Inserting a new node that exists in niether drupal nor chado
  289. *
  290. * @ingroup tripal_legacy_analysis
  291. */
  292. function tripal_analysis_validate($node, $form, &$form_state) {
  293. // We only want to validate when the node is saved.
  294. // Since this validate can be called on AJAX and Deletion of the node
  295. // we need to make this check to ensure queries are not executed
  296. // without the proper values.
  297. if ($node->op != 'Save') {
  298. return;
  299. }
  300. // we are syncing if we do not have a node ID but we do have a analysis_id. We don't
  301. // need to validate during syncing so just skip it.
  302. if (is_null($node->nid) and property_exists($node, 'analysis_id') and $node->analysis_id != 0) {
  303. return;
  304. }
  305. // remove surrounding white-space on submitted values
  306. $node->analysisname = trim($node->analysisname);
  307. $node->program = trim($node->program);
  308. $node->programversion = trim($node->programversion);
  309. $node->algorithm = trim($node->algorithm);
  310. $node->sourcename = trim($node->sourcename);
  311. $node->sourceversion = trim($node->sourceversion);
  312. $node->sourceuri = trim($node->sourceuri);
  313. // Validating for an update
  314. if (!is_null($node->nid)) {
  315. // get the existing node
  316. $values = ['analysis_id' => $node->analysis_id];
  317. $result = chado_select_record('analysis', ['*'], $values);
  318. $analysis = $result[0];
  319. // if the name has changed make sure it doesn't conflict with an existing name
  320. if ($analysis->name != $node->analysisname) {
  321. $values = ['name' => $node->analysisname];
  322. $result = chado_select_record('analysis', ['analysis_id'], $values);
  323. if ($result and count($result) > 0) {
  324. form_set_error('analysisname', 'Cannot update the analysis with this analysis name. An analysis with this name already exists.');
  325. return;
  326. }
  327. }
  328. // if the unique constraint has changed check to make sure it doesn't conflict with an
  329. // existing record
  330. if ($analysis->program != $node->program or $analysis->programversion != $node->programversion or
  331. $analysis->sourcename != $node->sourcename) {
  332. $values = [
  333. 'program' => $node->program,
  334. 'programversion' => $node->programversion,
  335. 'sourcename' => $node->sourcename,
  336. ];
  337. $result = chado_select_record('analysis', ['analysis_id'], $values);
  338. if ($result and count($result) > 0) {
  339. if ($analysis->program != $node->program) {
  340. $field = 'program';
  341. }
  342. if ($analysis->programversion != $node->programversion) {
  343. $field = 'programversion';
  344. }
  345. if ($analysis->sourcename != $node->sourcename) {
  346. $field = 'sourcename';
  347. }
  348. form_set_error($field, 'Cannot update the analysis with this program,
  349. program version and source name. An analysis with these values already exists.');
  350. return;
  351. }
  352. }
  353. }
  354. // Validating for an insert
  355. else {
  356. $values = [
  357. 'program' => $node->program,
  358. 'programversion' => $node->programversion,
  359. 'sourcename' => $node->sourcename,
  360. ];
  361. $analysis = chado_select_record('analysis', ['analysis_id'], $values);
  362. if ($analysis and count($analysis) > 0) {
  363. form_set_error('program', 'Cannot add the analysis with this program,
  364. program version and source name. An analysis with these values already exists.');
  365. return;
  366. }
  367. // make sure we have a unique analysis name. This is not a requirement
  368. // for the analysis table but we use the analysis name for the Drupal node
  369. // title, so it should be unique
  370. $values = ['name' => $node->analysisname];
  371. $result = chado_select_record('analysis', ['analysis_id'], $values);
  372. if ($result and count($result) > 0) {
  373. form_set_error('analysisname', 'Cannot add the analysis with this analysis name. An analysis with this name already exists.');
  374. return;
  375. }
  376. }
  377. }
  378. /**
  379. * Implements hook_insert().
  380. * When a new chado_analysis node is created we also need to add information
  381. * to our chado_analysis table. This function is called on insert of a new
  382. * node of type 'chado_analysis' and inserts the necessary information.
  383. *
  384. * @ingroup tripal_legacy_analysis
  385. */
  386. function chado_analysis_insert($node) {
  387. $node->analysisname = trim($node->analysisname);
  388. $node->program = trim($node->program);
  389. $node->programversion = trim($node->programversion);
  390. $node->algorithm = trim($node->algorithm);
  391. $node->sourcename = trim($node->sourcename);
  392. $node->sourceversion = trim($node->sourceversion);
  393. $node->sourceuri = trim($node->sourceuri);
  394. $node->description = trim($node->description['value']);
  395. // if there is an analysis_id in the $node object then this must be a sync so
  396. // we can skip adding the analysis as it is already there, although
  397. // we do need to proceed with the rest of the insert
  398. if (!property_exists($node, 'analysis_id')) {
  399. // Create a timestamp so we can insert it into the chado database
  400. $time = $node->timeexecuted;
  401. $month = $time['month'];
  402. $day = $time['day'];
  403. $year = $time['year'];
  404. $timestamp = $month . '/' . $day . '/' . $year;
  405. // Insert and then get the newly inserted analysis record
  406. $values = [
  407. 'name' => $node->analysisname,
  408. 'description' => $node->description,
  409. 'program' => $node->program,
  410. 'programversion' => $node->programversion,
  411. 'algorithm' => $node->algorithm,
  412. 'sourcename' => $node->sourcename,
  413. 'sourceversion' => $node->sourceversion,
  414. 'sourceuri' => $node->sourceuri,
  415. 'timeexecuted' => $timestamp,
  416. ];
  417. $analysis = chado_insert_record('analysis', $values);
  418. if (!$analysis) {
  419. drupal_set_message(t('Unable to add analysis.'), 'warning');
  420. tripal_report_error('tripal_analysis', TRIPAL_ERROR, 'Insert analysis: Unable to create analysis where values:%values',
  421. ['%values' => print_r($values, TRUE)]);
  422. return;
  423. }
  424. $analysis_id = $analysis['analysis_id'];
  425. // now add in the properties
  426. $details = [
  427. 'property_table' => 'analysisprop',
  428. 'base_table' => 'analysis',
  429. 'foreignkey_name' => 'analysis_id',
  430. 'foreignkey_value' => $analysis_id,
  431. ];
  432. chado_update_node_form_properties($node, $details);
  433. }
  434. else {
  435. $analysis_id = $node->analysis_id;
  436. }
  437. // Make sure the entry for this analysis doesn't already exist in the
  438. // chado_analysis table if it doesn't exist then we want to add it.
  439. $check_org_id = chado_get_id_from_nid('analysis', $node->nid);
  440. if (!$check_org_id) {
  441. $record = new stdClass();
  442. $record->nid = $node->nid;
  443. $record->vid = $node->vid;
  444. $record->analysis_id = $analysis_id;
  445. drupal_write_record('chado_analysis', $record);
  446. }
  447. // add the analysis to the node object for
  448. // use by other analysis modules that may be using this function
  449. $node->analysis = $analysis;
  450. $node->analysis_id = $analysis_id; // we need to set this for children
  451. }
  452. /**
  453. * Implements hook_delete().
  454. * Removes analysis from the chado database.
  455. *
  456. * @ingroup tripal_legacy_analysis
  457. */
  458. function chado_analysis_delete($node) {
  459. $analysis_id = chado_get_id_from_nid('analysis', $node->nid);
  460. // if we don't have an analysis id for this node then this isn't a node of
  461. // type chado_analysis or the entry in the chado_analysis table was lost.
  462. if (!$analysis_id) {
  463. return;
  464. }
  465. // Remove data from the {chado_analysis}, {node}, and {node_revisions} tables
  466. $sql_del = "DELETE FROM {chado_analysis} WHERE nid = :nid AND vid = :vid";
  467. db_query($sql_del, [':nid' => $node->nid, ':vid' => $node->vid]);
  468. $sql_del = "DELETE FROM {node} WHERE nid = :nid AND vid = :vid";
  469. db_query($sql_del, [':nid' => $node->nid, ':vid' => $node->vid]);
  470. $sql_del = "DELETE FROM {node_revision} WHERE nid = :nid AND vid = :vid";
  471. db_query($sql_del, [':nid' => $node->nid, ':vid' => $node->vid]);
  472. //Remove from analysis and analysisprop tables of chado database as well
  473. chado_query("DELETE FROM {analysis} WHERE analysis_id = :analysis_id", [':analysis_id' => $analysis_id]);
  474. }
  475. /**
  476. * Implements hook_update().
  477. * Update analyses
  478. *
  479. * @ingroup tripal_legacy_analysis
  480. */
  481. function chado_analysis_update($node) {
  482. $node->analysisname = trim($node->analysisname);
  483. $node->program = trim($node->program);
  484. $node->programversion = trim($node->programversion);
  485. $node->algorithm = trim($node->algorithm);
  486. $node->sourcename = trim($node->sourcename);
  487. $node->sourceversion = trim($node->sourceversion);
  488. $node->sourceuri = trim($node->sourceuri);
  489. $node->description = trim($node->description['value']);
  490. // Create a timestamp so we can insert it into the chado database
  491. $time = $node->timeexecuted;
  492. $month = $time['month'];
  493. $day = $time['day'];
  494. $year = $time['year'];
  495. $timestamp = $month . '/' . $day . '/' . $year;
  496. // update the record in Chado
  497. $analysis_id = chado_get_id_from_nid('analysis', $node->nid);
  498. $match = [
  499. 'analysis_id' => $node->analysis_id,
  500. ];
  501. $values = [
  502. 'name' => $node->analysisname,
  503. 'description' => $node->description,
  504. 'program' => $node->program,
  505. 'programversion' => $node->programversion,
  506. 'algorithm' => $node->algorithm,
  507. 'sourcename' => $node->sourcename,
  508. 'sourceversion' => $node->sourceversion,
  509. 'sourceuri' => $node->sourceuri,
  510. 'timeexecuted' => $timestamp,
  511. 'analysis_id' => $analysis_id,
  512. ];
  513. $status = chado_update_record('analysis', $match, $values);
  514. if (!$status) {
  515. drupal_set_message(t('Unable to update analysis.', 'warning'));
  516. tripal_report_error('tripal_analysis', TRIPAL_ERROR, 'Update analysis: Unable to update analysis where values: %values',
  517. ['%values' => print_r($values, TRUE)]);
  518. }
  519. // now add in the properties by first removing any the analysis
  520. // already has and adding the ones we have
  521. $details = [
  522. 'property_table' => 'analysisprop',
  523. 'base_table' => 'analysis',
  524. 'foreignkey_name' => 'analysis_id',
  525. 'foreignkey_value' => $analysis_id,
  526. ];
  527. chado_update_node_form_properties($node, $details);
  528. }
  529. /**
  530. * Implements hook_load().
  531. * When a node is requested by the user this function is called to allow us
  532. * to add auxiliary data to the node object.
  533. *
  534. * @ingroup tripal_legacy_analysis
  535. */
  536. function chado_analysis_load($nodes) {
  537. foreach ($nodes as $nid => $node) {
  538. // find the analysis and add in the details
  539. $analysis_id = chado_get_id_from_nid('analysis', $nid);
  540. // if the nid does not have a matching record then skip this node.
  541. // this can happen with orphaned nodes.
  542. if (!$analysis_id) {
  543. continue;
  544. }
  545. // build the analysis variable
  546. $values = ['analysis_id' => $analysis_id];
  547. $analysis = chado_generate_var('analysis', $values);
  548. // add in the description field
  549. $analysis = chado_expand_var($analysis, 'field', 'analysis.description');
  550. $nodes[$nid]->analysis = $analysis;
  551. // Now get the title
  552. $node->title = chado_get_node_title($node);
  553. }
  554. }
  555. /**
  556. * Implements hook_access().
  557. *
  558. * This hook allows node modules to limit access to the node types they define.
  559. *
  560. * @param $node
  561. * The node on which the operation is to be performed, or, if it does not yet
  562. * exist, the type of node to be created
  563. *
  564. * @param $op
  565. * The operation to be performed
  566. *
  567. * @param $account
  568. * A user object representing the user for whom the operation is to be
  569. * performed
  570. *
  571. * @return
  572. * If the permission for the specified operation is not set then return FALSE.
  573. * If the permission is set then return NULL as this allows other modules to
  574. * disable access. The only exception is when the $op == 'create'. We will
  575. * always return TRUE if the permission is set.
  576. *
  577. * @ingroup tripal_legacy_analysis
  578. */
  579. function tripal_analysis_node_access($node, $op, $account) {
  580. $node_type = $node;
  581. if (is_object($node)) {
  582. $node_type = $node->type;
  583. }
  584. if ($node_type == 'chado_analysis') {
  585. if ($op == 'create') {
  586. if (!user_access('create chado_analysis content', $account)) {
  587. return NODE_ACCESS_DENY;
  588. }
  589. return NODE_ACCESS_ALLOW;
  590. }
  591. if ($op == 'update') {
  592. if (!user_access('edit chado_analysis content', $account)) {
  593. return NODE_ACCESS_DENY;
  594. }
  595. }
  596. if ($op == 'delete') {
  597. if (!user_access('delete chado_analysis content', $account)) {
  598. return NODE_ACCESS_DENY;
  599. }
  600. }
  601. if ($op == 'view') {
  602. if (!user_access('access chado_analysis content', $account)) {
  603. return NODE_ACCESS_DENY;
  604. }
  605. }
  606. return NODE_ACCESS_IGNORE;
  607. }
  608. }
  609. /**
  610. * Implements hook_node_view().
  611. * Called for all node types.
  612. *
  613. * @ingroup tripal_legacy_analysis
  614. */
  615. function tripal_analysis_node_view($node, $view_mode, $langcode) {
  616. switch ($node->type) {
  617. case 'chado_analysis':
  618. // Show feature browser and counts
  619. if ($view_mode == 'full') {
  620. $node->content['tripal_analysis_base'] = [
  621. '#theme' => 'tripal_analysis_base',
  622. '#node' => $node,
  623. '#tripal_toc_id' => 'base',
  624. '#tripal_toc_title' => 'Overview',
  625. '#weight' => -100,
  626. ];
  627. $node->content['tripal_analysis_properties'] = [
  628. '#theme' => 'tripal_analysis_properties',
  629. '#node' => $node,
  630. '#tripal_toc_id' => 'properties',
  631. '#tripal_toc_title' => 'Properties',
  632. ];
  633. }
  634. if ($view_mode == 'teaser') {
  635. $node->content['tripal_analysis_teaser'] = [
  636. '#theme' => 'tripal_analysis_teaser',
  637. '#node' => $node,
  638. ];
  639. }
  640. break;
  641. }
  642. }
  643. /**
  644. * Implements hook_node_presave().
  645. * Called for all node types.
  646. *
  647. * @ingroup tripal_legacy_analysis
  648. */
  649. function tripal_analysis_node_presave($node) {
  650. $name = '';
  651. $program = '';
  652. $programversion = '';
  653. $sourcename = '';
  654. // This step is for setting the title for the Drupal node. This title
  655. // is permanent and thus is created to be unique. Title changes provided
  656. // by tokens are generated on the fly dynamically, but the node title
  657. // seen in the content listing needs to be set here. Do not call
  658. // the chado_get_node_title() function here to set the title as the node
  659. // object isn't properly filled out and the function will fail.
  660. // If this is an analysis of some type it will should have three required
  661. // fields for the Chado analysis table: program, programversion and sourcename.
  662. // So we will set the title for any node that has these three fields. Some extension
  663. // modules will use this module as a type of "inherited" class, so we don't know
  664. // for sure when type of analysis we have. If this is a sync then
  665. if (property_exists($node, 'program') and
  666. property_exists($node, 'programversion') and
  667. property_exists($node, 'sourcename')) {
  668. $name = $node->analysisname;
  669. $program = $node->program;
  670. $programversion = $node->programversion;
  671. $sourcename = $node->sourcename;
  672. // now construct the title
  673. $node->title = "$program ($programversion) $sourcename";
  674. if ($name) {
  675. $node->title = $name;
  676. }
  677. // reset the type
  678. //$node->type = $node->analysis_type;
  679. }
  680. else {
  681. if (property_exists($node, 'analysis')) {
  682. $name = $node->analysis->name;
  683. $program = $node->analysis->program;
  684. $programversion = $node->analysis->programversion;
  685. $sourcename = $node->analysis->sourcename;
  686. // now construct the title
  687. $node->title = "$program ($programversion) $sourcename";
  688. if ($name) {
  689. $node->title = $name;
  690. }
  691. //$node->type = $node->analysis_type;
  692. }
  693. }
  694. }
  695. /**
  696. * Implements hook_node_insert().
  697. * Acts on all content types.
  698. *
  699. * @ingroup tripal_legacy_analysis
  700. */
  701. function tripal_analysis_node_insert($node) {
  702. switch ($node->type) {
  703. case 'chado_analysis':
  704. // We still don't have a fully loaded node object in this hook. Therefore,
  705. // we need to simulate one so that the right values are available for
  706. // the URL to be determined.
  707. $analysis_id = chado_get_id_from_nid('analysis', $node->nid);
  708. $values = ['analysis_id' => $analysis_id];
  709. $analysis = chado_generate_var('analysis', $values);
  710. $node->analysis = $analysis;
  711. // Now get the title
  712. $node->title = chado_get_node_title($node);
  713. // Now use the API to set the path.
  714. chado_set_node_url($node);
  715. break;
  716. }
  717. }
  718. /**
  719. * Implements hook_node_update().
  720. * Acts on all content types.
  721. *
  722. * @ingroup tripal_legacy_analysis
  723. */
  724. function tripal_analysis_node_update($node) {
  725. switch ($node->type) {
  726. case 'chado_analysis':
  727. // Now get the title
  728. $node->title = chado_get_node_title($node);
  729. // Now use the API to set the path.
  730. chado_set_node_url($node);
  731. break;
  732. }
  733. }
  734. /**
  735. * Implements [content_type]_chado_node_default_title_format().
  736. *
  737. * Defines a default title format for the Chado Node API to set the titles on
  738. * Chado Analysis nodes based on chado fields.
  739. */
  740. function chado_analysis_chado_node_default_title_format() {
  741. return '[analysis.name]';
  742. }
  743. /**
  744. * Implements hook_chado_node_default_url_format().
  745. *
  746. * Designates a default URL format for analysis nodes.
  747. */
  748. function chado_analysis_chado_node_default_url_format() {
  749. return '/analysis/[analysis.analysis_id]';
  750. }