tripal_library.chado_node.inc 19 KB

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