tripal_featuremap.chado_node.inc 17 KB

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