tripal_organism.chado_node.inc 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  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. tripal_report_error('tripal_organism', TRIPAL_WARNING,
  122. 'Update organism: genus and species already exists: %values',
  123. array('%values' => "genus = $node->genus, species = $node->species"));
  124. }
  125. }
  126. // Validating for an insert
  127. else {
  128. $values = array(
  129. 'genus' => $node->genus,
  130. 'species' => $node->species,
  131. );
  132. $organism = chado_select_record('organism', array('organism_id'), $values);
  133. if (sizeof($organism) > 0) {
  134. form_set_error('genus', 'Cannot add the organism with this genus and species.
  135. The organism already exists.');
  136. tripal_report_error('tripal_organism', TRIPAL_WARNING,
  137. 'Insert organism: genus and species already exists: %values',
  138. array('%values' => "genus = $node->genus, species = $node->species"));
  139. }
  140. }
  141. }
  142. /**
  143. * When a new chado_organism node is created we also need to add information
  144. * to our chado_organism table. This function is called on insert of a new node
  145. * of type 'chado_organism' and inserts the necessary information.
  146. *
  147. * @ingroup tripal_organism
  148. */
  149. function chado_organism_insert($node) {
  150. // remove any white space around values
  151. $node->genus = trim($node->genus);
  152. $node->species = trim($node->species);
  153. $node->abbreviation = trim($node->abbreviation);
  154. $node->common_name = trim($node->common_name);
  155. $node->description = trim($node->description);
  156. // if there is an organism_id in the $node object then this must be a sync so
  157. // we can skip adding the organism as it is already there, although
  158. // we do need to proceed with the rest of the insert
  159. if (!property_exists($node,'organism_id')) {
  160. $values = array(
  161. 'genus' => $node->genus,
  162. 'species' => $node->species,
  163. 'abbreviation' => $node->abbreviation,
  164. 'common_name' => $node->common_name,
  165. 'comment' => $node->description
  166. );
  167. $organism = chado_insert_record('organism', $values);
  168. if (!$organism) {
  169. drupal_set_message(t('Unable to add organism.', 'warning'));
  170. tripal_report_error('tripal_organism', TRIPAL_ERROR, 'Insert Organism: Unable to create organism where values:%values',
  171. array('%values' => print_r($values, TRUE)));
  172. return;
  173. }
  174. $organism_id = $organism['organism_id'];
  175. if ($organism_id) {
  176. // * Properties Form *
  177. $details = array(
  178. 'property_table' => 'organismprop', // the name of the prop table
  179. 'base_table' => 'organism', // the name of your chado base table
  180. 'foreignkey_name' => 'organism_id', // the name of the key in your base table
  181. 'foreignkey_value' => $organism_id // the value of the example_id key
  182. );
  183. chado_update_node_form_properties($node, $details);
  184. // * Additional DBxrefs Form *
  185. $details = array(
  186. 'linking_table' => 'organism_dbxref', // the name of your _dbxref table
  187. 'foreignkey_name' => 'organism_id', // the name of the key in your base table
  188. 'foreignkey_value' => $organism_id // the value of the organism_id key
  189. );
  190. chado_update_node_form_dbxrefs($node, $details);
  191. }
  192. }
  193. else {
  194. $organism_id = $node->organism_id;
  195. }
  196. // Make sure the entry for this organism doesn't already exist in the
  197. // chado_organism table if it doesn't exist then we want to add it.
  198. $check_org_id = chado_get_id_from_nid('organism', $node->nid);
  199. if (!$check_org_id) {
  200. $record = new stdClass();
  201. $record->nid = $node->nid;
  202. $record->vid = $node->vid;
  203. $record->organism_id = $organism_id;
  204. drupal_write_record('chado_organism', $record);
  205. }
  206. // add the image
  207. chado_organism_add_image($node);
  208. }
  209. /**
  210. * Update organisms
  211. *
  212. * @ingroup tripal_organism
  213. */
  214. function chado_organism_update($node) {
  215. // remove any white space around values
  216. $node->genus = trim($node->genus);
  217. $node->species = trim($node->species);
  218. $node->abbreviation = trim($node->abbreviation);
  219. $node->common_name = trim($node->common_name);
  220. $node->description = trim($node->description);
  221. $organism_id = chado_get_id_from_nid('organism', $node->nid);
  222. if ($node->revision) {
  223. // there is no way to handle revisions in Chado but leave
  224. // this here just to make not we've addressed it.
  225. }
  226. $match = array(
  227. 'organism_id' => $organism_id,
  228. );
  229. $values = array(
  230. 'genus' => $node->genus,
  231. 'species' => $node->species,
  232. 'abbreviation' => $node->abbreviation,
  233. 'common_name' => $node->common_name,
  234. 'comment' => $node->description
  235. );
  236. $org_status = chado_update_record('organism', $match, $values);
  237. // add the image
  238. chado_organism_add_image($node);
  239. // * Properties Form *
  240. $details = array(
  241. 'property_table' => 'organismprop', // the name of the prop table
  242. 'base_table' => 'organism', // the name of your chado base table
  243. 'foreignkey_name' => 'organism_id', // the name of the key in your base table
  244. 'foreignkey_value' => $organism_id // the value of the example_id key
  245. );
  246. chado_update_node_form_properties($node, $details);
  247. // * Additional DBxrefs Form *
  248. $details = array(
  249. 'linking_table' => 'organism_dbxref', // the name of your _dbxref table
  250. 'foreignkey_name' => 'organism_id', // the name of the key in your base table
  251. 'foreignkey_value' => $organism_id // the value of the organism_id key
  252. );
  253. chado_update_node_form_dbxrefs($node, $details);
  254. }
  255. /**
  256. * Delete organism from both drupal and chado databases. Check dependency before
  257. * deleting from chado.
  258. *
  259. * @ingroup tripal_organism
  260. */
  261. function chado_organism_delete($node) {
  262. $organism_id = chado_get_id_from_nid('organism', $node->nid);
  263. // if we don't have an organism id for this node then this isn't a node of
  264. // type chado_organism or the entry in the chado_organism table was lost.
  265. if (!$organism_id) {
  266. return;
  267. }
  268. // Remove data from the {chado_organism}, {node}, and {node_revisions} tables
  269. $sql_del = "DELETE FROM {chado_organism} WHERE nid = :nid AND vid = :vid";
  270. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  271. $sql_del = "DELETE FROM {node} WHERE nid = :nid AND vid = :vid";
  272. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  273. $sql_del = "DELETE FROM {node_revision} WHERE nid = ':nid' AND vid = ':vid'";
  274. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  275. // Test dependency before deleting from chado database. If a library or
  276. // feature depends on this organism, don't delete it
  277. $sql = "SELECT feature_id FROM {feature} WHERE organism_id = :organism_id";
  278. $check_feature = chado_query($sql, array(':organism_id' => $organism_id))->fetchObject();
  279. $sql = "SELECT library_id FROM {library} WHERE organism_id = :organism_id";
  280. $check_lib = chado_query($sql, array(':organism_id' => $organism_id))->fetchObject();
  281. $sql = "SELECT stock_id FROM {stock} WHERE organism_id = :organism_id";
  282. $check_stock = chado_query($sql, array(':organism_id' => $organism_id))->fetchObject();
  283. if (!$check_lib && !$check_feature && !$check_stock) {
  284. chado_delete_record('organism', array('organism_id' => $organism_id));
  285. }
  286. else {
  287. 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');
  288. }
  289. }
  290. /**
  291. *
  292. *
  293. * @ingroup tripal_organism
  294. */
  295. function chado_organism_add_image($node) {
  296. // check to see if a file was uploaded. If so then copy it to the images
  297. // directory for display with the organism
  298. if (isset($_FILES['files']) &&
  299. $_FILES['files']['name']['organism_image'] &&
  300. is_uploaded_file($_FILES['files']['tmp_name']['organism_image'])) {
  301. // make sure the destination directory exists
  302. $dest = tripal_get_files_dir() . "/tripal_organism/images";
  303. file_prepare_directory($dest, FILE_CREATE_DIRECTORY);
  304. // now move the file
  305. $validators = array('file_validate_is_image' => array());
  306. $destination = "public://tripal/tripal_organism/images/";
  307. $file = file_save_upload('organism_image', $validators, $destination);
  308. if (!$file) {
  309. drupal_set_message(t("Organism image was not uploaded."));
  310. }
  311. else {
  312. file_move($file, $destination . "/" . $node->nid . ".jpg", FILE_EXISTS_REPLACE);
  313. }
  314. }
  315. }
  316. /**
  317. * When editing or creating a new node of type 'chado_organism' we need
  318. * a form. This function creates the form that will be used for this.
  319. *
  320. * @ingroup tripal_organism
  321. */
  322. function chado_organism_form($node, $form_state) {
  323. $form = array();
  324. // we have a file upload element on the form soe we need the multipart encoding type
  325. $form['#attributes']['enctype'] = 'multipart/form-data';
  326. // if the organism is part of the node object then we are editing. If not we are inserting
  327. if (property_exists($node, 'organism')) {
  328. $organism = $node->organism;
  329. // add in the comment since it is a text field and may not be included if too big
  330. $organism = chado_expand_var($organism, 'field', 'organism.comment');
  331. // get form defaults
  332. $abbreviation = property_exists($node, 'abbreviation') ? property_exists($node, 'abbreviation') : $organism->abbreviation;
  333. $genus = property_exists($node, 'genus') ? property_exists($node, 'genus') : $organism->genus;
  334. $species = property_exists($node, 'species') ? property_exists($node, 'species') : $organism->species;
  335. $common_name = property_exists($node, 'common_name') ? property_exists($node, 'common_name') : $organism->common_name;
  336. $description = property_exists($node, 'description') ? property_exists($node, 'description') : $organism->comment;
  337. $organism_image = property_exists($node, 'organism_image') ? property_exists($node, 'organism_image') : '';
  338. // set the organism_id in the form
  339. $form['organism_id'] = array(
  340. '#type' => 'value',
  341. '#value' => $organism->organism_id,
  342. );
  343. $organism_id = $organism->organism_id;
  344. }
  345. else {
  346. // get form defaults
  347. $abbreviation = property_exists($node, 'abbreviation') ? property_exists($node, 'abbreviation') : '';
  348. $genus = property_exists($node, 'genus') ? property_exists($node, 'genus') : '';
  349. $species = property_exists($node, 'species') ? property_exists($node, 'species') : '';
  350. $common_name = property_exists($node, 'common_name') ? property_exists($node, 'common_name') : '';
  351. $description = property_exists($node, 'description') ? property_exists($node, 'description') : '';
  352. $organism_image = property_exists($node, 'organism_image') ? property_exists($node, 'organism_image') : '';
  353. $organism_id = NULL;
  354. }
  355. $form['genus']= array(
  356. '#type' => 'textfield',
  357. '#title' => t('Genus'),
  358. '#required' => TRUE,
  359. '#default_value' => $genus,
  360. );
  361. $form['species']= array(
  362. '#type' => 'textfield',
  363. '#title' => t('Species'),
  364. '#required' => TRUE,
  365. '#default_value' => $species,
  366. );
  367. $form['abbreviation']= array(
  368. '#type' => 'textfield',
  369. '#title' => t('Abbreviation'),
  370. '#required' => TRUE,
  371. '#default_value' => $abbreviation,
  372. );
  373. $form['common_name']= array(
  374. '#type' => 'textfield',
  375. '#title' => t('Common Name'),
  376. '#required' => TRUE,
  377. '#default_value' => $common_name,
  378. );
  379. $form['description']= array(
  380. '#type' => 'textarea',
  381. '#rows' => 15,
  382. '#title' => t('Description'),
  383. '#default_value' => $description,
  384. );
  385. $form['organism_image']= array(
  386. '#type' => 'file',
  387. '#title' => t('Organism Image'),
  388. '#description' => 'Add an image for this organism',
  389. '#progress_indicator' => 'bar',
  390. );
  391. // PROPERTIES FORM
  392. //---------------------------------------------
  393. $details = array(
  394. 'property_table' => 'organismprop', // the name of the prop table
  395. 'base_foreign_key' => 'organism_id', // the name of the key in your base chado table
  396. 'base_key_value' => $organism_id, // the value of organism_id for this record
  397. 'cv_name' => 'organism_property' // the cv.name of the cv governing organismprop.type_id
  398. );
  399. // Adds the form elements to your current form
  400. chado_add_node_form_properties($form, $form_state, $details);
  401. // ADDITIONAL DBXREFS FORM
  402. //---------------------------------------------
  403. $details = array(
  404. 'linking_table' => 'organism_dbxref', // the name of the _dbxref table
  405. 'base_foreign_key' => 'organism_id', // the name of the key in your base chado table
  406. 'base_key_value' => $organism_id // the value of organism_id for this record
  407. );
  408. // Adds the form elements to your current form
  409. chado_add_node_form_dbxrefs($form, $form_state, $details);
  410. return $form;
  411. }
  412. /**
  413. * When a node is requested by the user this function is called to allow us
  414. * to add auxiliary data to the node object.
  415. *
  416. * @ingroup tripal_organism
  417. */
  418. function chado_organism_load($nodes) {
  419. foreach ($nodes as $nid => $node) {
  420. // find the organism and add in the details
  421. $organism_id = chado_get_id_from_nid('organism', $nid);
  422. // build the organism variable
  423. $values = array('organism_id' => $organism_id);
  424. $organism = chado_generate_var('organism', $values);
  425. // add in the description field
  426. $organism = chado_expand_var($organism, 'field', 'organism.comment');
  427. $nodes[$nid]->organism = $organism;
  428. }
  429. }
  430. /**
  431. *
  432. * @param $node
  433. */
  434. function tripal_organism_node_presave($node) {
  435. print_r($node->organism);
  436. switch ($node->type) {
  437. case 'chado_organism':
  438. // for a form submission the 'genus' field will be set,
  439. // for a sync, we must pull from the organism object
  440. if(property_exists($node, 'genus')) {
  441. // set the title
  442. $node->title = $node->genus . " " . $node->species;
  443. }
  444. else {
  445. // set the title
  446. $node->title = $node->organism->genus . " " . $node->organism->species;
  447. }
  448. break;
  449. }
  450. }
  451. /**
  452. *
  453. * @ingroup tripal_feature
  454. */
  455. function tripal_organism_node_view($node, $view_mode, $langcode) {
  456. switch ($node->type) {
  457. case 'chado_organism':
  458. // Show feature browser and counts
  459. if ($view_mode == 'full') {
  460. $node->content['tripal_organism_base'] = array(
  461. '#markup' => theme('tripal_organism_base', array('node' => $node)),
  462. '#tripal_toc_id' => 'base',
  463. '#tripal_toc_title' => 'Overview',
  464. '#weight' => -100,
  465. );
  466. $node->content['tripal_organism_properties'] = array(
  467. '#markup' => theme('tripal_organism_properties', array('node' => $node)),
  468. '#tripal_toc_id' => 'properties',
  469. '#tripal_toc_title' => 'Properties',
  470. );
  471. $node->content['tripal_organism_references'] = array(
  472. '#markup' => theme('tripal_organism_references', array('node' => $node)),
  473. '#tripal_toc_id' => 'references',
  474. '#tripal_toc_title' => 'Cross References',
  475. );
  476. }
  477. if ($view_mode == 'teaser') {
  478. $node->content['tripal_organism_teaser'] = array(
  479. '#markup' => theme('tripal_organism_teaser', array('node' => $node)),
  480. );
  481. }
  482. break;
  483. }
  484. }