tripal_organism.chado_node.inc 19 KB

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