tripal_library.chado_node.inc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. <?php
  2. /**
  3. * Provide information to drupal about the node types that we're creating
  4. * in this module
  5. *
  6. * @ingroup tripal_library
  7. */
  8. function tripal_library_node_info() {
  9. $nodes = array();
  10. $nodes['chado_library'] = array(
  11. 'name' => t('Library'),
  12. 'base' => 'chado_library',
  13. 'description' => t('A library from the chado database'),
  14. 'has_title' => FALSE,
  15. 'title_label' => t('Library'),
  16. 'has_body' => FALSE,
  17. 'locked' => TRUE,
  18. 'chado_node_api' => array(
  19. 'base_table' => 'library',
  20. 'hook_prefix' => 'chado_library',
  21. 'record_type_title' => array(
  22. 'singular' => t('Library'),
  23. 'plural' => t('Libraries')
  24. ),
  25. 'sync_filters' => array(
  26. 'type_id' => TRUE,
  27. 'organism_id' => TRUE
  28. ),
  29. )
  30. );
  31. return $nodes;
  32. }
  33. /**
  34. * When editing or creating a new node of type 'chado_library' we need
  35. * a form. This function creates the form that will be used for this.
  36. *
  37. * @ingroup tripal_library
  38. */
  39. function chado_library_form($node, &$form_state) {
  40. $form = array();
  41. // Default values can come in the following ways:
  42. //
  43. // 1) as elements of the $node object. This occurs when editing an existing library
  44. // 2) in the $form_state['values'] array which occurs on a failed validation or
  45. // ajax callbacks from non submit form elements
  46. // 3) in the $form_state['input'] array which occurs on ajax callbacks from submit
  47. // form elements and the form is being rebuilt
  48. //
  49. // set form field defaults
  50. $library_id = NULL;
  51. $title = '';
  52. $uniquename = '';
  53. $library_type = '';
  54. $organism_id = '';
  55. $description = '';
  56. // if we are editing an existing node then the library is already part of the node
  57. if (property_exists($node, 'library')) {
  58. $library = $node->library;
  59. $library_id = $library->library_id;
  60. $title = $library->name;
  61. $uniquename = $library->uniquename;
  62. $library_type = $library->type_id->cvterm_id;
  63. $organism_id = $library->organism_id->organism_id;
  64. $libprop = tripal_library_get_property($library->library_id, 'Library Description');
  65. $description = $libprop->value;
  66. // keep track of the library id if we have. If we do have one then
  67. // this is an update as opposed to an insert.
  68. $form['library_id'] = array(
  69. '#type' => 'value',
  70. '#value' => $library_id,
  71. );
  72. }
  73. // if we are re constructing the form from a failed validation or ajax callback
  74. // then use the $form_state['values'] values
  75. if (array_key_exists('values', $form_state)) {
  76. $title = $form_state['values']['title'];
  77. $uniquename = $form_state['values']['uniquename'];
  78. $library_type = $form_state['values']['library_type'];
  79. $organism_id = $form_state['values']['organism_id'];
  80. $description = $form_state['values']['description'];
  81. }
  82. // if we are re building the form from after submission (from ajax call) then
  83. // the values are in the $form_state['input'] array
  84. if (array_key_exists('input', $form_state) and !empty($form_state['input'])) {
  85. $title = $form_state['input']['title'];
  86. $uniquename = $form_state['input']['uniquename'];
  87. $library_type = $form_state['input']['library_type'];
  88. $organism_id = $form_state['input']['organism_id'];
  89. $description = $form_state['input']['description'];
  90. }
  91. $form['title']= array(
  92. '#type' => 'textfield',
  93. '#title' => t('Library Name'),
  94. '#description' => t('Please enter the name for this library. Library names should be recognizable but do not need to be unique.'),
  95. '#required' => TRUE,
  96. '#default_value' => $title,
  97. '#weight' => 1
  98. );
  99. $form['uniquename']= array(
  100. '#type' => 'textfield',
  101. '#title' => t('Unique Name'),
  102. '#description' => t('Please enter a unique name for this library. This can be any value used to uniquely identify a library.'),
  103. '#required' => TRUE,
  104. '#default_value' => $uniquename,
  105. '#weight' => 2
  106. );
  107. // get the list of library types
  108. $values = array(
  109. 'cv_id' => array(
  110. 'name' => 'library_type',
  111. )
  112. );
  113. $columns = array('cvterm_id','name');
  114. $options = array('order_by' => array('name' => 'ASC'));
  115. $lib_types = tripal_core_chado_select('cvterm', $columns, $values, $options);
  116. $types = array();
  117. $types[''] = '';
  118. foreach($lib_types as $type) {
  119. $types[$type->cvterm_id] = $type->name;
  120. }
  121. $form['library_type'] = array(
  122. '#title' => t('Library Type'),
  123. '#type' => t('select'),
  124. '#description' => t("Choose the library type."),
  125. '#required' => TRUE,
  126. '#default_value' => $library_type,
  127. '#options' => $types,
  128. '#weight' => 3
  129. );
  130. // get the list of organisms
  131. $sql = "SELECT * FROM {organism}";
  132. $org_rset = chado_query($sql);
  133. $organisms = array();
  134. $organisms[''] = '';
  135. while ($organism = $org_rset->fetchObject()) {
  136. $organisms[$organism->organism_id] =
  137. "$organism->genus $organism->species ($organism->common_name)";
  138. }
  139. $form['organism_id'] = array(
  140. '#title' => t('Organism'),
  141. '#type' => t('select'),
  142. '#description' => t("Choose the organism with which this library is associated."),
  143. '#required' => TRUE,
  144. '#default_value' => $organism_id,
  145. '#options' => $organisms,
  146. '#weight' => 4,
  147. );
  148. $form['description']= array(
  149. '#type' => 'textarea',
  150. '#title' => t('Library Description'),
  151. '#description' => t('A brief description of the library'),
  152. '#required' => TRUE,
  153. '#default_value' => $description,
  154. '#weight' => 5
  155. );
  156. return $form;
  157. }
  158. /**
  159. * validates submission of form when adding or updating a library node
  160. *
  161. * @ingroup tripal_library
  162. */
  163. function chado_library_validate($node, $form, &$form_state) {
  164. $node->title = trim($node->title);
  165. $node->uniquename = trim($node->uniquename);
  166. $node->description = trim($node->description);
  167. $lib = 0;
  168. // check to make sure the unique name on the library is unique
  169. // before we try to insert into chado.
  170. if (property_exists($node, 'library_id')) {
  171. $sql = "
  172. SELECT *
  173. FROM {library}
  174. WHERE uniquename = :uname AND NOT library_id = :library_id
  175. ";
  176. $lib = chado_query($sql, array(':uname' => $node->uniquename, ':library_id' => $node->library_id))->fetchObject();
  177. }
  178. else {
  179. $sql = "SELECT * FROM {library} WHERE uniquename = :uname";
  180. $lib = chado_query($sql, array(':uname' => $node->uniquename))->fetchObject();
  181. }
  182. if ($lib) {
  183. form_set_error('uniquename', t('The unique library name already exists. Please choose another'));
  184. }
  185. }
  186. /**
  187. * When a new chado_library node is created we also need to add information
  188. * to our chado_library table. This function is called on insert of a new node
  189. * of type 'chado_library' and inserts the necessary information.
  190. *
  191. * @ingroup tripal_library
  192. */
  193. function chado_library_insert($node) {
  194. $node->title = trim($node->title);
  195. $node->uniquename = trim($node->uniquename);
  196. $node->description = trim($node->description);
  197. // if there is an library_id in the $node object then this must be a sync so
  198. // we can skip adding the library as it is already there, although
  199. // we do need to proceed with the rest of the insert
  200. if (!property_exists($node, 'library_id')) {
  201. $values = array(
  202. 'name' => $node->title,
  203. 'uniquename' => $node->uniquename,
  204. 'organism_id' => $node->organism_id,
  205. 'type_id' => $node->library_type,
  206. );
  207. $library = tripal_core_chado_insert('library', $values);
  208. if (!$library) {
  209. drupal_set_message(t('Unable to add library.', 'warning'));
  210. watchdog('tripal_library', 'Insert library: Unable to create library where values: %values',
  211. array('%values' => print_r($values, TRUE)), WATCHDOG_ERROR);
  212. return;
  213. }
  214. $library_id = $library['library_id'];
  215. // add the description property
  216. tripal_library_insert_property($library_id, 'Library Description', $node->description);
  217. }
  218. else {
  219. $library_id = $node->library_id;
  220. }
  221. // Make sure the entry for this library doesn't already exist in the
  222. // chado_library table if it doesn't exist then we want to add it.
  223. $check_org_id = chado_get_id_for_node('library', $node->nid);
  224. if (!$check_org_id) {
  225. $record = new stdClass();
  226. $record->nid = $node->nid;
  227. $record->vid = $node->vid;
  228. $record->library_id = $library_id;
  229. drupal_write_record('chado_library', $record);
  230. }
  231. }
  232. /**
  233. * Update nodes
  234. *
  235. * @ingroup tripal_library
  236. */
  237. function chado_library_update($node) {
  238. $node->title = trim($node->title);
  239. $node->uniquename = trim($node->uniquename);
  240. $node->description = trim($node->description);
  241. // update the library record
  242. $library_id = chado_get_id_for_node('library', $node->nid);
  243. $match = array(
  244. 'library_id' => $library_id,
  245. );
  246. $values = array(
  247. 'name' => $node->title,
  248. 'uniquename' => $node->uniquename,
  249. 'organism_id' => $node->organism_id,
  250. 'type_id' => $node->library_type,
  251. );
  252. $status = tripal_core_chado_update('library', $match, $values);
  253. if (!$status) {
  254. drupal_set_message(t('Unable to update library.', 'warning'));
  255. watchdog('tripal_library', 'Update library: Unable to update library where values: %values',
  256. array('%values' => print_r($values, TRUE)), WATCHDOG_ERROR);
  257. }
  258. // add in the library description as a property
  259. tripal_library_update_property($library_id, 'Library Description', $node->description, 1);
  260. }
  261. /**
  262. * When a node is requested by the user this function is called to allow us
  263. * to add auxiliary data to the node object.
  264. *
  265. * @ingroup tripal_library
  266. */
  267. function chado_library_load($nodes) {
  268. foreach ($nodes as $nid => $node) {
  269. // get the feature details from chado
  270. $library_id = chado_get_id_for_node('library', $node->nid);
  271. $values = array('library_id' => $library_id);
  272. $library = tripal_core_generate_chado_var('library', $values);
  273. // the uniquename field is a text field so we need to expand it
  274. $library = tripal_core_expand_chado_vars($library, 'field', 'library.uniquename');
  275. $nodes[$nid]->library = $library;
  276. }
  277. }
  278. /**
  279. * Delete data from drupal and chado databases when a node is deleted
  280. * @ingroup tripal_library
  281. */
  282. function chado_library_delete(&$node) {
  283. $library_id = chado_get_id_for_node('library', $node->nid);
  284. // if we don't have a library id for this node then this isn't a node of
  285. // type chado_library or the entry in the chado_library table was lost.
  286. if (!$library_id) {
  287. return;
  288. }
  289. // Remove data from {chado_library}, {node} and {node_revisions} tables of
  290. // drupal database
  291. $sql_del = "DELETE FROM {chado_library} WHERE nid = :nid AND vid = :vid";
  292. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  293. $sql_del = "DELETE FROM {node_revision} WHERE nid = :nid AND vid = :vid";
  294. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  295. $sql_del = "DELETE FROM {node} WHERE nid = :nid AND vid = :vid";
  296. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  297. // Remove data from library and libraryprop tables of chado database as well
  298. chado_query("DELETE FROM {libraryprop} WHERE library_id = :library_id", array(':library_id' => $library_id));
  299. chado_query("DELETE FROM {library} WHERE library_id = :library_id", array(':library_id' => $library_id));
  300. }
  301. /**
  302. * Implement hook_access().
  303. *
  304. * This hook allows node modules to limit access to the node types they define.
  305. *
  306. * @param $node
  307. * The node on which the operation is to be performed, or, if it does not yet exist, the
  308. * type of node to be created
  309. *
  310. * @param $op
  311. * The operation to be performed
  312. *
  313. * @param $account
  314. * A user object representing the user for whom the operation is to be performed
  315. *
  316. * @return
  317. * If the permission for the specified operation is not set then return FALSE. If the
  318. * permission is set then return NULL as this allows other modules to disable
  319. * access. The only exception is when the $op == 'create'. We will always
  320. * return TRUE if the permission is set.
  321. *
  322. * @ingroup tripal_library
  323. */
  324. function chado_library_node_access($node, $op, $account) {
  325. if ($op == 'create') {
  326. if (!user_access('create chado_library content', $account)) {
  327. return FALSE;
  328. }
  329. return TRUE;
  330. }
  331. if ($op == 'update') {
  332. if (!user_access('edit chado_library content', $account)) {
  333. return FALSE;
  334. }
  335. }
  336. if ($op == 'delete') {
  337. if (!user_access('delete chado_library content', $account)) {
  338. return FALSE;
  339. }
  340. }
  341. if ($op == 'view') {
  342. if (!user_access('access chado_library content', $account)) {
  343. return FALSE;
  344. }
  345. }
  346. return NULL;
  347. }
  348. /**
  349. * @ingroup tripal_library
  350. */
  351. function tripal_library_node_view($node, $view_mode, $langcode) {
  352. switch ($node->type) {
  353. case 'chado_library':
  354. if ($view_mode == 'full') {
  355. $node->content['tripal_library_base'] = array(
  356. '#value' => theme('tripal_library_base', array('node' => $node)),
  357. '#tripal_toc_id' => 'base',
  358. '#tripal_toc_title' => 'Details',
  359. '#weight' => 0,
  360. );
  361. $node->content['tripal_library_properties'] = array(
  362. '#value' => theme('tripal_library_properties', array('node' => $node)),
  363. '#tripal_toc_id' => 'properties',
  364. '#tripal_toc_title' => 'Properties',
  365. );
  366. $node->content['tripal_library_publications'] = array(
  367. '#value' => theme('tripal_library_publications', array('node' => $node)),
  368. '#tripal_toc_id' => 'publications',
  369. '#tripal_toc_title' => 'Publications',
  370. );
  371. $node->content['tripal_library_references'] = array(
  372. '#value' => theme('tripal_library_references', array('node' => $node)),
  373. '#tripal_toc_id' => 'references',
  374. '#tripal_toc_title' => 'References',
  375. );
  376. $node->content['tripal_library_synonyms'] = array(
  377. '#value' => theme('tripal_library_synonyms', array('node' => $node)),
  378. '#tripal_toc_id' => 'synonyms',
  379. '#tripal_toc_title' => 'Synonyms',
  380. );
  381. $node->content['tripal_library_terms'] = array(
  382. '#value' => theme('tripal_library_terms', array('node' => $node)),
  383. '#tripal_toc_id' => 'terms',
  384. '#tripal_toc_title' => 'Annotated Terms',
  385. );
  386. }
  387. if ($view_mode == 'teaser') {
  388. $node->content['tripal_library_teaser'] = array(
  389. '#value' => theme('tripal_library_teaser', array('node' => $node)),
  390. );
  391. }
  392. break;
  393. case 'chado_organism':
  394. if ($view_mode == 'full') {
  395. $node->content['tripal_organism.libraries'] = array(
  396. '#value' => theme('tripal_organism.libraries', array('node' => $node)),
  397. '#tripal_toc_id' => 'libraries',
  398. '#tripal_toc_title' => 'Libraries',
  399. );
  400. }
  401. break;
  402. case 'chado_feature':
  403. if ($view_mode == 'full') {
  404. $node->content['tripal_feature.libraries'] = array(
  405. '#value' => theme('tripal_feature.libraries', array('node' => $node)),
  406. '#tripal_toc_id' => 'libraries',
  407. '#tripal_toc_title' => 'Libraries',
  408. );
  409. }
  410. break;
  411. }
  412. }
  413. /**
  414. *
  415. * @param $node
  416. */
  417. function tripal_library_node_presave($node) {
  418. switch ($node->type) {
  419. case 'chado_library':
  420. // for a form submission the fields part of the node object
  421. // but for a sync the feilds are in an object of the node
  422. if(property_exists($node, 'library')) {
  423. // set the title
  424. $node->title = $node->name;
  425. }
  426. else {
  427. // the title field is already in the form
  428. }
  429. break;
  430. }
  431. }