tripal_library.module 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  1. <?php
  2. /**
  3. * @defgroup tripal_library Library Module
  4. * @ingroup tripal_modules
  5. * @{
  6. * Provides functions for managing chado libraries including creating details pages for each library
  7. * @}
  8. */
  9. require('api/tripal_library.api.inc');
  10. require('includes/tripal_library.admin.inc');
  11. /**
  12. * Provide information to drupal about the node types that we're creating
  13. * in this module
  14. *
  15. * @ingroup tripal_library
  16. */
  17. function tripal_library_node_info() {
  18. $nodes = array();
  19. $nodes['chado_library'] = array(
  20. 'name' => t('Library'),
  21. 'base' => 'chado_library',
  22. 'description' => t('A library from the chado database'),
  23. 'has_title' => FALSE,
  24. 'title_label' => t('Library'),
  25. 'has_body' => FALSE,
  26. 'locked' => TRUE
  27. );
  28. return $nodes;
  29. }
  30. /**
  31. * Set the permission types that the chado module uses. Essentially we
  32. * want permissionis that protect creation, editing and deleting of chado
  33. * data objects
  34. *
  35. * @ingroup tripal_library
  36. */
  37. function tripal_library_permisssions() {
  38. return array(
  39. 'access chado_library content' => array(
  40. 'title' => t('View Libraries'),
  41. 'description' => t('Allow users to view library pages.'),
  42. ),
  43. 'create chado_library content' => array(
  44. 'title' => t('Create Libraries'),
  45. 'description' => t('Allow users to create new library pages.'),
  46. ),
  47. 'delete chado_library content' => array(
  48. 'title' => t('Delete Libraries'),
  49. 'description' => t('Allow users to delete library pages.'),
  50. ),
  51. 'edit chado_library content' => array(
  52. 'title' => t('Edit Libraries'),
  53. 'description' => t('Allow users to edit library pages.'),
  54. ),
  55. 'adminster tripal library' => array(
  56. 'title' => t('Administer Libraries'),
  57. 'description' => t('Allow users to administer all libraries.'),
  58. ),
  59. );
  60. }
  61. /**
  62. * Implement hook_access().
  63. *
  64. * This hook allows node modules to limit access to the node types they define.
  65. *
  66. * @param $node
  67. * The node on which the operation is to be performed, or, if it does not yet exist, the
  68. * type of node to be created
  69. *
  70. * @param $op
  71. * The operation to be performed
  72. *
  73. * @param $account
  74. * A user object representing the user for whom the operation is to be performed
  75. *
  76. * @return
  77. * If the permission for the specified operation is not set then return FALSE. If the
  78. * permission is set then return NULL as this allows other modules to disable
  79. * access. The only exception is when the $op == 'create'. We will always
  80. * return TRUE if the permission is set.
  81. *
  82. * @ingroup tripal_library
  83. */
  84. function chado_library_node_access($node, $op, $account) {
  85. if ($op == 'create') {
  86. if (!user_access('create chado_library content', $account)) {
  87. return FALSE;
  88. }
  89. return TRUE;
  90. }
  91. if ($op == 'update') {
  92. if (!user_access('edit chado_library content', $account)) {
  93. return FALSE;
  94. }
  95. }
  96. if ($op == 'delete') {
  97. if (!user_access('delete chado_library content', $account)) {
  98. return FALSE;
  99. }
  100. }
  101. if ($op == 'view') {
  102. if (!user_access('access chado_library content', $account)) {
  103. return FALSE;
  104. }
  105. }
  106. return NULL;
  107. }
  108. /**
  109. * Menu items are automatically added for the new node types created
  110. * by this module to the 'Create Content' Navigation menu item. This function
  111. * adds more menu items needed for this module.
  112. *
  113. * @ingroup tripal_library
  114. */
  115. function tripal_library_menu() {
  116. $items = array();
  117. // The administative settings menu
  118. $items['admin/tripal/tripal_library'] = array(
  119. 'title' => 'Libraries',
  120. 'description' => 'Basic Description of Tripal Library Module Functionality',
  121. 'page callback' => 'theme',
  122. 'page arguments' => array('tripal_library_help'),
  123. 'access arguments' => array('administer tripal libraries'),
  124. 'type' => MENU_NORMAL_ITEM,
  125. );
  126. $items['admin/tripal/tripal_library/configuration'] = array(
  127. 'title' => 'Configuration',
  128. 'description' => 'Configure the Tripal Library module',
  129. 'page callback' => 'drupal_get_form',
  130. 'page arguments' => array('tripal_library_help'),
  131. 'access arguments' => array('administer tripal libraries'),
  132. 'type' => MENU_NORMAL_ITEM,
  133. );
  134. // Synchronizing libraries from Chado to Drupal
  135. $items['chado_sync_libraries'] = array(
  136. 'title' => 'Sync Library Data',
  137. 'page callback' => 'tripal_library_sync_libraries',
  138. 'access arguments' => array('administer tripal libraries'),
  139. 'type' => MENU_CALLBACK
  140. );
  141. return $items;
  142. }
  143. /**
  144. * Implements hook_views_api()
  145. * Purpose: Essentially this hook tells drupal that there is views support for
  146. * for this module which then includes tripal_db.views.inc where all the
  147. * views integration code is
  148. *
  149. * @ingroup tripal_library
  150. */
  151. function tripal_library_views_api() {
  152. return array(
  153. 'api' => 2.0,
  154. );
  155. }
  156. /**
  157. * @ingroup tripal_library
  158. */
  159. function tripal_library_node_view(&$node, $view_mode, $langcode) {
  160. switch ($node->type) {
  161. case 'chado_organism':
  162. if ($view_mode == 'full') {
  163. $node->content['tripal_organism_libraries'] = array(
  164. '#value' => theme('tripal_organism_libraries', $node),
  165. );
  166. }
  167. break;
  168. case 'chado_feature':
  169. if ($view_mode == 'full') {
  170. $node->content['tripal_feature_libraries'] = array(
  171. '#value' => theme('tripal_feature_libraries', $node),
  172. );
  173. }
  174. break;
  175. }
  176. }
  177. /**
  178. * We need to let drupal know about our theme functions and their arguments.
  179. * We create theme functions to allow users of the module to customize the
  180. * look and feel of the output generated in this module
  181. *
  182. * @ingroup tripal_library
  183. */
  184. function tripal_library_theme() {
  185. $theme_path = drupal_get_path('module', 'tripal_library') . '/theme';
  186. $items = array(
  187. // themed functions
  188. 'tripal_library_library_table' => array(
  189. 'arguments' => array('libraries'),
  190. ),
  191. 'tripal_library_search_index' => array(
  192. 'arguments' => array('node'),
  193. ),
  194. 'tripal_library_search_result' => array(
  195. 'arguments' => array('node'),
  196. ),
  197. // tripal_organism templates
  198. 'tripal_organism_libraries' => array(
  199. 'arguments' => array('node' => NULL),
  200. 'template' => 'tripal_organism_libraries',
  201. 'path' => "$theme_path/tripal_organism",
  202. ),
  203. // tripal_feature templates
  204. 'tripal_feature_libraries' => array(
  205. 'arguments' => array('node' => NULL),
  206. 'template' => 'tripal_feature_libraries',
  207. 'path' => "$theme_path/tripal_feature",
  208. ),
  209. // tripal_library templates
  210. 'tripal_library_base' => array(
  211. 'arguments' => array('node' => NULL),
  212. 'template' => 'tripal_library_base',
  213. 'path' => "$theme_path/tripal_library",
  214. ),
  215. 'tripal_library_synonyms' => array(
  216. 'arguments' => array('node' => NULL),
  217. 'template' => 'tripal_library_synonyms',
  218. 'path' => "$theme_path/tripal_library",
  219. ),
  220. 'tripal_library_references' => array(
  221. 'arguments' => array('node' => NULL),
  222. 'template' => 'tripal_library_references',
  223. 'path' => "$theme_path/tripal_library",
  224. ),
  225. 'tripal_library_properties' => array(
  226. 'arguments' => array('node' => NULL),
  227. 'template' => 'tripal_library_properties',
  228. 'path' => "$theme_path/tripal_library",
  229. ),
  230. 'tripal_library_terms' => array(
  231. 'arguments' => array('node' => NULL),
  232. 'template' => 'tripal_library_terms',
  233. 'path' => "$theme_path/tripal_library",
  234. ),
  235. // help template
  236. 'tripal_library_help' => array(
  237. 'template' => 'tripal_library_help',
  238. 'arguments' => array(NULL),
  239. 'path' => $theme_path,
  240. ),
  241. );
  242. return $items;
  243. }
  244. /**
  245. * Implements hook_help()
  246. * Purpose: Adds a help page to the module list
  247. */
  248. function tripal_library_help ($path, $arg) {
  249. if ($path == 'admin/help#tripal_library') {
  250. return theme('tripal_library_help', array());
  251. }
  252. }
  253. /**
  254. * @ingroup tripal_library
  255. */
  256. function tripal_library_block_info() {
  257. $blocks['libreferences']['info'] = t('Tripal Library Cross References');
  258. $blocks['libreferences']['cache'] = DRUPAL_NO_CACHE;
  259. $blocks['libbase']['info'] = t('Tripal Library Details');
  260. $blocks['libbase']['cache'] = DRUPAL_NO_CACHE;
  261. $blocks['libterms']['info'] = t('Tripal Library Terms');
  262. $blocks['libterms']['cache'] = DRUPAL_NO_CACHE;
  263. $blocks['libsynonyms']['info'] = t('Tripal Library Synonyms');
  264. $blocks['libsynonyms']['cache'] = DRUPAL_NO_CACHE;
  265. $blocks['libproperties']['info'] = t('Tripal Library Properties');
  266. $blocks['libproperties']['cache'] = DRUPAL_NO_CACHE;
  267. $blocks['featurelibs']['info'] = t('Tripal Feature Libraries');
  268. $blocks['featurelibs']['cache'] = DRUPAL_NO_CACHE;
  269. $blocks['orglibs']['info'] = t('Tripal Organism Libraries');
  270. $blocks['orglibs']['cache'] = DRUPAL_NO_CACHE;
  271. return $blocks;
  272. }
  273. /**
  274. * @ingroup tripal_library
  275. */
  276. function tripal_library_block_view($delta = '') {
  277. if (user_access('access chado_library content') and arg(0) == 'node' and is_numeric(arg(1))) {
  278. $nid = arg(1);
  279. $node = node_load($nid);
  280. $block = array();
  281. switch ($delta) {
  282. case 'libreferences':
  283. $block['subject'] = t('Cross References');
  284. $block['content'] = theme('tripal_library_references', $node);
  285. break;
  286. case 'libbase':
  287. $block['subject'] = t('Library Details');
  288. $block['content'] = theme('tripal_library_base', $node);
  289. break;
  290. case 'libsynonyms':
  291. $block['subject'] = t('Synonyms');
  292. $block['content'] = theme('tripal_library_synonyms', $node);
  293. break;
  294. case 'libproperties':
  295. $block['subject'] = t('Properties');
  296. $block['content'] = theme('tripal_library_properties', $node);
  297. break;
  298. case 'libterms':
  299. $block['subject'] = t('Library Terms');
  300. $block['content'] = theme('tripal_library_terms', $node);
  301. break;
  302. case 'featurelibs':
  303. $block['subject'] = t('Libraries');
  304. $block['content'] = theme('tripal_feature_libraries', $node);
  305. break;
  306. case 'orglibs':
  307. $block['subject'] = t('Libraries');
  308. $block['content'] = theme('tripal_organism_libraries', $node);
  309. break;
  310. default :
  311. }
  312. return $block;
  313. }
  314. }
  315. /**
  316. * This function is an extension of the chado_feature_view and
  317. * chado_organism_view by providing the markup for the library object
  318. * THAT WILL BE INDEXED.
  319. *
  320. * @ingroup tripal_library
  321. */
  322. function theme_tripal_library_search_index($node) {
  323. if ($node->type == 'chado_organism') {
  324. $content = "";
  325. // get the libraries for the organism
  326. $sql = "SELECT * FROM {library} L WHERE L.organism_id = :organism_id";
  327. $libraries = array();
  328. $results = chado_query($sql, array(':organism_id' => $node->organism->organism_id));
  329. while ($library = $results->fetchObject()) {
  330. // get the description
  331. $sql = "
  332. SELECT *
  333. FROM {libraryprop} LP
  334. INNER JOIN {cvterm} CVT ON CVT.cvterm_id = LP.type_id
  335. WHERE LP.library_id = :library_id
  336. AND CVT.name = 'library_description'
  337. ";
  338. $desc = chado_query($sql, array(':library_id' => $library->library_id))->fetchObject();
  339. $library->description = $desc->value;
  340. $libraries[] = $library;
  341. }
  342. if (count($libraries) > 0) {
  343. foreach ($libraries as $library) {
  344. $content .= "$library->name ";
  345. $content .= "$library->description";
  346. };
  347. }
  348. // Provide library names to show in a feature page
  349. }
  350. elseif ($node->type == 'chado_feature') {
  351. $content = "";
  352. $organism_id = $node->feature->organism_id;
  353. $sql = "
  354. SELECT *
  355. FROM {library} L
  356. INNER JOIN {library_feature} LF ON L.library_id = LF.library_id
  357. WHERE LF.feature_id = :feature_id
  358. ";
  359. $libraries = array();
  360. $results = chado_query($sql, array(':feature_id' => $node->feature->feature_id));
  361. while ($library = $results->fetchObject()) {
  362. $libraries[] = $library;
  363. }
  364. if (count($libraries) > 0) {
  365. $lib_additions = array();
  366. foreach ($libraries as $library) {
  367. $content .= $library->name;
  368. };
  369. }
  370. }
  371. return $content;
  372. }
  373. /**
  374. * This function shows library information on an organism/feature node
  375. *
  376. * @ingroup tripal_library
  377. */
  378. function theme_tripal_library_node_libraries($node) {
  379. $content = "";
  380. // Show library information in a expandable box for a organism page.
  381. // Make sure we have $node->organism_id. In the case of creating a new
  382. // organism, the organism_id is not created until we save. This will cause
  383. // an error when users preview the creation without a $node->organism_id
  384. if ($node->type == 'chado_organism' && $node->organism_id) {
  385. $box_status = variable_get("tripal_library-box-libraries", "menu_off");
  386. if (strcmp($box_status, "menu_off")==0) {
  387. return get_tripal_library_organism_libraries($node->nid);
  388. }
  389. }
  390. // Provide library names to show in a feature page.
  391. // Make sure we have $node->feature->feature_id or there will be an error
  392. // when a feature is previewed at its creation
  393. elseif ($node->type == 'chado_feature' && $node->feature->feature_id) {
  394. $organism_id = $node->feature->organism_id;
  395. $sql = "
  396. SELECT *
  397. FROM {library} L
  398. INNER JOIN Library_feature LF ON L.library_id = LF.library_id
  399. WHERE LF.feature_id = :feature_id
  400. ";
  401. $libraries = array();
  402. $results = chado_query($sql, array(':feature_id' => $node->feature->feature_id));
  403. while ($library = $results->fetchObject()) {
  404. $libraries[] = $library;
  405. }
  406. if (count($libraries) > 0) {
  407. $lib_additions = array();
  408. foreach ($libraries as $library) {
  409. $sql = "SELECT nid FROM {chado_library} WHERE library_id = :library_id";
  410. $lib_nid = db_query($sql, array(':library_id' => $library->library_id))->fetchField();
  411. if ($lib_nid) {
  412. $lib_url = url("node/$lib_nid");
  413. }
  414. $lib_additions[$lib_url] = $library->name;
  415. };
  416. $node->lib_additions = $lib_additions;
  417. }
  418. }
  419. return $content;
  420. }
  421. /**
  422. *
  423. * @ingroup tripal_library
  424. */
  425. function tripal_library_cron() {
  426. }
  427. /**
  428. * When editing or creating a new node of type 'chado_library' we need
  429. * a form. This function creates the form that will be used for this.
  430. *
  431. * @ingroup tripal_library
  432. */
  433. function chado_library_form($node) {
  434. $form = array();
  435. $library = $node->library;
  436. // get the default values
  437. $uniquename = $node->uniquename;
  438. if (!$uniquename) {
  439. $uniquename = $library->uniquename;
  440. }
  441. $library_type = $node->library_type;
  442. if (!$library_type) {
  443. $library_type = $library->type_id->cvterm_id;
  444. }
  445. $organism_id = $node->organism_id;
  446. if (!$organism_id) {
  447. $organism_id = $library->organism_id->organism_id;
  448. }
  449. $library_description = $node->library_description;
  450. if (!$library_description) {
  451. $libprop = tripal_library_get_property($library->library_id, 'library_description');
  452. $library_description = $libprop->value;
  453. }
  454. // keep track of the library id if we have. If we do have one then
  455. // this is an update as opposed to an insert.
  456. $form['library_id'] = array(
  457. '#type' => 'value',
  458. '#value' => $library->library_id,
  459. );
  460. $form['title']= array(
  461. '#type' => 'textfield',
  462. '#title' => t('Library Title'),
  463. '#description' => t('Please enter the title for this library. '.
  464. 'This appears at the top of the library page.'),
  465. '#required' => TRUE,
  466. '#default_value' => $node->title,
  467. '#weight' => 1
  468. );
  469. $form['uniquename']= array(
  470. '#type' => 'textfield',
  471. '#title' => t('Unique Library Name'),
  472. '#description' => t('Please enter a unique name for this library'),
  473. '#required' => TRUE,
  474. '#default_value' => $uniquename,
  475. '#weight' => 2
  476. );
  477. // get the list of library types
  478. $values = array(
  479. 'cv_id' => array(
  480. 'name' => 'tripal_library_types',
  481. )
  482. );
  483. $columns = array('cvterm_id','name');
  484. $options = array('order_by' => array('name' => 'ASC'));
  485. $lib_types = tripal_core_chado_select('cvterm', $columns, $values, $options);
  486. $types = array();
  487. $types[''] = '';
  488. foreach($lib_types as $type) {
  489. $types[$type->cvterm_id] = $type->name;
  490. }
  491. $form['library_type'] = array(
  492. '#title' => t('Library Type'),
  493. '#type' => t('select'),
  494. '#description' => t("Choose the library type."),
  495. '#required' => TRUE,
  496. '#default_value' => $library_type,
  497. '#options' => $types,
  498. '#weight' => 3
  499. );
  500. // get the list of organisms
  501. $sql = "SELECT * FROM {organism}";
  502. $org_rset = chado_query($sql);
  503. $organisms = array();
  504. $organisms[''] = '';
  505. while ($organism = $org_rset->fetchObject()) {
  506. $organisms[$organism->organism_id] =
  507. "$organism->genus $organism->species ($organism->common_name)";
  508. }
  509. $form['organism_id'] = array(
  510. '#title' => t('Organism'),
  511. '#type' => t('select'),
  512. '#description' => t("Choose the organism with which this library is ".
  513. "associated."),
  514. '#required' => TRUE,
  515. '#default_value' => $organism_id,
  516. '#options' => $organisms,
  517. '#weight' => 4,
  518. );
  519. $form['library_description']= array(
  520. '#type' => 'textarea',
  521. '#title' => t('Library Description'),
  522. '#description' => t('A brief description of the library'),
  523. '#required' => TRUE,
  524. '#default_value' => $library_description,
  525. '#weight' => 5
  526. );
  527. return $form;
  528. }
  529. /**
  530. * validates submission of form when adding or updating a library node
  531. *
  532. * @ingroup tripal_library
  533. */
  534. function chado_library_validate($node) {
  535. $lib = 0;
  536. // check to make sure the unique name on the library is unique
  537. // before we try to insert into chado.
  538. if ($node->library_id) {
  539. $sql = "
  540. SELECT *
  541. FROM {library}
  542. WHERE uniquename = :uname AND NOT library_id = :library_id
  543. ";
  544. $lib = chado_query($sql, array(':uname' => $node->uniquename, ':library_id' => $node->library_id))->fetchObject();
  545. }
  546. else {
  547. $sql = "SELECT * FROM {library} WHERE uniquename = :uname";
  548. $lib = chado_query($sql, array(':uname' => $node->uniquename))->fetchObject();
  549. }
  550. if ($lib) {
  551. form_set_error('uniquename', t('The unique library name already exists. Please choose another'));
  552. }
  553. }
  554. /**
  555. * When a new chado_library node is created we also need to add information
  556. * to our chado_library table. This function is called on insert of a new node
  557. * of type 'chado_library' and inserts the necessary information.
  558. *
  559. * @ingroup tripal_library
  560. */
  561. function chado_library_insert($node) {
  562. if ($node->library_id) {
  563. $library['library_id'] = $node->library_id;
  564. }
  565. else {
  566. $values = array(
  567. 'name' => $node->title,
  568. 'uniquename' => $node->uniquename,
  569. 'organism_id' => $node->organism_id,
  570. 'type_id' => $node->library_type,
  571. );
  572. $library = tripal_core_chado_insert('library', $values);
  573. }
  574. if ($library) {
  575. // add the description property
  576. tripal_library_insert_property($library['library_id'], 'library_description',
  577. $node->library_description);
  578. // make sure the entry for this feature doesn't already exist in the chado_library table
  579. // if it doesn't exist then we want to add it.
  580. $library_id = chado_get_id_for_node('library', $node->nid) ;
  581. if (!$library_id) {
  582. // next add the item to the drupal table
  583. $sql = "
  584. INSERT INTO {chado_library} (nid, vid, library_id)
  585. VALUES (:nid, :vid, :library_id)
  586. ";
  587. db_query($sql, array(':nid' => $node->nid, ':vid' => $node->vid, ':library_id' => $library['library_id']));
  588. }
  589. }
  590. else {
  591. drupal_set_message(t('Unable to add library.', 'warning'));
  592. watchdog('tripal_library', 'Insert feature: Unable to create library where values: %values',
  593. array('%values' => print_r($values, TRUE)), WATCHDOG_WARNING);
  594. }
  595. }
  596. /**
  597. * Update nodes
  598. *
  599. * @ingroup tripal_library
  600. */
  601. function chado_library_update($node) {
  602. if ($node->revision) {
  603. // there is no way to handle revisions in Chado but leave
  604. // this here just to make not we've addressed it.
  605. }
  606. $library_id = chado_get_id_for_node('library', $node->nid);
  607. // update the library record
  608. $match = array(
  609. 'library_id' => $library_id,
  610. );
  611. $values = array(
  612. 'name' => $node->title,
  613. 'uniquename' => $node->uniquename,
  614. 'organism_id' => $node->organism_id,
  615. 'type_id' => $node->library_type,
  616. );
  617. $status = tripal_core_chado_update('library', $match, $values);
  618. tripal_library_update_property($library_id, 'library_description', $node->library_description, 1);
  619. }
  620. /**
  621. * When a node is requested by the user this function is called to allow us
  622. * to add auxiliary data to the node object.
  623. *
  624. * @ingroup tripal_library
  625. */
  626. function chado_library_load($node) {
  627. // get the feature details from chado
  628. $library_id = chado_get_id_for_node('library', $node->nid);
  629. $values = array('library_id' => $library_id);
  630. $library = tripal_core_generate_chado_var('library', $values);
  631. $additions = new stdClass();
  632. $additions->library = $library;
  633. return $additions;
  634. }
  635. /**
  636. * This function customizes the view of the chado_library node. It allows
  637. * us to generate the markup. This function is required for node [Preview]
  638. *
  639. * @ingroup tripal_library
  640. */
  641. function chado_library_view($node, $teaser = FALSE, $page = FALSE) {
  642. // use drupal's default node view:
  643. if (!$teaser) {
  644. $node = node_prepare($node, $teaser);
  645. // If Hook_view() is called by Hook_form(), we'll only have orgnism_id
  646. // but not genus/species/common_name. We need to get those from chado
  647. // database so they will show up in preview
  648. if (!$node->genus) {
  649. $sql = "SELECT * FROM {organism} WHERE organism_id = :organism_id";
  650. $data = chado_query($sql, array(':organism_id' => $node->organism_id))->fetchObject();
  651. $node->genus = $data->genus;
  652. $node->species = $data->species;
  653. $node->common_name = $data->common_name;
  654. }
  655. }
  656. return $node;
  657. }
  658. /**
  659. * Delete data from drupal and chado databases when a node is deleted
  660. * @ingroup tripal_library
  661. */
  662. function chado_library_delete(&$node) {
  663. $library_id = chado_get_id_for_node('library', $node->nid);
  664. // if we don't have a library id for this node then this isn't a node of
  665. // type chado_library or the entry in the chado_library table was lost.
  666. if (!$library_id) {
  667. return;
  668. }
  669. // Remove data from {chado_library}, {node} and {node_revisions} tables of
  670. // drupal database
  671. $sql_del = "DELETE FROM {chado_library} WHERE nid = :nid AND vid = :vid";
  672. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  673. $sql_del = "DELETE FROM {node_revision} WHERE nid = :nid AND vid = :vid";
  674. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  675. $sql_del = "DELETE FROM {node} WHERE nid = :nid AND vid = :vid";
  676. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  677. // Remove data from library and libraryprop tables of chado database as well
  678. chado_query("DELETE FROM {libraryprop} WHERE library_id = :library_id", array(':library_id' => $library_id));
  679. chado_query("DELETE FROM {library} WHERE library_id = :library_id", array(':library_id' => $library_id));
  680. }