tripal_analysis.chado_node.inc 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809
  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 = array();
  18. $nodes['chado_analysis'] = array(
  19. 'name' => t('Analysis'),
  20. 'base' => 'chado_analysis',
  21. 'description' => t('An analysis'),
  22. 'has_title' => TRUE,
  23. 'locked' => TRUE,
  24. 'chado_node_api' => array(
  25. 'base_table' => 'analysis',
  26. 'hook_prefix' => 'chado_analysis',
  27. 'record_type_title' => array(
  28. 'singular' => t('Analysis'),
  29. 'plural' => t('Analyses')
  30. ),
  31. 'sync_filters' => array(
  32. 'type_id' => FALSE,
  33. 'organism_id' => FALSE,
  34. 'checkboxes' => array('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 = array();
  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 = array(); // 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'] = array(
  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'] : array();
  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']= array(
  137. '#type' => 'value',
  138. '#default_value' => $node->title,
  139. );
  140. $form['instructions'] = array(
  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']= array(
  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']= array(
  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']= array(
  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']= array(
  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']= array(
  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']= array(
  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']= array(
  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']= array(
  215. '#type' => 'date',
  216. '#title' => t('Time Executed'),
  217. '#required' => TRUE,
  218. '#default_value' => array(
  219. 'year' => $year,
  220. 'month' => $month,
  221. 'day' => $day,
  222. ),
  223. );
  224. $form['description']= array(
  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 = array(
  262. 'property_table' => 'analysisprop', // the name of the prop table
  263. 'chado_id' => $analysis_id, // the value of analysis_id for this record
  264. 'cv_id' => $cv_id, // the cv.cv_id of the cv governing analysisprop.type_id
  265. 'fieldset_title' => 'Properties',
  266. 'additional_instructions' => $instructions
  267. );
  268. chado_add_node_form_properties($form, $form_state, $details);
  269. return $form;
  270. }
  271. /**
  272. * Implements hook_validate().
  273. * Validates the user input before creating an analysis node
  274. *
  275. * @ingroup tripal_legacy_analysis
  276. */
  277. function chado_analysis_validate($node, $form, &$form_state) {
  278. // use the analysis parent to validate the node
  279. tripal_analysis_validate($node, $form, $form_state);
  280. }
  281. /**
  282. * This validation is being used for three activities:
  283. * CASE A: Update a node that exists in both drupal and chado
  284. * CASE B: Synchronizing a node from chado to drupal
  285. * CASE C: Inserting a new node that exists in niether drupal nor chado
  286. *
  287. * @ingroup tripal_legacy_analysis
  288. */
  289. function tripal_analysis_validate($node, $form, &$form_state) {
  290. // We only want to validate when the node is saved.
  291. // Since this validate can be called on AJAX and Deletion of the node
  292. // we need to make this check to ensure queries are not executed
  293. // without the proper values.
  294. if($node->op != 'Save') {
  295. return;
  296. }
  297. // we are syncing if we do not have a node ID but we do have a analysis_id. We don't
  298. // need to validate during syncing so just skip it.
  299. if (is_null($node->nid) and property_exists($node, 'analysis_id') and $node->analysis_id != 0) {
  300. return;
  301. }
  302. // remove surrounding white-space on submitted values
  303. $node->analysisname = trim($node->analysisname);
  304. $node->program = trim($node->program);
  305. $node->programversion = trim($node->programversion);
  306. $node->algorithm = trim($node->algorithm);
  307. $node->sourcename = trim($node->sourcename);
  308. $node->sourceversion = trim($node->sourceversion);
  309. $node->sourceuri = trim($node->sourceuri);
  310. // Validating for an update
  311. if (!is_null($node->nid)) {
  312. // get the existing node
  313. $values = array('analysis_id' => $node->analysis_id);
  314. $result = chado_select_record('analysis', array('*'), $values);
  315. $analysis = $result[0];
  316. // if the name has changed make sure it doesn't conflict with an existing name
  317. if ($analysis->name != $node->analysisname) {
  318. $values = array('name' => $node->analysisname);
  319. $result = chado_select_record('analysis', array('analysis_id'), $values);
  320. if ($result and count($result) > 0) {
  321. form_set_error('analysisname', 'Cannot update the analysis with this analysis name. An analysis with this name already exists.');
  322. return;
  323. }
  324. }
  325. // if the unique constraint has changed check to make sure it doesn't conflict with an
  326. // existing record
  327. if ($analysis->program != $node->program or $analysis->programversion != $node->programversion or
  328. $analysis->sourcename != $node->sourcename) {
  329. $values = array(
  330. 'program' => $node->program,
  331. 'programversion' => $node->programversion,
  332. 'sourcename' => $node->sourcename,
  333. );
  334. $result = chado_select_record('analysis', array('analysis_id'), $values);
  335. if ($result and count($result) > 0) {
  336. if ($analysis->program != $node->program) {
  337. $field = 'program';
  338. }
  339. if ($analysis->programversion != $node->programversion) {
  340. $field = 'programversion';
  341. }
  342. if ($analysis->sourcename != $node->sourcename) {
  343. $field = 'sourcename';
  344. }
  345. form_set_error($field, 'Cannot update the analysis with this program,
  346. program version and source name. An analysis with these values already exists.');
  347. return;
  348. }
  349. }
  350. }
  351. // Validating for an insert
  352. else {
  353. $values = array(
  354. 'program' => $node->program,
  355. 'programversion' => $node->programversion,
  356. 'sourcename' => $node->sourcename,
  357. );
  358. $analysis = chado_select_record('analysis', array('analysis_id'), $values);
  359. if ($analysis and count($analysis) > 0) {
  360. form_set_error('program', 'Cannot add the analysis with this program,
  361. program version and source name. An analysis with these values already exists.');
  362. return;
  363. }
  364. // make sure we have a unique analysis name. This is not a requirement
  365. // for the analysis table but we use the analysis name for the Drupal node
  366. // title, so it should be unique
  367. $values = array('name' => $node->analysisname);
  368. $result = chado_select_record('analysis', array('analysis_id'), $values);
  369. if ($result and count($result) > 0) {
  370. form_set_error('analysisname', 'Cannot add the analysis with this analysis name. An analysis with this name already exists.');
  371. return;
  372. }
  373. }
  374. }
  375. /**
  376. * Implements hook_insert().
  377. * When a new chado_analysis node is created we also need to add information
  378. * to our chado_analysis table. This function is called on insert of a new
  379. * node of type 'chado_analysis' and inserts the necessary information.
  380. *
  381. * @ingroup tripal_legacy_analysis
  382. */
  383. function chado_analysis_insert($node) {
  384. $node->analysisname = trim($node->analysisname);
  385. $node->program = trim($node->program);
  386. $node->programversion = trim($node->programversion);
  387. $node->algorithm = trim($node->algorithm);
  388. $node->sourcename = trim($node->sourcename);
  389. $node->sourceversion = trim($node->sourceversion);
  390. $node->sourceuri = trim($node->sourceuri);
  391. $node->description = trim($node->description['value']);
  392. // if there is an analysis_id in the $node object then this must be a sync so
  393. // we can skip adding the analysis as it is already there, although
  394. // we do need to proceed with the rest of the insert
  395. if (!property_exists($node, 'analysis_id')) {
  396. // Create a timestamp so we can insert it into the chado database
  397. $time = $node->timeexecuted;
  398. $month = $time['month'];
  399. $day = $time['day'];
  400. $year = $time['year'];
  401. $timestamp = $month . '/' . $day . '/' . $year;
  402. // Insert and then get the newly inserted analysis record
  403. $values = array(
  404. 'name' => $node->analysisname,
  405. 'description' => $node->description,
  406. 'program' => $node->program,
  407. 'programversion' => $node->programversion,
  408. 'algorithm' => $node->algorithm,
  409. 'sourcename' => $node->sourcename,
  410. 'sourceversion' => $node->sourceversion,
  411. 'sourceuri' => $node->sourceuri,
  412. 'timeexecuted' => $timestamp
  413. );
  414. $analysis = chado_insert_record('analysis', $values);
  415. if (!$analysis) {
  416. drupal_set_message(t('Unable to add analysis.'), 'warning');
  417. tripal_report_error('tripal_analysis', TRIPAL_ERROR, 'Insert analysis: Unable to create analysis where values:%values',
  418. array('%values' => print_r($values, TRUE)));
  419. return;
  420. }
  421. $analysis_id = $analysis['analysis_id'];
  422. // now add in the properties
  423. $details = array(
  424. 'property_table' => 'analysisprop',
  425. 'base_table' => 'analysis',
  426. 'foreignkey_name' => 'analysis_id',
  427. 'foreignkey_value' => $analysis_id
  428. );
  429. chado_update_node_form_properties($node, $details);
  430. }
  431. else {
  432. $analysis_id = $node->analysis_id;
  433. }
  434. // Make sure the entry for this analysis doesn't already exist in the
  435. // chado_analysis table if it doesn't exist then we want to add it.
  436. $check_org_id = chado_get_id_from_nid('analysis', $node->nid);
  437. if (!$check_org_id) {
  438. $record = new stdClass();
  439. $record->nid = $node->nid;
  440. $record->vid = $node->vid;
  441. $record->analysis_id = $analysis_id;
  442. drupal_write_record('chado_analysis', $record);
  443. }
  444. // add the analysis to the node object for
  445. // use by other analysis modules that may be using this function
  446. $node->analysis = $analysis;
  447. $node->analysis_id = $analysis_id; // we need to set this for children
  448. }
  449. /**
  450. * Implements hook_delete().
  451. * Removes analysis from the chado database.
  452. *
  453. * @ingroup tripal_legacy_analysis
  454. */
  455. function chado_analysis_delete($node) {
  456. $analysis_id = chado_get_id_from_nid('analysis', $node->nid);
  457. // if we don't have an analysis id for this node then this isn't a node of
  458. // type chado_analysis or the entry in the chado_analysis table was lost.
  459. if (!$analysis_id) {
  460. return;
  461. }
  462. // Remove data from the {chado_analysis}, {node}, and {node_revisions} tables
  463. $sql_del = "DELETE FROM {chado_analysis} WHERE nid = :nid AND vid = :vid";
  464. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  465. $sql_del = "DELETE FROM {node} WHERE nid = :nid AND vid = :vid";
  466. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  467. $sql_del = "DELETE FROM {node_revision} WHERE nid = :nid AND vid = :vid";
  468. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  469. //Remove from analysis and analysisprop tables of chado database as well
  470. chado_query("DELETE FROM {analysis} WHERE analysis_id = :analysis_id", array(':analysis_id' => $analysis_id));
  471. }
  472. /**
  473. * Implements hook_update().
  474. * Update analyses
  475. *
  476. * @ingroup tripal_legacy_analysis
  477. */
  478. function chado_analysis_update($node) {
  479. $node->analysisname = trim($node->analysisname);
  480. $node->program = trim($node->program);
  481. $node->programversion = trim($node->programversion);
  482. $node->algorithm = trim($node->algorithm);
  483. $node->sourcename = trim($node->sourcename);
  484. $node->sourceversion = trim($node->sourceversion);
  485. $node->sourceuri = trim($node->sourceuri);
  486. $node->description = trim($node->description['value']);
  487. // Create a timestamp so we can insert it into the chado database
  488. $time = $node->timeexecuted;
  489. $month = $time['month'];
  490. $day = $time['day'];
  491. $year = $time['year'];
  492. $timestamp = $month . '/' . $day . '/' . $year;
  493. // update the record in Chado
  494. $analysis_id = chado_get_id_from_nid('analysis', $node->nid);
  495. $match = array(
  496. 'analysis_id' => $node->analysis_id,
  497. );
  498. $values = array(
  499. 'name' => $node->analysisname,
  500. 'description' => $node->description,
  501. 'program' => $node->program,
  502. 'programversion' => $node->programversion,
  503. 'algorithm' => $node->algorithm,
  504. 'sourcename' => $node->sourcename,
  505. 'sourceversion' => $node->sourceversion,
  506. 'sourceuri' => $node->sourceuri,
  507. 'timeexecuted' => $timestamp,
  508. 'analysis_id' => $analysis_id
  509. );
  510. $status = chado_update_record('analysis', $match, $values);
  511. if (!$status) {
  512. drupal_set_message(t('Unable to update analysis.', 'warning'));
  513. tripal_report_error('tripal_analysis', TRIPAL_ERROR, 'Update analysis: Unable to update analysis where values: %values',
  514. array('%values' => print_r($values, TRUE)));
  515. }
  516. // now add in the properties by first removing any the analysis
  517. // already has and adding the ones we have
  518. $details = array(
  519. 'property_table' => 'analysisprop',
  520. 'base_table' => 'analysis',
  521. 'foreignkey_name' => 'analysis_id',
  522. 'foreignkey_value' => $analysis_id
  523. );
  524. chado_update_node_form_properties($node, $details);
  525. }
  526. /**
  527. * Implements hook_load().
  528. * When a node is requested by the user this function is called to allow us
  529. * to add auxiliary data to the node object.
  530. *
  531. * @ingroup tripal_legacy_analysis
  532. */
  533. function chado_analysis_load($nodes) {
  534. foreach ($nodes as $nid => $node) {
  535. // find the analysis and add in the details
  536. $analysis_id = chado_get_id_from_nid('analysis', $nid);
  537. // if the nid does not have a matching record then skip this node.
  538. // this can happen with orphaned nodes.
  539. if (!$analysis_id) {
  540. continue;
  541. }
  542. // build the analysis variable
  543. $values = array('analysis_id' => $analysis_id);
  544. $analysis = chado_generate_var('analysis', $values);
  545. // add in the description field
  546. $analysis = chado_expand_var($analysis, 'field', 'analysis.description');
  547. $nodes[$nid]->analysis = $analysis;
  548. // Now get the title
  549. $node->title = chado_get_node_title($node);
  550. }
  551. }
  552. /**
  553. * Implements hook_access().
  554. *
  555. * This hook allows node modules to limit access to the node types they define.
  556. *
  557. * @param $node
  558. * The node on which the operation is to be performed, or, if it does not yet exist, the
  559. * type of node to be created
  560. *
  561. * @param $op
  562. * The operation to be performed
  563. *
  564. * @param $account
  565. * A user object representing the user for whom the operation is to be performed
  566. *
  567. * @return
  568. * If the permission for the specified operation is not set then return FALSE. If the
  569. * permission is set then return NULL as this allows other modules to disable
  570. * access. The only exception is when the $op == 'create'. We will always
  571. * return TRUE if the permission is set.
  572. *
  573. * @ingroup tripal_legacy_analysis
  574. */
  575. function tripal_analysis_node_access($node, $op, $account) {
  576. $node_type = $node;
  577. if (is_object($node)) {
  578. $node_type = $node->type;
  579. }
  580. if($node_type == 'chado_analysis') {
  581. if ($op == 'create') {
  582. if (!user_access('create chado_analysis content', $account)) {
  583. return NODE_ACCESS_DENY;
  584. }
  585. return NODE_ACCESS_ALLOW;
  586. }
  587. if ($op == 'update') {
  588. if (!user_access('edit chado_analysis content', $account)) {
  589. return NODE_ACCESS_DENY;
  590. }
  591. }
  592. if ($op == 'delete') {
  593. if (!user_access('delete chado_analysis content', $account)) {
  594. return NODE_ACCESS_DENY;
  595. }
  596. }
  597. if ($op == 'view') {
  598. if (!user_access('access chado_analysis content', $account)) {
  599. return NODE_ACCESS_DENY;
  600. }
  601. }
  602. return NODE_ACCESS_IGNORE;
  603. }
  604. }
  605. /**
  606. * Implements hook_node_view().
  607. * Called for all node types.
  608. *
  609. * @ingroup tripal_legacy_analysis
  610. */
  611. function tripal_analysis_node_view($node, $view_mode, $langcode) {
  612. switch ($node->type) {
  613. case 'chado_analysis':
  614. // Show feature browser and counts
  615. if ($view_mode == 'full') {
  616. $node->content['tripal_analysis_base'] = array(
  617. '#theme' => 'tripal_analysis_base',
  618. '#node' => $node,
  619. '#tripal_toc_id' => 'base',
  620. '#tripal_toc_title' => 'Overview',
  621. '#weight' => -100,
  622. );
  623. $node->content['tripal_analysis_properties'] = array(
  624. '#theme' => 'tripal_analysis_properties',
  625. '#node' => $node,
  626. '#tripal_toc_id' => 'properties',
  627. '#tripal_toc_title' => 'Properties',
  628. );
  629. }
  630. if ($view_mode == 'teaser') {
  631. $node->content['tripal_analysis_teaser'] = array(
  632. '#theme' => 'tripal_analysis_teaser',
  633. '#node' => $node,
  634. );
  635. }
  636. break;
  637. }
  638. }
  639. /**
  640. * Implements hook_node_presave().
  641. * Called for all node types.
  642. *
  643. * @ingroup tripal_legacy_analysis
  644. */
  645. function tripal_analysis_node_presave($node) {
  646. $name = '';
  647. $program = '';
  648. $programversion = '';
  649. $sourcename = '';
  650. // This step is for setting the title for the Drupal node. This title
  651. // is permanent and thus is created to be unique. Title changes provided
  652. // by tokens are generated on the fly dynamically, but the node title
  653. // seen in the content listing needs to be set here. Do not call
  654. // the chado_get_node_title() function here to set the title as the node
  655. // object isn't properly filled out and the function will fail.
  656. // If this is an analysis of some type it will should have three required
  657. // fields for the Chado analysis table: program, programversion and sourcename.
  658. // So we will set the title for any node that has these three fields. Some extension
  659. // modules will use this module as a type of "inherited" class, so we don't know
  660. // for sure when type of analysis we have. If this is a sync then
  661. if (property_exists($node, 'program') and
  662. property_exists($node, 'programversion') and
  663. property_exists($node, 'sourcename')) {
  664. $name = $node->analysisname;
  665. $program = $node->program;
  666. $programversion = $node->programversion;
  667. $sourcename = $node->sourcename;
  668. // now construct the title
  669. $node->title = "$program ($programversion) $sourcename";
  670. if ($name) {
  671. $node->title = $name;
  672. }
  673. // reset the type
  674. //$node->type = $node->analysis_type;
  675. }
  676. else if (property_exists($node, 'analysis')) {
  677. $name = $node->analysis->name;
  678. $program = $node->analysis->program;
  679. $programversion = $node->analysis->programversion;
  680. $sourcename = $node->analysis->sourcename;
  681. // now construct the title
  682. $node->title = "$program ($programversion) $sourcename";
  683. if ($name) {
  684. $node->title = $name;
  685. }
  686. //$node->type = $node->analysis_type;
  687. }
  688. }
  689. /**
  690. * Implements hook_node_insert().
  691. * Acts on all content types.
  692. *
  693. * @ingroup tripal_legacy_analysis
  694. */
  695. function tripal_analysis_node_insert($node) {
  696. switch ($node->type) {
  697. case 'chado_analysis':
  698. // We still don't have a fully loaded node object in this hook. Therefore,
  699. // we need to simulate one so that the right values are available for
  700. // the URL to be determined.
  701. $analysis_id = chado_get_id_from_nid('analysis', $node->nid);
  702. $values = array('analysis_id' => $analysis_id);
  703. $analysis = chado_generate_var('analysis', $values);
  704. $node->analysis = $analysis;
  705. // Now get the title
  706. $node->title = chado_get_node_title($node);
  707. // Now use the API to set the path.
  708. chado_set_node_url($node);
  709. break;
  710. }
  711. }
  712. /**
  713. * Implements hook_node_update().
  714. * Acts on all content types.
  715. *
  716. * @ingroup tripal_legacy_analysis
  717. */
  718. function tripal_analysis_node_update($node) {
  719. switch ($node->type) {
  720. case 'chado_analysis':
  721. // Now get the title
  722. $node->title = chado_get_node_title($node);
  723. // Now use the API to set the path.
  724. chado_set_node_url($node);
  725. break;
  726. }
  727. }
  728. /**
  729. * Implements [content_type]_chado_node_default_title_format().
  730. *
  731. * Defines a default title format for the Chado Node API to set the titles on
  732. * Chado Analysis nodes based on chado fields.
  733. */
  734. function chado_analysis_chado_node_default_title_format() {
  735. return '[analysis.name]';
  736. }
  737. /**
  738. * Implements hook_chado_node_default_url_format().
  739. *
  740. * Designates a default URL format for analysis nodes.
  741. */
  742. function chado_analysis_chado_node_default_url_format() {
  743. return '/analysis/[analysis.analysis_id]';
  744. }