tripal_organism.chado_node.inc 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. <?php
  2. /**
  3. * @file
  4. * Implements the organims node content type
  5. */
  6. /**
  7. * Implements hook_node_info().
  8. *
  9. * Provide information to drupal about the node types that we're creating
  10. * in this module
  11. *
  12. * @ingroup tripal_organism
  13. */
  14. function tripal_organism_node_info() {
  15. $nodes = array();
  16. $nodes['chado_organism'] = array(
  17. 'name' => t('Organism'),
  18. 'base' => 'chado_organism',
  19. 'description' => t('An organism'),
  20. 'has_title' => TRUE,
  21. 'locked' => TRUE,
  22. 'chado_node_api' => array(
  23. 'base_table' => 'organism',
  24. 'hook_prefix' => 'chado_organism',
  25. 'record_type_title' => array(
  26. 'singular' => t('Organism'),
  27. 'plural' => t('Organisms')
  28. ),
  29. 'sync_filters' => array(
  30. 'type_id' => FALSE,
  31. 'organism_id' => FALSE,
  32. 'checkboxes' => array('genus', 'species'),
  33. ),
  34. )
  35. );
  36. return $nodes;
  37. }
  38. /**
  39. * Implement hook_node_access().
  40. *
  41. * This hook allows node modules to limit access to the node types they define.
  42. *
  43. * @param $node
  44. * The node on which the operation is to be performed, or, if it does not yet exist, the
  45. * type of node to be created
  46. *
  47. * @param $op
  48. * The operation to be performed
  49. *
  50. *
  51. * @param $account
  52. * A user object representing the user for whom the operation is to be performed
  53. *
  54. * @return
  55. * If the permission for the specified operation is not set then return FALSE. If the
  56. * permission is set then return NULL as this allows other modules to disable
  57. * access. The only exception is when the $op == 'create'. We will always
  58. * return TRUE if the permission is set.
  59. *
  60. * @ingroup tripal_organism
  61. */
  62. function chado_organism_node_access($node, $op, $account) {
  63. if ($op == 'create') {
  64. if (!user_access('create chado_organism content', $account)) {
  65. return FALSE;
  66. }
  67. return TRUE;
  68. }
  69. if ($op == 'update') {
  70. if (!user_access('edit chado_organism content', $account)) {
  71. return FALSE;
  72. }
  73. }
  74. if ($op == 'delete') {
  75. if (!user_access('delete chado_organism content', $account)) {
  76. return FALSE;
  77. }
  78. }
  79. if ($op == 'view') {
  80. if (!user_access('access chado_organism content', $account)) {
  81. return FALSE;
  82. }
  83. }
  84. return NULL;
  85. }
  86. /**
  87. * Implement hook_form().
  88. *
  89. * When editing or creating a new node of type 'chado_organism' we need
  90. * a form. This function creates the form that will be used for this.
  91. *
  92. * @ingroup tripal_organism
  93. */
  94. function chado_organism_form($node, $form_state) {
  95. $form = array();
  96. // we have a file upload element on the form soe we need the multipart encoding type
  97. $form['#attributes']['enctype'] = 'multipart/form-data';
  98. // if the organism is part of the node object then we are editing. If not we are inserting
  99. if (property_exists($node, 'organism')) {
  100. $organism = $node->organism;
  101. // add in the comment since it is a text field and may not be included if too big
  102. $organism = chado_expand_var($organism, 'field', 'organism.comment');
  103. // get form defaults
  104. $abbreviation = property_exists($node, 'abbreviation') ? property_exists($node, 'abbreviation') : $organism->abbreviation;
  105. $genus = property_exists($node, 'genus') ? property_exists($node, 'genus') : $organism->genus;
  106. $species = property_exists($node, 'species') ? property_exists($node, 'species') : $organism->species;
  107. $common_name = property_exists($node, 'common_name') ? property_exists($node, 'common_name') : $organism->common_name;
  108. $description = property_exists($node, 'description') ? property_exists($node, 'description') : $organism->comment;
  109. $organism_image = property_exists($node, 'organism_image') ? property_exists($node, 'organism_image') : '';
  110. // set the organism_id in the form
  111. $form['organism_id'] = array(
  112. '#type' => 'value',
  113. '#value' => $organism->organism_id,
  114. );
  115. $organism_id = $organism->organism_id;
  116. }
  117. else {
  118. // get form defaults
  119. $abbreviation = property_exists($node, 'abbreviation') ? property_exists($node, 'abbreviation') : '';
  120. $genus = property_exists($node, 'genus') ? property_exists($node, 'genus') : '';
  121. $species = property_exists($node, 'species') ? property_exists($node, 'species') : '';
  122. $common_name = property_exists($node, 'common_name') ? property_exists($node, 'common_name') : '';
  123. $description = property_exists($node, 'description') ? property_exists($node, 'description') : '';
  124. $organism_image = property_exists($node, 'organism_image') ? property_exists($node, 'organism_image') : '';
  125. $organism_id = NULL;
  126. }
  127. $form['genus']= array(
  128. '#type' => 'textfield',
  129. '#title' => t('Genus'),
  130. '#required' => TRUE,
  131. '#default_value' => $genus,
  132. );
  133. $form['species']= array(
  134. '#type' => 'textfield',
  135. '#title' => t('Species'),
  136. '#required' => TRUE,
  137. '#default_value' => $species,
  138. );
  139. $form['abbreviation']= array(
  140. '#type' => 'textfield',
  141. '#title' => t('Abbreviation'),
  142. '#required' => TRUE,
  143. '#default_value' => $abbreviation,
  144. );
  145. $form['common_name']= array(
  146. '#type' => 'textfield',
  147. '#title' => t('Common Name'),
  148. '#required' => TRUE,
  149. '#default_value' => $common_name,
  150. );
  151. $form['description']= array(
  152. '#type' => 'textarea',
  153. '#rows' => 15,
  154. '#title' => t('Description'),
  155. '#default_value' => $description,
  156. );
  157. $form['organism_image']= array(
  158. '#type' => 'file',
  159. '#title' => t('Organism Image'),
  160. '#description' => 'Add an image for this organism',
  161. '#progress_indicator' => 'bar',
  162. );
  163. // PROPERTIES FORM
  164. //---------------------------------------------
  165. $details = array(
  166. 'property_table' => 'organismprop', // the name of the prop table
  167. 'chado_id' => $organism_id, // the value of organism_id for this record
  168. 'cv_name' => 'organism_property' // the cv.name of the cv governing organismprop.type_id
  169. );
  170. // Adds the form elements to your current form
  171. chado_add_node_form_properties($form, $form_state, $details);
  172. // ADDITIONAL DBXREFS FORM
  173. //---------------------------------------------
  174. $details = array(
  175. 'linking_table' => 'organism_dbxref', // the name of the _dbxref table
  176. 'base_foreign_key' => 'organism_id', // the name of the key in your base chado table
  177. 'base_key_value' => $organism_id // the value of organism_id for this record
  178. );
  179. // Adds the form elements to your current form
  180. chado_add_node_form_dbxrefs($form, $form_state, $details);
  181. return $form;
  182. }
  183. /**
  184. * Implementation of hook_validate().
  185. *
  186. * @param $node
  187. * @param $form
  188. * @param $form_state
  189. *
  190. * @ingroup tripal_organism
  191. */
  192. function chado_organism_validate($node, $form, &$form_state) {
  193. // remove any white space around values
  194. $node->genus = trim($node->genus);
  195. $node->species = trim($node->species);
  196. $node->abbreviation = trim($node->abbreviation);
  197. $node->common_name = trim($node->common_name);
  198. $node->description = trim($node->description);
  199. // if this is a delete then don't validate
  200. if($node->op == 'Delete') {
  201. return;
  202. }
  203. // we are syncing if we do not have a node ID but we do have a organism_id. We don't
  204. // need to validate during syncing so just skip it.
  205. if (is_null($node->nid) and property_exists($node, 'organism_id') and $node->organism_id != 0) {
  206. return;
  207. }
  208. // Validating for an update
  209. if (property_exists($node, 'organism_id')) {
  210. $sql = "
  211. SELECT *
  212. FROM {organism} O
  213. WHERE
  214. genus = :genus AND
  215. species = :species AND NOT
  216. organism_id = :organism_id
  217. ";
  218. $args = array(':genus' => $node->genus, ':species' => $node->species, ':organism_id' => $node->organism_id);
  219. $result = chado_query($sql, $args)->fetchObject();
  220. if ($result) {
  221. form_set_error('genus', t("Update cannot proceed. The organism genus
  222. '$node->genus' and species '$node->species' is already present in the database."));
  223. tripal_report_error('tripal_organism', TRIPAL_WARNING,
  224. 'Update organism: genus and species already exists: %values',
  225. array('%values' => "genus = $node->genus, species = $node->species"));
  226. }
  227. }
  228. // Validating for an insert
  229. else {
  230. $values = array(
  231. 'genus' => $node->genus,
  232. 'species' => $node->species,
  233. );
  234. $organism = chado_select_record('organism', array('organism_id'), $values);
  235. if (sizeof($organism) > 0) {
  236. form_set_error('genus', 'Cannot add the organism with this genus and species.
  237. The organism already exists.');
  238. tripal_report_error('tripal_organism', TRIPAL_WARNING,
  239. 'Insert organism: genus and species already exists: %values',
  240. array('%values' => "genus = $node->genus, species = $node->species"));
  241. }
  242. }
  243. }
  244. /**
  245. * Implements hook_insert().
  246. *
  247. * When a new chado_organism node is created we also need to add information
  248. * to our chado_organism table. This function is called on insert of a new node
  249. * of type 'chado_organism' and inserts the necessary information.
  250. *
  251. * @ingroup tripal_organism
  252. */
  253. function chado_organism_insert($node) {
  254. // remove any white space around values
  255. $node->genus = trim($node->genus);
  256. $node->species = trim($node->species);
  257. $node->abbreviation = trim($node->abbreviation);
  258. $node->common_name = trim($node->common_name);
  259. $node->description = trim($node->description);
  260. // if there is an organism_id in the $node object then this must be a sync so
  261. // we can skip adding the organism as it is already there, although
  262. // we do need to proceed with the rest of the insert
  263. if (!property_exists($node,'organism_id')) {
  264. $values = array(
  265. 'genus' => $node->genus,
  266. 'species' => $node->species,
  267. 'abbreviation' => $node->abbreviation,
  268. 'common_name' => $node->common_name,
  269. 'comment' => $node->description
  270. );
  271. $organism = chado_insert_record('organism', $values);
  272. if (!$organism) {
  273. drupal_set_message(t('Unable to add organism.', 'warning'));
  274. tripal_report_error('tripal_organism', TRIPAL_ERROR, 'Insert Organism: Unable to create organism where values:%values',
  275. array('%values' => print_r($values, TRUE)));
  276. return;
  277. }
  278. $organism_id = $organism['organism_id'];
  279. if ($organism_id) {
  280. // * Properties Form *
  281. $details = array(
  282. 'property_table' => 'organismprop', // the name of the prop table
  283. 'base_table' => 'organism', // the name of your chado base table
  284. 'foreignkey_name' => 'organism_id', // the name of the key in your base table
  285. 'foreignkey_value' => $organism_id // the value of the example_id key
  286. );
  287. chado_update_node_form_properties($node, $details);
  288. // * Additional DBxrefs Form *
  289. $details = array(
  290. 'linking_table' => 'organism_dbxref', // the name of your _dbxref table
  291. 'foreignkey_name' => 'organism_id', // the name of the key in your base table
  292. 'foreignkey_value' => $organism_id // the value of the organism_id key
  293. );
  294. chado_update_node_form_dbxrefs($node, $details);
  295. }
  296. }
  297. else {
  298. $organism_id = $node->organism_id;
  299. }
  300. // Make sure the entry for this organism doesn't already exist in the
  301. // chado_organism table if it doesn't exist then we want to add it.
  302. $check_org_id = chado_get_id_from_nid('organism', $node->nid);
  303. if (!$check_org_id) {
  304. $record = new stdClass();
  305. $record->nid = $node->nid;
  306. $record->vid = $node->vid;
  307. $record->organism_id = $organism_id;
  308. drupal_write_record('chado_organism', $record);
  309. }
  310. // add the image
  311. chado_organism_add_image($node);
  312. }
  313. /**
  314. * Implements hook_update().
  315. *
  316. * @ingroup tripal_organism
  317. */
  318. function chado_organism_update($node) {
  319. // remove any white space around values
  320. $node->genus = trim($node->genus);
  321. $node->species = trim($node->species);
  322. $node->abbreviation = trim($node->abbreviation);
  323. $node->common_name = trim($node->common_name);
  324. $node->description = trim($node->description);
  325. $organism_id = chado_get_id_from_nid('organism', $node->nid);
  326. if ($node->revision) {
  327. // there is no way to handle revisions in Chado but leave
  328. // this here just to make not we've addressed it.
  329. }
  330. $match = array(
  331. 'organism_id' => $organism_id,
  332. );
  333. $values = array(
  334. 'genus' => $node->genus,
  335. 'species' => $node->species,
  336. 'abbreviation' => $node->abbreviation,
  337. 'common_name' => $node->common_name,
  338. 'comment' => $node->description
  339. );
  340. $org_status = chado_update_record('organism', $match, $values);
  341. // add the image
  342. chado_organism_add_image($node);
  343. // * Properties Form *
  344. $details = array(
  345. 'property_table' => 'organismprop', // the name of the prop table
  346. 'base_table' => 'organism', // the name of your chado base table
  347. 'foreignkey_name' => 'organism_id', // the name of the key in your base table
  348. 'foreignkey_value' => $organism_id // the value of the example_id key
  349. );
  350. chado_update_node_form_properties($node, $details);
  351. // * Additional DBxrefs Form *
  352. $details = array(
  353. 'linking_table' => 'organism_dbxref', // the name of your _dbxref table
  354. 'foreignkey_name' => 'organism_id', // the name of the key in your base table
  355. 'foreignkey_value' => $organism_id // the value of the organism_id key
  356. );
  357. chado_update_node_form_dbxrefs($node, $details);
  358. }
  359. /**
  360. * Implements hook_delete().
  361. *
  362. * Delete organism from both drupal and chado databases. Check dependency before
  363. * deleting from chado.
  364. *
  365. * @ingroup tripal_organism
  366. */
  367. function chado_organism_delete($node) {
  368. $organism_id = chado_get_id_from_nid('organism', $node->nid);
  369. // if we don't have an organism id for this node then this isn't a node of
  370. // type chado_organism or the entry in the chado_organism table was lost.
  371. if (!$organism_id) {
  372. return;
  373. }
  374. // Remove data from the {chado_organism}, {node}, and {node_revisions} tables
  375. $sql_del = "DELETE FROM {chado_organism} WHERE nid = :nid AND vid = :vid";
  376. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  377. $sql_del = "DELETE FROM {node} WHERE nid = :nid AND vid = :vid";
  378. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  379. $sql_del = "DELETE FROM {node_revision} WHERE nid = ':nid' AND vid = ':vid'";
  380. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  381. // Test dependency before deleting from chado database. If a library or
  382. // feature depends on this organism, don't delete it
  383. $sql = "SELECT feature_id FROM {feature} WHERE organism_id = :organism_id";
  384. $check_feature = chado_query($sql, array(':organism_id' => $organism_id))->fetchObject();
  385. $sql = "SELECT library_id FROM {library} WHERE organism_id = :organism_id";
  386. $check_lib = chado_query($sql, array(':organism_id' => $organism_id))->fetchObject();
  387. $sql = "SELECT stock_id FROM {stock} WHERE organism_id = :organism_id";
  388. $check_stock = chado_query($sql, array(':organism_id' => $organism_id))->fetchObject();
  389. if (!$check_lib && !$check_feature && !$check_stock) {
  390. chado_delete_record('organism', array('organism_id' => $organism_id));
  391. }
  392. else {
  393. drupal_set_message(t("Warning: other data depends on this organism. The organism page was removed from this site but the organism was removed from Chado."), 'warning');
  394. }
  395. }
  396. /**
  397. * Add an image to an organims node
  398. *
  399. * @param $node
  400. * The node to add an image to
  401. *
  402. * The file is specified in the $_FILES array created by Drupal
  403. *
  404. * @ingroup tripal_organism
  405. */
  406. function chado_organism_add_image($node) {
  407. // check to see if a file was uploaded. If so then copy it to the images
  408. // directory for display with the organism
  409. if (isset($_FILES['files']) &&
  410. $_FILES['files']['name']['organism_image'] &&
  411. is_uploaded_file($_FILES['files']['tmp_name']['organism_image'])) {
  412. // make sure the destination directory exists
  413. $dest = tripal_get_files_dir() . "/tripal_organism/images";
  414. file_prepare_directory($dest, FILE_CREATE_DIRECTORY);
  415. // now move the file
  416. $validators = array('file_validate_is_image' => array());
  417. $destination = "public://tripal/tripal_organism/images/";
  418. $file = file_save_upload('organism_image', $validators, $destination);
  419. if (!$file) {
  420. drupal_set_message(t("Organism image was not uploaded."));
  421. }
  422. else {
  423. file_move($file, $destination . "/" . $node->nid . ".jpg", FILE_EXISTS_REPLACE);
  424. }
  425. }
  426. }
  427. /**
  428. * Implements hook_load().
  429. *
  430. * When a node is requested by the user this function is called to allow us
  431. * to add auxiliary data to the node object.
  432. *
  433. * @ingroup tripal_organism
  434. */
  435. function chado_organism_load($nodes) {
  436. foreach ($nodes as $nid => $node) {
  437. // find the organism and add in the details
  438. $organism_id = chado_get_id_from_nid('organism', $nid);
  439. // build the organism variable
  440. $values = array('organism_id' => $organism_id);
  441. $organism = chado_generate_var('organism', $values);
  442. // add in the description field
  443. $organism = chado_expand_var($organism, 'field', 'organism.comment');
  444. $nodes[$nid]->organism = $organism;
  445. }
  446. }
  447. /**
  448. * Implements hook_node_presave(). Acts on all content types.
  449. *
  450. * @param $node
  451. * The node to be saved
  452. *
  453. * @ingroup tripal_organism
  454. */
  455. function tripal_organism_node_presave($node) {
  456. switch ($node->type) {
  457. case 'chado_organism':
  458. // for a form submission the 'genus' field will be set,
  459. // for a sync, we must pull from the organism object
  460. if(property_exists($node, 'genus')) {
  461. // set the title
  462. $node->title = $node->genus . " " . $node->species;
  463. }
  464. else {
  465. // set the title
  466. $node->title = $node->organism->genus . " " . $node->organism->species;
  467. }
  468. break;
  469. }
  470. }
  471. /**
  472. * Implements hook_node_view().
  473. *
  474. * @ingroup tripal_organism
  475. */
  476. function tripal_organism_node_view($node, $view_mode, $langcode) {
  477. switch ($node->type) {
  478. case 'chado_organism':
  479. // Show feature browser and counts
  480. if ($view_mode == 'full') {
  481. $node->content['tripal_organism_base'] = array(
  482. '#markup' => theme('tripal_organism_base', array('node' => $node)),
  483. '#tripal_toc_id' => 'base',
  484. '#tripal_toc_title' => 'Overview',
  485. '#weight' => -100,
  486. );
  487. $node->content['tripal_organism_properties'] = array(
  488. '#markup' => theme('tripal_organism_properties', array('node' => $node)),
  489. '#tripal_toc_id' => 'properties',
  490. '#tripal_toc_title' => 'Properties',
  491. );
  492. $node->content['tripal_organism_references'] = array(
  493. '#markup' => theme('tripal_organism_references', array('node' => $node)),
  494. '#tripal_toc_id' => 'references',
  495. '#tripal_toc_title' => 'Cross References',
  496. );
  497. }
  498. if ($view_mode == 'teaser') {
  499. $node->content['tripal_organism_teaser'] = array(
  500. '#markup' => theme('tripal_organism_teaser', array('node' => $node)),
  501. );
  502. }
  503. break;
  504. }
  505. }