tripal_library.module 24 KB

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