tripal_library.chado_node.inc 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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)) {
  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. return $form;
  175. }
  176. /**
  177. * validates submission of form when adding or updating a library node
  178. *
  179. * @ingroup tripal_library
  180. */
  181. function chado_library_validate($node, $form, &$form_state) {
  182. $node->title = trim($node->title);
  183. $node->uniquename = trim($node->uniquename);
  184. $node->description = trim($node->description);
  185. $lib = 0;
  186. // check to make sure the unique name on the library is unique
  187. // before we try to insert into chado.
  188. if (property_exists($node, 'library_id')) {
  189. $sql = "
  190. SELECT *
  191. FROM {library}
  192. WHERE uniquename = :uname AND NOT library_id = :library_id
  193. ";
  194. $lib = chado_query($sql, array(':uname' => $node->uniquename, ':library_id' => $node->library_id))->fetchObject();
  195. }
  196. else {
  197. $sql = "SELECT * FROM {library} WHERE uniquename = :uname";
  198. $lib = chado_query($sql, array(':uname' => $node->uniquename))->fetchObject();
  199. }
  200. if ($lib) {
  201. form_set_error('uniquename', t('The unique library name already exists. Please choose another'));
  202. }
  203. }
  204. /**
  205. * When a new chado_library node is created we also need to add information
  206. * to our chado_library table. This function is called on insert of a new node
  207. * of type 'chado_library' and inserts the necessary information.
  208. *
  209. * @ingroup tripal_library
  210. */
  211. function chado_library_insert($node) {
  212. $node->title = trim($node->title);
  213. $node->uniquename = trim($node->uniquename);
  214. $node->description = trim($node->description);
  215. // if there is an library_id in the $node object then this must be a sync so
  216. // we can skip adding the library as it is already there, although
  217. // we do need to proceed with the rest of the insert
  218. if (!property_exists($node, 'library_id')) {
  219. $values = array(
  220. 'name' => $node->title,
  221. 'uniquename' => $node->uniquename,
  222. 'organism_id' => $node->organism_id,
  223. 'type_id' => $node->library_type,
  224. );
  225. $library = tripal_core_chado_insert('library', $values);
  226. if (!$library) {
  227. drupal_set_message(t('Unable to add library.', 'warning'));
  228. watchdog('tripal_library', 'Insert library: Unable to create library where values: %values',
  229. array('%values' => print_r($values, TRUE)), WATCHDOG_ERROR);
  230. return;
  231. }
  232. $library_id = $library['library_id'];
  233. // * Properties Form *
  234. // add the description property
  235. $properties = chado_node_properties_form_retreive($node);
  236. $descrip_id = tripal_cv_get_cvterm_by_name('Library Description', NULL, 'library_property');
  237. $properties[$descrip_id->cvterm_id][0] = $node->description;
  238. $details = array(
  239. 'property_table' => 'libraryprop', // the name of the prop table
  240. 'base_table' => 'library', // the name of your chado base table
  241. 'foreignkey_name' => 'library_id', // the name of the key in your base table
  242. 'foreignkey_value' => $library_id // the value of the library_id key
  243. );
  244. chado_node_properties_form_update_properties($node, $details, $properties);
  245. }
  246. else {
  247. $library_id = $node->library_id;
  248. }
  249. // Make sure the entry for this library doesn't already exist in the
  250. // chado_library table if it doesn't exist then we want to add it.
  251. $check_org_id = chado_get_id_for_node('library', $node->nid);
  252. if (!$check_org_id) {
  253. $record = new stdClass();
  254. $record->nid = $node->nid;
  255. $record->vid = $node->vid;
  256. $record->library_id = $library_id;
  257. drupal_write_record('chado_library', $record);
  258. }
  259. }
  260. /**
  261. * Update nodes
  262. *
  263. * @ingroup tripal_library
  264. */
  265. function chado_library_update($node) {
  266. $node->title = trim($node->title);
  267. $node->uniquename = trim($node->uniquename);
  268. $node->description = trim($node->description);
  269. // update the library record
  270. $library_id = chado_get_id_for_node('library', $node->nid);
  271. $match = array(
  272. 'library_id' => $library_id,
  273. );
  274. $values = array(
  275. 'name' => $node->title,
  276. 'uniquename' => $node->uniquename,
  277. 'organism_id' => $node->organism_id,
  278. 'type_id' => $node->library_type,
  279. );
  280. $status = tripal_core_chado_update('library', $match, $values);
  281. if (!$status) {
  282. drupal_set_message(t('Unable to update library.', 'warning'));
  283. watchdog('tripal_library', 'Update library: Unable to update library where values: %values',
  284. array('%values' => print_r($values, TRUE)), WATCHDOG_ERROR);
  285. }
  286. // * Properties Form *
  287. // add the description property
  288. $properties = chado_node_properties_form_retreive($node);
  289. $descrip_id = tripal_cv_get_cvterm_by_name('Library Description', NULL, 'library_property');
  290. $properties[$descrip_id->cvterm_id][0] = $node->description;
  291. $details = array(
  292. 'property_table' => 'libraryprop', // the name of the prop table
  293. 'base_table' => 'library', // the name of your chado base table
  294. 'foreignkey_name' => 'library_id', // the name of the key in your base table
  295. 'foreignkey_value' => $library_id // the value of the library_id key
  296. );
  297. chado_node_properties_form_update_properties($node, $details, $properties);
  298. }
  299. /**
  300. * When a node is requested by the user this function is called to allow us
  301. * to add auxiliary data to the node object.
  302. *
  303. * @ingroup tripal_library
  304. */
  305. function chado_library_load($nodes) {
  306. foreach ($nodes as $nid => $node) {
  307. // get the feature details from chado
  308. $library_id = chado_get_id_for_node('library', $node->nid);
  309. $values = array('library_id' => $library_id);
  310. $library = tripal_core_generate_chado_var('library', $values);
  311. // the uniquename field is a text field so we need to expand it
  312. $library = tripal_core_expand_chado_vars($library, 'field', 'library.uniquename');
  313. $nodes[$nid]->library = $library;
  314. }
  315. }
  316. /**
  317. * Delete data from drupal and chado databases when a node is deleted
  318. * @ingroup tripal_library
  319. */
  320. function chado_library_delete(&$node) {
  321. $library_id = chado_get_id_for_node('library', $node->nid);
  322. // if we don't have a library id for this node then this isn't a node of
  323. // type chado_library or the entry in the chado_library table was lost.
  324. if (!$library_id) {
  325. return;
  326. }
  327. // Remove data from {chado_library}, {node} and {node_revisions} tables of
  328. // drupal database
  329. $sql_del = "DELETE FROM {chado_library} WHERE nid = :nid AND vid = :vid";
  330. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  331. $sql_del = "DELETE FROM {node_revision} WHERE nid = :nid AND vid = :vid";
  332. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  333. $sql_del = "DELETE FROM {node} WHERE nid = :nid AND vid = :vid";
  334. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  335. // Remove data from library and libraryprop tables of chado database as well
  336. chado_query("DELETE FROM {libraryprop} WHERE library_id = :library_id", array(':library_id' => $library_id));
  337. chado_query("DELETE FROM {library} WHERE library_id = :library_id", array(':library_id' => $library_id));
  338. }
  339. /**
  340. * Implement hook_access().
  341. *
  342. * This hook allows node modules to limit access to the node types they define.
  343. *
  344. * @param $node
  345. * The node on which the operation is to be performed, or, if it does not yet exist, the
  346. * type of node to be created
  347. *
  348. * @param $op
  349. * The operation to be performed
  350. *
  351. * @param $account
  352. * A user object representing the user for whom the operation is to be performed
  353. *
  354. * @return
  355. * If the permission for the specified operation is not set then return FALSE. If the
  356. * permission is set then return NULL as this allows other modules to disable
  357. * access. The only exception is when the $op == 'create'. We will always
  358. * return TRUE if the permission is set.
  359. *
  360. * @ingroup tripal_library
  361. */
  362. function chado_library_node_access($node, $op, $account) {
  363. if ($op == 'create') {
  364. if (!user_access('create chado_library content', $account)) {
  365. return FALSE;
  366. }
  367. return TRUE;
  368. }
  369. if ($op == 'update') {
  370. if (!user_access('edit chado_library content', $account)) {
  371. return FALSE;
  372. }
  373. }
  374. if ($op == 'delete') {
  375. if (!user_access('delete chado_library content', $account)) {
  376. return FALSE;
  377. }
  378. }
  379. if ($op == 'view') {
  380. if (!user_access('access chado_library content', $account)) {
  381. return FALSE;
  382. }
  383. }
  384. return NULL;
  385. }
  386. /**
  387. * @ingroup tripal_library
  388. */
  389. function tripal_library_node_view($node, $view_mode, $langcode) {
  390. switch ($node->type) {
  391. case 'chado_library':
  392. if ($view_mode == 'full') {
  393. $node->content['tripal_library_base'] = array(
  394. '#markup' => theme('tripal_library_base', array('node' => $node)),
  395. '#tripal_toc_id' => 'base',
  396. '#tripal_toc_title' => 'Details',
  397. '#weight' => 0,
  398. );
  399. $node->content['tripal_library_properties'] = array(
  400. '#markup' => theme('tripal_library_properties', array('node' => $node)),
  401. '#tripal_toc_id' => 'properties',
  402. '#tripal_toc_title' => 'Properties',
  403. );
  404. $node->content['tripal_library_publications'] = array(
  405. '#markup' => theme('tripal_library_publications', array('node' => $node)),
  406. '#tripal_toc_id' => 'publications',
  407. '#tripal_toc_title' => 'Publications',
  408. );
  409. $node->content['tripal_library_references'] = array(
  410. '#markup' => theme('tripal_library_references', array('node' => $node)),
  411. '#tripal_toc_id' => 'references',
  412. '#tripal_toc_title' => 'References',
  413. );
  414. $node->content['tripal_library_synonyms'] = array(
  415. '#markup' => theme('tripal_library_synonyms', array('node' => $node)),
  416. '#tripal_toc_id' => 'synonyms',
  417. '#tripal_toc_title' => 'Synonyms',
  418. );
  419. $node->content['tripal_library_terms'] = array(
  420. '#markup' => theme('tripal_library_terms', array('node' => $node)),
  421. '#tripal_toc_id' => 'terms',
  422. '#tripal_toc_title' => 'Annotated Terms',
  423. );
  424. }
  425. if ($view_mode == 'teaser') {
  426. $node->content['tripal_library_teaser'] = array(
  427. '#markup' => theme('tripal_library_teaser', array('node' => $node)),
  428. );
  429. }
  430. break;
  431. case 'chado_organism':
  432. if ($view_mode == 'full') {
  433. $node->content['tripal_organism.libraries'] = array(
  434. '#markup' => theme('tripal_organism.libraries', array('node' => $node)),
  435. '#tripal_toc_id' => 'libraries',
  436. '#tripal_toc_title' => 'Libraries',
  437. );
  438. }
  439. break;
  440. case 'chado_feature':
  441. if ($view_mode == 'full') {
  442. $node->content['tripal_feature.libraries'] = array(
  443. '#markup' => theme('tripal_feature.libraries', array('node' => $node)),
  444. '#tripal_toc_id' => 'libraries',
  445. '#tripal_toc_title' => 'Libraries',
  446. );
  447. }
  448. break;
  449. }
  450. }
  451. /**
  452. *
  453. * @param $node
  454. */
  455. function tripal_library_node_presave($node) {
  456. switch ($node->type) {
  457. case 'chado_library':
  458. // for a form submission the fields part of the node object
  459. // but for a sync the feilds are in an object of the node
  460. if(property_exists($node, 'library')) {
  461. // set the title
  462. $node->title = $node->name;
  463. }
  464. else {
  465. // the title field is already in the form
  466. }
  467. break;
  468. }
  469. }