tripal_analysis.chado_node.inc 28 KB

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