tripal_featuremap.chado_node.inc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. <?php
  2. /**
  3. * When editing or creating a new node of type 'chado_featuremap' we need
  4. * a form. This function creates the form that will be used for this.
  5. *
  6. * @ingroup tripal_featuremap
  7. */
  8. function chado_featuremap_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. $featuremap_id = NULL;
  20. $title = '';
  21. $description = '';
  22. $unittype_id = '';
  23. // if we are editing an existing node then the featuremap is already part of the node
  24. if (property_exists($node, 'featuremap')) {
  25. $featuremap = $node->featuremap;
  26. $featuremap = tripal_core_expand_chado_vars($featuremap, 'field', 'featuremap.description');
  27. $featuremap_id = $featuremap->featuremap_id;
  28. // get form defaults
  29. $title = $featuremap->name;
  30. $description = $featuremap->description;
  31. $unittype_id = $featuremap->unittype_id->cvterm_id;
  32. // set the featuremap_id in the form
  33. $form['featuremap_id'] = array(
  34. '#type' => 'hidden',
  35. '#value' => $featuremap_id,
  36. );
  37. }
  38. // if we are re constructing the form from a failed validation or ajax callback
  39. // then use the $form_state['values'] values
  40. if (array_key_exists('values', $form_state)) {
  41. $title = $form_state['values']['title'];
  42. $description = $form_state['values']['description'];
  43. $unittype_id = $form_state['values']['unittype_id'];
  44. }
  45. // if we are re building the form from after submission (from ajax call) then
  46. // the values are in the $form_state['input'] array
  47. if (array_key_exists('input', $form_state) and !empty($form_state['input'])) {
  48. $title = $form_state['input']['title'];
  49. $description = $form_state['input']['description'];
  50. $unittype_id = $form_state['input']['unittype_id'];
  51. }
  52. $form['title']= array(
  53. '#type' => 'textfield',
  54. '#title' => t('Map Name'),
  55. '#description' => t('Please enter a name for this map'),
  56. '#required' => TRUE,
  57. '#default_value' => $title,
  58. '#maxlength' => 255
  59. );
  60. $form['description']= array(
  61. '#type' => 'textarea',
  62. '#title' => t('Map Description'),
  63. '#description' => t('A description of the map.'),
  64. '#required' => TRUE,
  65. '#default_value' => $description,
  66. );
  67. // get the list of unit types
  68. $values = array(
  69. 'cv_id' => array(
  70. 'name' => 'featuremap_units',
  71. )
  72. );
  73. $columns = array('cvterm_id','name');
  74. $options = array('order_by' => array('name' => 'ASC'));
  75. $featuremap_units = tripal_core_chado_select('cvterm', $columns, $values, $options);
  76. $units = array();
  77. $units[''] = '';
  78. foreach($featuremap_units as $unit) {
  79. $units[$unit->cvterm_id] = $unit->name;
  80. }
  81. $form['unittype_id'] = array(
  82. '#title' => t('Map Units'),
  83. '#type' => t('select'),
  84. '#description' => t("Chose the units for this map"),
  85. '#required' => TRUE,
  86. '#default_value' => $unittype_id,
  87. '#options' => $units,
  88. );
  89. // get the featuremap properties
  90. $properties = array();
  91. $properties[] = 'Select a Property';
  92. $sql = "
  93. SELECT DISTINCT CVT.cvterm_id, CVT.name, CVT.definition
  94. FROM {cvterm} CVT
  95. INNER JOIN {cv} ON CVT.cv_id = CV.cv_id
  96. WHERE
  97. CV.name = 'featuremap_property' AND
  98. NOT CVT.is_obsolete = 1
  99. ORDER BY CVT.name ASC
  100. ";
  101. $prop_types = chado_query($sql);
  102. while ($prop = $prop_types->fetchObject()) {
  103. $properties[$prop->cvterm_id] = $prop->name;
  104. }
  105. $exclude = array();
  106. $include = array();
  107. $instructions = t('To add additional properties to the drop down. ' . l("Add terms to the featuremap_property vocabulary", "admin/tripal/chado/tripal_cv/cvterm/add") . ".");
  108. tripal_core_properties_form($form, $form_state, 'featuremapprop', 'featuremap_id', 'featuremap_property',
  109. $properties, $featuremap_id, $exclude, $include, $instructions, 'Properties');
  110. return $form;
  111. }
  112. /**
  113. * validates submission of form when adding or updating a map node
  114. *
  115. * @ingroup tripal_featuremap
  116. */
  117. function chado_featuremap_validate($node, $form, &$form_state) {
  118. $node->title = trim($node->title);
  119. $node->description = trim($node->description);
  120. // if this is a delete then don't validate
  121. if($node->op == 'Delete') {
  122. return;
  123. }
  124. // we are syncing if we do not have a node ID but we do have a featuremap_id. We don't
  125. // need to validate during syncing so just skip it.
  126. if (is_null($node->nid) and property_exists($node, 'featuremap_id') and $node->featuremap_id != 0) {
  127. return;
  128. }
  129. $featuremap = 0;
  130. // check to make sure the unique name on the map is unique
  131. // before we try to insert into chado. If this is an update then we will
  132. // have a featuremap_id, therefore we want to look for another map with this
  133. // name but with a different featuremap_id. If this is an insert, just look
  134. // for a case where the name already exists.
  135. if (property_exists($node, 'featuremap_id')) {
  136. $sql = "
  137. SELECT * FROM {featuremap}
  138. WHERE name = :name AND NOT featuremap_id = :featuremap_id
  139. ";
  140. $featuremap = chado_query($sql, array(':name' => $node->title, ':featuremap_id' => $node->featuremap_id))->fetchObject();
  141. }
  142. else {
  143. $sql = "SELECT * FROM {featuremap} WHERE name = :name";
  144. $featuremap = chado_query($sql, array(':name' => $node->title))->fetchObject();
  145. }
  146. if ($featuremap) {
  147. form_set_error('title', t('The unique map name already exists. Please choose another'));
  148. }
  149. }
  150. /**
  151. * Implement hook_access().
  152. *
  153. * This hook allows node modules to limit access to the node types they define.
  154. *
  155. * @param $node
  156. * The node on which the operation is to be performed, or, if it does not yet exist, the
  157. * type of node to be created
  158. *
  159. * @param $op
  160. * The operation to be performed
  161. *
  162. * @param $account
  163. * A user object representing the user for whom the operation is to be performed
  164. *
  165. * @return
  166. * If the permission for the specified operation is not set then return FALSE. If the
  167. * permission is set then return NULL as this allows other modules to disable
  168. * access. The only exception is when the $op == 'create'. We will always
  169. * return TRUE if the permission is set.
  170. *
  171. * @ingroup tripal_featuremap
  172. */
  173. function chado_featuremap_node_access($node, $op, $account) {
  174. if ($op == 'create') {
  175. if (!user_access('create chado_featuremap content', $account)) {
  176. return FALSE;
  177. }
  178. return TRUE;
  179. }
  180. if ($op == 'update') {
  181. if (!user_access('edit any chado_featuremap content', $account) &&
  182. !user_access('edit own chado_featuremap content', $account)) {
  183. return FALSE;
  184. }
  185. if (user_access('edit own chado_featuremap content', $account) &&
  186. $account->uid != $node->uid) {
  187. return FALSE;
  188. }
  189. }
  190. if ($op == 'delete') {
  191. if (!user_access('delete any chado_featuremap content', $account) &&
  192. !user_access('delete own chado_featuremap content', $account)) {
  193. return FALSE;
  194. }
  195. if (user_access('delete own chado_featuremap content', $account) &&
  196. $account->uid != $node->uid) {
  197. return FALSE;
  198. }
  199. }
  200. return NULL;
  201. }
  202. /**
  203. * When a new chado_featuremap node is created we also need to add information
  204. * to our chado_featuremap table. This function is called on insert of a new node
  205. * of type 'chado_featuremap' and inserts the necessary information.
  206. *
  207. * @ingroup tripal_featuremap
  208. */
  209. function chado_featuremap_insert($node) {
  210. $node->title = trim($node->title);
  211. $node->description = trim($node->description);
  212. // if there is an featuremap_id in the $node object then this must be a sync so
  213. // we can skip adding the featuremap as it is already there, although
  214. // we do need to proceed with the rest of the insert
  215. if (!property_exists($node, 'featuremap_id')) {
  216. $values = array(
  217. 'name' => $node->title,
  218. 'description' => $node->description,
  219. 'unittype_id' => $node->unittype_id
  220. );
  221. $featuremap = tripal_core_chado_insert('featuremap', $values);
  222. if(!$featuremap) {
  223. drupal_set_message(t('Unable to add featuremap.', 'warning'));
  224. watchdog('tripal_featuremap', 'Unable to create feature map where values: %values',
  225. array('%values' => print_r($values, TRUE)), WATCHDOG_WARNING);
  226. return;
  227. }
  228. $featuremap_id = $featuremap['featuremap_id'];
  229. // get the properties from the form
  230. $properties = tripal_core_properties_form_retreive($node, 'featuremap_property');
  231. // now add in the properties
  232. $properties = tripal_core_properties_form_retreive($node, 'featuremap_property');
  233. foreach ($properties as $property => $elements) {
  234. foreach ($elements as $rank => $value) {
  235. // if the property name is 'Map Dbxref' then save this as a Dbxref record not a property
  236. if ($property == 'Map Dbxref') {
  237. $featuremap_dbxref = tripal_featuremap_add_featuremap_dbxref($featuremap_id, $value);
  238. if (!$featuremap_dbxref) {
  239. drupal_set_message("Error cannot add featuremap cross reference: $value", "error");
  240. watchdog('t_featuremap', "Error cannot add featuremap cross reference: %ref",
  241. array('%ref' => $value), WATCHDOG_ERROR);
  242. }
  243. }
  244. // this is a property so add it
  245. else {
  246. $status = tripal_featuremap_insert_property($featuremap_id, $property, $value, FALSE, 'featuremap_property');
  247. if (!$status) {
  248. drupal_set_message("Error cannot add property: $property", "error");
  249. watchdog('t_featuremap', "Error cannot add property: %prop",
  250. array('%property' => $property), WATCHDOG_ERROR);
  251. }
  252. }
  253. }
  254. }
  255. }
  256. else {
  257. $featuremap_id = $node->featuremap_id;
  258. }
  259. // Make sure the entry for this featuremap doesn't already exist in the
  260. // chado_featuremap table if it doesn't exist then we want to add it.
  261. $check_org_id = chado_get_id_for_node('featuremap', $node->nid);
  262. if (!$check_org_id) {
  263. $record = new stdClass();
  264. $record->nid = $node->nid;
  265. $record->vid = $node->vid;
  266. $record->featuremap_id = $featuremap_id;
  267. drupal_write_record('chado_featuremap', $record);
  268. }
  269. }
  270. /**
  271. * Update nodes
  272. *
  273. * @ingroup tripal_featuremap
  274. */
  275. function chado_featuremap_update($node) {
  276. $node->title = trim($node->title);
  277. $node->description = trim($node->description);
  278. $featuremap_id = chado_get_id_for_node('featuremap', $node->nid) ;
  279. // update the map record
  280. $match = array(
  281. 'featuremap_id' => $featuremap_id,
  282. );
  283. $values = array(
  284. 'name' => $node->title,
  285. 'description' => $node->description,
  286. 'unittype_id' => $node->unittype_id
  287. );
  288. $status = tripal_core_chado_update('featuremap', $match, $values);
  289. if (!$status) {
  290. drupal_set_message("Error updating map", "error");
  291. watchdog('t_featuremap', "Error updating map", array(), WATCHDOG_ERROR);
  292. return;
  293. }
  294. // get the properties from the form
  295. $properties = tripal_core_properties_form_retreive($node, 'featuremap_property');
  296. tripal_core_chado_delete('featuremapprop', array('featuremap_id' => $featuremap_id));
  297. foreach ($properties as $property => $elements) {
  298. foreach ($elements as $rank => $value) {
  299. // if the property name is 'Map Dbxref' then save this as a Dbxref record not a property
  300. if ($property == 'Map Dbxref') {
  301. $featuremap_dbxref = tripal_featuremap_add_featuremap_dbxref($featuremap_id, $value);
  302. if (!$featuremap_dbxref) {
  303. drupal_set_message("Error cannot add featuremap cross reference: $value", "error");
  304. watchdog('t_featuremap', "Error cannot add featuremap cross reference: %ref",
  305. array('%ref' => $value), WATCHDOG_ERROR);
  306. }
  307. }
  308. // this is a property so add it
  309. else {
  310. $status = tripal_featuremap_insert_property($featuremap_id, $property, $value, FALSE, 'featuremap_property');
  311. if (!$status) {
  312. drupal_set_message("Error cannot add property: $property", "error");
  313. watchdog('t_featuremap', "Error cannot add property: %prop",
  314. array('%property' => $property), WATCHDOG_ERROR);
  315. }
  316. }
  317. }
  318. }
  319. }
  320. /**
  321. * When a node is requested by the user this function is called to allow us
  322. * to add auxiliary data to the node object.
  323. *
  324. * @ingroup tripal_featuremap
  325. */
  326. function chado_featuremap_load($nodes) {
  327. foreach ($nodes as $nid => $node) {
  328. // get the feature details from chado
  329. $featuremap_id = chado_get_id_for_node('featuremap', $node->nid);
  330. $values = array('featuremap_id' => $featuremap_id);
  331. $featuremap = tripal_core_generate_chado_var('featuremap', $values);
  332. // expand the description field as it is needed by the form
  333. $featuremap = tripal_core_expand_chado_vars($featuremap, 'field', 'featuremap.description');
  334. $nodes[$nid]->featuremap = $featuremap;
  335. }
  336. }
  337. /**
  338. * Delete data from drupal and chado databases when a node is deleted
  339. * @ingroup tripal_featuremap
  340. */
  341. function chado_featuremap_delete(&$node) {
  342. $featuremap_id = chado_get_id_for_node('featuremap', $node->nid);
  343. // if we don't have a map id for this node then this isn't a node of
  344. // type chado_featuremap or the entry in the chado_featuremap table was lost.
  345. if (!$featuremap_id) {
  346. return;
  347. }
  348. // Remove data from {chado_featuremap}, {node} and {node_revisions} tables of
  349. // drupal database
  350. $sql_del = "DELETE FROM {chado_featuremap} WHERE nid = :nid AND vid = :vid";
  351. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  352. $sql_del = "DELETE FROM {node} WHERE nid = :nid AND vid = :vid";
  353. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  354. $sql_del = "DELETE FROM {node_revisions} WHERE nid = :nid AND vid = :vid";
  355. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  356. // Remove data from map and mapprop tables of chado database as well
  357. chado_query("DELETE FROM {featuremapprop} WHERE featuremap_id = :featuremap_id", array(':featuremap_id' => $featuremap_id));
  358. chado_query("DELETE FROM {featuremap_dbxref} WHERE featuremap_id = :featuremap_id", array(':featuremap_id' => $featuremap_id));
  359. chado_query("DELETE FROM {featuremap} WHERE featuremap_id = :featuremap_id", array(':featuremap_id' => $featuremap_id));
  360. }