tripal_library.chado_node.inc 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  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. $libraryname = '';
  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. $libraryname = $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)) {
  76. $libraryname = $form_state['values']['libraryname'];
  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. $libraryname = $form_state['input']['libraryname'];
  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['libraryname']= 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' => $libraryname,
  97. );
  98. $form['uniquename']= array(
  99. '#type' => 'textfield',
  100. '#title' => t('Unique Name'),
  101. '#description' => t('Please enter a unique name for this library. This can be any value used to uniquely identify a library.'),
  102. '#required' => TRUE,
  103. '#default_value' => $uniquename,
  104. );
  105. // get the list of library types
  106. $values = array(
  107. 'cv_id' => array(
  108. 'name' => 'library_type',
  109. )
  110. );
  111. $columns = array('cvterm_id','name');
  112. $options = array('order_by' => array('name' => 'ASC'));
  113. $lib_types = chado_select_record('cvterm', $columns, $values, $options);
  114. $types = array();
  115. $types[''] = '';
  116. foreach($lib_types as $type) {
  117. $types[$type->cvterm_id] = $type->name;
  118. }
  119. $form['library_type'] = array(
  120. '#title' => t('Library Type'),
  121. '#type' => t('select'),
  122. '#description' => t("Choose the library type."),
  123. '#required' => TRUE,
  124. '#default_value' => $library_type,
  125. '#options' => $types,
  126. );
  127. // get the list of organisms
  128. $sql = "SELECT * FROM {organism}";
  129. $org_rset = chado_query($sql);
  130. $organisms = array();
  131. $organisms[''] = '';
  132. while ($organism = $org_rset->fetchObject()) {
  133. $organisms[$organism->organism_id] =
  134. "$organism->genus $organism->species ($organism->common_name)";
  135. }
  136. $form['organism_id'] = array(
  137. '#title' => t('Organism'),
  138. '#type' => t('select'),
  139. '#description' => t("Choose the organism with which this library is associated."),
  140. '#required' => TRUE,
  141. '#default_value' => $organism_id,
  142. '#options' => $organisms,
  143. );
  144. $form['description']= array(
  145. '#type' => 'textarea',
  146. '#title' => t('Library Description'),
  147. '#description' => t('A brief description of the library'),
  148. '#required' => TRUE,
  149. '#default_value' => $description,
  150. );
  151. // PROPERTIES FORM
  152. //---------------------------------------------
  153. // Generate our own select list so we can desclude the description since it has it's
  154. // own form element above
  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. $details = array(
  161. 'property_table' => 'libraryprop', // the name of the prop table
  162. 'base_foreign_key' => 'library_id', // the name of the key in your base chado table
  163. 'base_key_value' => $library_id, // the value of library_id for this record
  164. 'cv_name' => 'library_property', // the cv.name 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. * validates submission of form when adding or updating a library node
  182. *
  183. * @ingroup tripal_library
  184. */
  185. function chado_library_validate($node, $form, &$form_state) {
  186. $node->libraryname = trim($node->libraryname);
  187. $node->uniquename = trim($node->uniquename);
  188. $node->description = trim($node->description);
  189. $lib = 0;
  190. // check to make sure the unique name on the library is unique
  191. // before we try to insert into chado.
  192. if (property_exists($node, 'library_id')) {
  193. $sql = "
  194. SELECT *
  195. FROM {library}
  196. WHERE uniquename = :uname AND NOT library_id = :library_id
  197. ";
  198. $lib = chado_query($sql, array(':uname' => $node->uniquename, ':library_id' => $node->library_id))->fetchObject();
  199. }
  200. else {
  201. $sql = "SELECT * FROM {library} WHERE uniquename = :uname";
  202. $lib = chado_query($sql, array(':uname' => $node->uniquename))->fetchObject();
  203. }
  204. if ($lib) {
  205. form_set_error('uniquename', t('The unique library name already exists. Please choose another'));
  206. }
  207. }
  208. /**
  209. * When a new chado_library node is created we also need to add information
  210. * to our chado_library table. This function is called on insert of a new node
  211. * of type 'chado_library' and inserts the necessary information.
  212. *
  213. * @ingroup tripal_library
  214. */
  215. function chado_library_insert($node) {
  216. $node->libraryname = trim($node->libraryname);
  217. $node->uniquename = trim($node->uniquename);
  218. $node->description = trim($node->description);
  219. // if there is an library_id in the $node object then this must be a sync so
  220. // we can skip adding the library as it is already there, although
  221. // we do need to proceed with the rest of the insert
  222. if (!property_exists($node, 'library_id')) {
  223. $values = array(
  224. 'name' => $node->libraryname,
  225. 'uniquename' => $node->uniquename,
  226. 'organism_id' => $node->organism_id,
  227. 'type_id' => $node->library_type,
  228. );
  229. $library = chado_insert_record('library', $values);
  230. if (!$library) {
  231. drupal_set_message(t('Unable to add library.', 'warning'));
  232. watchdog('tripal_library', 'Insert library: Unable to create library where values: %values',
  233. array('%values' => print_r($values, TRUE)), WATCHDOG_ERROR);
  234. return;
  235. }
  236. $library_id = $library['library_id'];
  237. // * Properties Form *
  238. // add the description property
  239. $properties = chado_retrieve_node_form_properties($node);
  240. $descrip_id = tripal_cv_get_cvterm_by_name('Library Description', NULL, 'library_property');
  241. $properties[$descrip_id->cvterm_id][0] = $node->description;
  242. $details = array(
  243. 'property_table' => 'libraryprop', // the name of the prop table
  244. 'base_table' => 'library', // the name of your chado base table
  245. 'foreignkey_name' => 'library_id', // the name of the key in your base table
  246. 'foreignkey_value' => $library_id // the value of the library_id key
  247. );
  248. chado_update_node_form_properties($node, $details, $properties);
  249. // * Additional DBxrefs Form *
  250. $details = array(
  251. 'linking_table' => 'library_dbxref', // the name of your _dbxref table
  252. 'foreignkey_name' => 'library_id', // the name of the key in your base table
  253. 'foreignkey_value' => $library_id // the value of the library_id key
  254. );
  255. chado_update_node_form_dbxrefs($node, $details);
  256. }
  257. else {
  258. $library_id = $node->library_id;
  259. }
  260. // Make sure the entry for this library doesn't already exist in the
  261. // chado_library table if it doesn't exist then we want to add it.
  262. $check_org_id = chado_get_id_from_nid('library', $node->nid);
  263. if (!$check_org_id) {
  264. $record = new stdClass();
  265. $record->nid = $node->nid;
  266. $record->vid = $node->vid;
  267. $record->library_id = $library_id;
  268. drupal_write_record('chado_library', $record);
  269. }
  270. }
  271. /**
  272. * Update nodes
  273. *
  274. * @ingroup tripal_library
  275. */
  276. function chado_library_update($node) {
  277. $node->libraryname = trim($node->libraryname);
  278. $node->uniquename = trim($node->uniquename);
  279. $node->description = trim($node->description);
  280. // update the library record
  281. $library_id = chado_get_id_from_nid('library', $node->nid);
  282. $match = array(
  283. 'library_id' => $library_id,
  284. );
  285. $values = array(
  286. 'name' => $node->libraryname,
  287. 'uniquename' => $node->uniquename,
  288. 'organism_id' => $node->organism_id,
  289. 'type_id' => $node->library_type,
  290. );
  291. $status = chado_update_record('library', $match, $values);
  292. if (!$status) {
  293. drupal_set_message(t('Unable to update library.', 'warning'));
  294. watchdog('tripal_library', 'Update library: Unable to update library where values: %values',
  295. array('%values' => print_r($values, TRUE)), WATCHDOG_ERROR);
  296. }
  297. // * Properties Form *
  298. // add the description property
  299. $properties = chado_retrieve_node_form_properties($node);
  300. $descrip_id = tripal_cv_get_cvterm_by_name('Library Description', NULL, 'library_property');
  301. $properties[$descrip_id->cvterm_id][0] = $node->description;
  302. $details = array(
  303. 'property_table' => 'libraryprop', // the name of the prop table
  304. 'base_table' => 'library', // the name of your chado base table
  305. 'foreignkey_name' => 'library_id', // the name of the key in your base table
  306. 'foreignkey_value' => $library_id // the value of the library_id key
  307. );
  308. chado_update_node_form_properties($node, $details, $properties);
  309. // * Additional DBxrefs Form *
  310. $details = array(
  311. 'linking_table' => 'library_dbxref', // the name of your _dbxref table
  312. 'foreignkey_name' => 'library_id', // the name of the key in your base table
  313. 'foreignkey_value' => $library_id // the value of the library_id key
  314. );
  315. chado_update_node_form_dbxrefs($node, $details);
  316. }
  317. /**
  318. * When a node is requested by the user this function is called to allow us
  319. * to add auxiliary data to the node object.
  320. *
  321. * @ingroup tripal_library
  322. */
  323. function chado_library_load($nodes) {
  324. foreach ($nodes as $nid => $node) {
  325. // get the feature details from chado
  326. $library_id = chado_get_id_from_nid('library', $node->nid);
  327. $values = array('library_id' => $library_id);
  328. $library = chado_generate_var('library', $values);
  329. // the uniquename field is a text field so we need to expand it
  330. $library = chado_expand_var($library, 'field', 'library.uniquename');
  331. $nodes[$nid]->library = $library;
  332. }
  333. }
  334. /**
  335. * Delete data from drupal and chado databases when a node is deleted
  336. * @ingroup tripal_library
  337. */
  338. function chado_library_delete(&$node) {
  339. $library_id = chado_get_id_from_nid('library', $node->nid);
  340. // if we don't have a library id for this node then this isn't a node of
  341. // type chado_library or the entry in the chado_library table was lost.
  342. if (!$library_id) {
  343. return;
  344. }
  345. // Remove data from {chado_library}, {node} and {node_revisions} tables of
  346. // drupal database
  347. $sql_del = "DELETE FROM {chado_library} WHERE nid = :nid AND vid = :vid";
  348. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  349. $sql_del = "DELETE FROM {node_revision} WHERE nid = :nid AND vid = :vid";
  350. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  351. $sql_del = "DELETE FROM {node} WHERE nid = :nid AND vid = :vid";
  352. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  353. // Remove data from library and libraryprop tables of chado database as well
  354. chado_query("DELETE FROM {libraryprop} WHERE library_id = :library_id", array(':library_id' => $library_id));
  355. chado_query("DELETE FROM {library} WHERE library_id = :library_id", array(':library_id' => $library_id));
  356. }
  357. /**
  358. * Implement hook_access().
  359. *
  360. * This hook allows node modules to limit access to the node types they define.
  361. *
  362. * @param $node
  363. * The node on which the operation is to be performed, or, if it does not yet exist, the
  364. * type of node to be created
  365. *
  366. * @param $op
  367. * The operation to be performed
  368. *
  369. * @param $account
  370. * A user object representing the user for whom the operation is to be performed
  371. *
  372. * @return
  373. * If the permission for the specified operation is not set then return FALSE. If the
  374. * permission is set then return NULL as this allows other modules to disable
  375. * access. The only exception is when the $op == 'create'. We will always
  376. * return TRUE if the permission is set.
  377. *
  378. * @ingroup tripal_library
  379. */
  380. function chado_library_node_access($node, $op, $account) {
  381. if ($op == 'create') {
  382. if (!user_access('create chado_library content', $account)) {
  383. return FALSE;
  384. }
  385. return TRUE;
  386. }
  387. if ($op == 'update') {
  388. if (!user_access('edit chado_library content', $account)) {
  389. return FALSE;
  390. }
  391. }
  392. if ($op == 'delete') {
  393. if (!user_access('delete chado_library content', $account)) {
  394. return FALSE;
  395. }
  396. }
  397. if ($op == 'view') {
  398. if (!user_access('access chado_library content', $account)) {
  399. return FALSE;
  400. }
  401. }
  402. return NULL;
  403. }
  404. /**
  405. * @ingroup tripal_library
  406. */
  407. function tripal_library_node_view($node, $view_mode, $langcode) {
  408. switch ($node->type) {
  409. case 'chado_library':
  410. if ($view_mode == 'full') {
  411. $node->content['tripal_library_base'] = array(
  412. '#markup' => theme('tripal_library_base', array('node' => $node)),
  413. '#tripal_toc_id' => 'base',
  414. '#tripal_toc_title' => 'Overview',
  415. '#weight' => -100,
  416. );
  417. $node->content['tripal_library_properties'] = array(
  418. '#markup' => theme('tripal_library_properties', array('node' => $node)),
  419. '#tripal_toc_id' => 'properties',
  420. '#tripal_toc_title' => 'Properties',
  421. );
  422. $node->content['tripal_library_publications'] = array(
  423. '#markup' => theme('tripal_library_publications', array('node' => $node)),
  424. '#tripal_toc_id' => 'publications',
  425. '#tripal_toc_title' => 'Publications',
  426. );
  427. $node->content['tripal_library_references'] = array(
  428. '#markup' => theme('tripal_library_references', array('node' => $node)),
  429. '#tripal_toc_id' => 'references',
  430. '#tripal_toc_title' => 'References',
  431. );
  432. $node->content['tripal_library_synonyms'] = array(
  433. '#markup' => theme('tripal_library_synonyms', array('node' => $node)),
  434. '#tripal_toc_id' => 'synonyms',
  435. '#tripal_toc_title' => 'Synonyms',
  436. );
  437. $node->content['tripal_library_terms'] = array(
  438. '#markup' => theme('tripal_library_terms', array('node' => $node)),
  439. '#tripal_toc_id' => 'terms',
  440. '#tripal_toc_title' => 'Annotated Terms',
  441. );
  442. }
  443. if ($view_mode == 'teaser') {
  444. $node->content['tripal_library_teaser'] = array(
  445. '#markup' => theme('tripal_library_teaser', array('node' => $node)),
  446. );
  447. }
  448. break;
  449. case 'chado_organism':
  450. if ($view_mode == 'full') {
  451. $node->content['tripal_organism.libraries'] = array(
  452. '#markup' => theme('tripal_organism.libraries', array('node' => $node)),
  453. '#tripal_toc_id' => 'libraries',
  454. '#tripal_toc_title' => 'Libraries',
  455. );
  456. }
  457. break;
  458. case 'chado_feature':
  459. if ($view_mode == 'full') {
  460. $node->content['tripal_feature.libraries'] = array(
  461. '#markup' => theme('tripal_feature.libraries', array('node' => $node)),
  462. '#tripal_toc_id' => 'libraries',
  463. '#tripal_toc_title' => 'Libraries',
  464. );
  465. }
  466. break;
  467. }
  468. }
  469. /**
  470. *
  471. * @param $node
  472. */
  473. function tripal_library_node_presave($node) {
  474. switch ($node->type) {
  475. case 'chado_library':
  476. // for a form submission the 'libraryname' field will be set,
  477. // for a sync, we must pull from the library object
  478. if(property_exists($node, 'libraryname')) {
  479. // set the title
  480. $node->title = $node->libraryname;
  481. }
  482. else {
  483. $node->title = $node->library->name;
  484. }
  485. break;
  486. }
  487. }