tripal_library.chado_node.inc 19 KB

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