tripal_library.chado_node.inc 21 KB

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