tripal_library.chado_node.inc 17 KB

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