tripal_organism.chado_node.inc 15 KB

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