tripal_library.chado_node.inc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. <?php
  2. /**
  3. * When editing or creating a new node of type 'chado_library' we need
  4. * a form. This function creates the form that will be used for this.
  5. *
  6. * @ingroup tripal_library
  7. */
  8. function chado_library_form($node, &$form_state) {
  9. $form = array();
  10. // Default values can come in the following ways:
  11. //
  12. // 1) as elements of the $node object. This occurs when editing an existing library
  13. // 2) in the $form_state['values'] array which occurs on a failed validation or
  14. // ajax callbacks from non submit form elements
  15. // 3) in the $form_state['input'] array which occurs on ajax callbacks from submit
  16. // form elements and the form is being rebuilt
  17. //
  18. // set form field defaults
  19. $library_id = NULL;
  20. $title = '';
  21. $uniquename = '';
  22. $library_type = '';
  23. $organism_id = '';
  24. $description = '';
  25. // if we are editing an existing node then the library is already part of the node
  26. if (property_exists($node, 'library')) {
  27. $library = $node->library;
  28. $library_id = $library->library_id;
  29. $title = $library->name;
  30. $uniquename = $library->uniquename;
  31. $library_type = $library->type_id->cvterm_id;
  32. $organism_id = $library->organism_id->organism_id;
  33. $libprop = tripal_library_get_property($library->library_id, 'Library Description');
  34. $description = $libprop->value;
  35. // keep track of the library id if we have. If we do have one then
  36. // this is an update as opposed to an insert.
  37. $form['library_id'] = array(
  38. '#type' => 'value',
  39. '#value' => $library_id,
  40. );
  41. }
  42. // if we are re constructing the form from a failed validation or ajax callback
  43. // then use the $form_state['values'] values
  44. if (array_key_exists('values', $form_state)) {
  45. $title = $form_state['values']['title'];
  46. $uniquename = $form_state['values']['uniquename'];
  47. $library_type = $form_state['values']['library_type'];
  48. $organism_id = $form_state['values']['organism_id'];
  49. $description = $form_state['values']['description'];
  50. }
  51. // if we are re building the form from after submission (from ajax call) then
  52. // the values are in the $form_state['input'] array
  53. if (array_key_exists('input', $form_state) and !empty($form_state['input'])) {
  54. $title = $form_state['input']['title'];
  55. $uniquename = $form_state['input']['uniquename'];
  56. $library_type = $form_state['input']['library_type'];
  57. $organism_id = $form_state['input']['organism_id'];
  58. $description = $form_state['input']['description'];
  59. }
  60. $form['title']= array(
  61. '#type' => 'textfield',
  62. '#title' => t('Library Name'),
  63. '#description' => t('Please enter the name for this library. Library names should be recognizable but do not need to be unique.'),
  64. '#required' => TRUE,
  65. '#default_value' => $title,
  66. '#weight' => 1
  67. );
  68. $form['uniquename']= array(
  69. '#type' => 'textfield',
  70. '#title' => t('Unique Name'),
  71. '#description' => t('Please enter a unique name for this library. This can be any value used to uniquely identify a library.'),
  72. '#required' => TRUE,
  73. '#default_value' => $uniquename,
  74. '#weight' => 2
  75. );
  76. // get the list of library types
  77. $values = array(
  78. 'cv_id' => array(
  79. 'name' => 'library_type',
  80. )
  81. );
  82. $columns = array('cvterm_id','name');
  83. $options = array('order_by' => array('name' => 'ASC'));
  84. $lib_types = tripal_core_chado_select('cvterm', $columns, $values, $options);
  85. $types = array();
  86. $types[''] = '';
  87. foreach($lib_types as $type) {
  88. $types[$type->cvterm_id] = $type->name;
  89. }
  90. $form['library_type'] = array(
  91. '#title' => t('Library Type'),
  92. '#type' => t('select'),
  93. '#description' => t("Choose the library type."),
  94. '#required' => TRUE,
  95. '#default_value' => $library_type,
  96. '#options' => $types,
  97. '#weight' => 3
  98. );
  99. // get the list of organisms
  100. $sql = "SELECT * FROM {organism}";
  101. $org_rset = chado_query($sql);
  102. $organisms = array();
  103. $organisms[''] = '';
  104. while ($organism = $org_rset->fetchObject()) {
  105. $organisms[$organism->organism_id] =
  106. "$organism->genus $organism->species ($organism->common_name)";
  107. }
  108. $form['organism_id'] = array(
  109. '#title' => t('Organism'),
  110. '#type' => t('select'),
  111. '#description' => t("Choose the organism with which this library is associated."),
  112. '#required' => TRUE,
  113. '#default_value' => $organism_id,
  114. '#options' => $organisms,
  115. '#weight' => 4,
  116. );
  117. $form['description']= array(
  118. '#type' => 'textarea',
  119. '#title' => t('Library Description'),
  120. '#description' => t('A brief description of the library'),
  121. '#required' => TRUE,
  122. '#default_value' => $description,
  123. '#weight' => 5
  124. );
  125. return $form;
  126. }
  127. /**
  128. * validates submission of form when adding or updating a library node
  129. *
  130. * @ingroup tripal_library
  131. */
  132. function chado_library_validate($node, $form, &$form_state) {
  133. $node->title = trim($node->title);
  134. $node->uniquename = trim($node->uniquename);
  135. $node->description = trim($node->description);
  136. $lib = 0;
  137. // check to make sure the unique name on the library is unique
  138. // before we try to insert into chado.
  139. if (property_exists($node, 'library_id')) {
  140. $sql = "
  141. SELECT *
  142. FROM {library}
  143. WHERE uniquename = :uname AND NOT library_id = :library_id
  144. ";
  145. $lib = chado_query($sql, array(':uname' => $node->uniquename, ':library_id' => $node->library_id))->fetchObject();
  146. }
  147. else {
  148. $sql = "SELECT * FROM {library} WHERE uniquename = :uname";
  149. $lib = chado_query($sql, array(':uname' => $node->uniquename))->fetchObject();
  150. }
  151. if ($lib) {
  152. form_set_error('uniquename', t('The unique library name already exists. Please choose another'));
  153. }
  154. }
  155. /**
  156. * When a new chado_library node is created we also need to add information
  157. * to our chado_library table. This function is called on insert of a new node
  158. * of type 'chado_library' and inserts the necessary information.
  159. *
  160. * @ingroup tripal_library
  161. */
  162. function chado_library_insert($node) {
  163. $node->title = trim($node->title);
  164. $node->uniquename = trim($node->uniquename);
  165. $node->description = trim($node->description);
  166. // if there is an library_id in the $node object then this must be a sync so
  167. // we can skip adding the library as it is already there, although
  168. // we do need to proceed with the rest of the insert
  169. if (!property_exists($node, 'library_id')) {
  170. $values = array(
  171. 'name' => $node->title,
  172. 'uniquename' => $node->uniquename,
  173. 'organism_id' => $node->organism_id,
  174. 'type_id' => $node->library_type,
  175. );
  176. $library = tripal_core_chado_insert('library', $values);
  177. if (!$library) {
  178. drupal_set_message(t('Unable to add library.', 'warning'));
  179. watchdog('tripal_library', 'Insert library: Unable to create library where values: %values',
  180. array('%values' => print_r($values, TRUE)), WATCHDOG_ERROR);
  181. return;
  182. }
  183. $library_id = $library['library_id'];
  184. // add the description property
  185. tripal_library_insert_property($library_id, 'Library Description', $node->description);
  186. }
  187. else {
  188. $library_id = $node->library_id;
  189. }
  190. // Make sure the entry for this library doesn't already exist in the
  191. // chado_library table if it doesn't exist then we want to add it.
  192. $check_org_id = chado_get_id_for_node('library', $node->nid);
  193. if (!$check_org_id) {
  194. $record = new stdClass();
  195. $record->nid = $node->nid;
  196. $record->vid = $node->vid;
  197. $record->library_id = $library_id;
  198. drupal_write_record('chado_library', $record);
  199. }
  200. }
  201. /**
  202. * Update nodes
  203. *
  204. * @ingroup tripal_library
  205. */
  206. function chado_library_update($node) {
  207. $node->title = trim($node->title);
  208. $node->uniquename = trim($node->uniquename);
  209. $node->description = trim($node->description);
  210. // update the library record
  211. $library_id = chado_get_id_for_node('library', $node->nid);
  212. $match = array(
  213. 'library_id' => $library_id,
  214. );
  215. $values = array(
  216. 'name' => $node->title,
  217. 'uniquename' => $node->uniquename,
  218. 'organism_id' => $node->organism_id,
  219. 'type_id' => $node->library_type,
  220. );
  221. $status = tripal_core_chado_update('library', $match, $values);
  222. if (!$status) {
  223. drupal_set_message(t('Unable to update library.', 'warning'));
  224. watchdog('tripal_library', 'Update library: Unable to update library where values: %values',
  225. array('%values' => print_r($values, TRUE)), WATCHDOG_ERROR);
  226. }
  227. // add in the library description as a property
  228. tripal_library_update_property($library_id, 'Library Description', $node->description, 1);
  229. }
  230. /**
  231. * When a node is requested by the user this function is called to allow us
  232. * to add auxiliary data to the node object.
  233. *
  234. * @ingroup tripal_library
  235. */
  236. function chado_library_load($nodes) {
  237. foreach ($nodes as $nid => $node) {
  238. // get the feature details from chado
  239. $library_id = chado_get_id_for_node('library', $node->nid);
  240. $values = array('library_id' => $library_id);
  241. $library = tripal_core_generate_chado_var('library', $values);
  242. // the uniquename field is a text field so we need to expand it
  243. $library = tripal_core_expand_chado_vars($library, 'field', 'library.uniquename');
  244. $nodes[$nid]->library = $library;
  245. }
  246. }
  247. /**
  248. * Delete data from drupal and chado databases when a node is deleted
  249. * @ingroup tripal_library
  250. */
  251. function chado_library_delete(&$node) {
  252. $library_id = chado_get_id_for_node('library', $node->nid);
  253. // if we don't have a library id for this node then this isn't a node of
  254. // type chado_library or the entry in the chado_library table was lost.
  255. if (!$library_id) {
  256. return;
  257. }
  258. // Remove data from {chado_library}, {node} and {node_revisions} tables of
  259. // drupal database
  260. $sql_del = "DELETE FROM {chado_library} WHERE nid = :nid AND vid = :vid";
  261. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  262. $sql_del = "DELETE FROM {node_revision} WHERE nid = :nid AND vid = :vid";
  263. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  264. $sql_del = "DELETE FROM {node} WHERE nid = :nid AND vid = :vid";
  265. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  266. // Remove data from library and libraryprop tables of chado database as well
  267. chado_query("DELETE FROM {libraryprop} WHERE library_id = :library_id", array(':library_id' => $library_id));
  268. chado_query("DELETE FROM {library} WHERE library_id = :library_id", array(':library_id' => $library_id));
  269. }
  270. /**
  271. * Implement hook_access().
  272. *
  273. * This hook allows node modules to limit access to the node types they define.
  274. *
  275. * @param $node
  276. * The node on which the operation is to be performed, or, if it does not yet exist, the
  277. * type of node to be created
  278. *
  279. * @param $op
  280. * The operation to be performed
  281. *
  282. * @param $account
  283. * A user object representing the user for whom the operation is to be performed
  284. *
  285. * @return
  286. * If the permission for the specified operation is not set then return FALSE. If the
  287. * permission is set then return NULL as this allows other modules to disable
  288. * access. The only exception is when the $op == 'create'. We will always
  289. * return TRUE if the permission is set.
  290. *
  291. * @ingroup tripal_library
  292. */
  293. function chado_library_node_access($node, $op, $account) {
  294. if ($op == 'create') {
  295. if (!user_access('create chado_library content', $account)) {
  296. return FALSE;
  297. }
  298. return TRUE;
  299. }
  300. if ($op == 'update') {
  301. if (!user_access('edit chado_library content', $account)) {
  302. return FALSE;
  303. }
  304. }
  305. if ($op == 'delete') {
  306. if (!user_access('delete chado_library content', $account)) {
  307. return FALSE;
  308. }
  309. }
  310. if ($op == 'view') {
  311. if (!user_access('access chado_library content', $account)) {
  312. return FALSE;
  313. }
  314. }
  315. return NULL;
  316. }