tripal_featuremap.chado_node.inc 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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. $fmapname = '';
  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. $fmapname = $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. $fmapname = $form_state['values']['fmapname'];
  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. $fmapname = $form_state['input']['fmapname'];
  81. $description = $form_state['input']['description'];
  82. $unittype_id = $form_state['input']['unittype_id'];
  83. }
  84. $form['fmapname']= 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' => $fmapname,
  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. // Properties Form
  122. // ----------------------------------
  123. $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") . ".");
  124. $details = array(
  125. 'property_table' => 'featuremapprop',
  126. 'base_foreign_key' => 'featuremap_id',
  127. 'base_key_value' => $featuremap_id,
  128. 'cv_name' => 'featuremap_property',
  129. 'fieldset_name' => 'Additional Details',
  130. 'additional_instructions' => $instructions
  131. );
  132. chado_node_properties_form($form, $form_state, $details);
  133. return $form;
  134. }
  135. /**
  136. * validates submission of form when adding or updating a map node
  137. *
  138. * @ingroup tripal_featuremap
  139. */
  140. function chado_featuremap_validate($node, $form, &$form_state) {
  141. $node->fmapname = trim($node->fmapname);
  142. $node->description = trim($node->description);
  143. // if this is a delete then don't validate
  144. if($node->op == 'Delete') {
  145. return;
  146. }
  147. // we are syncing if we do not have a node ID but we do have a featuremap_id. We don't
  148. // need to validate during syncing so just skip it.
  149. if (is_null($node->nid) and property_exists($node, 'featuremap_id') and $node->featuremap_id != 0) {
  150. return;
  151. }
  152. $featuremap = 0;
  153. // check to make sure the unique name on the map is unique
  154. // before we try to insert into chado. If this is an update then we will
  155. // have a featuremap_id, therefore we want to look for another map with this
  156. // name but with a different featuremap_id. If this is an insert, just look
  157. // for a case where the name already exists.
  158. if (property_exists($node, 'featuremap_id')) {
  159. $sql = "
  160. SELECT * FROM {featuremap}
  161. WHERE name = :name AND NOT featuremap_id = :featuremap_id
  162. ";
  163. $featuremap = chado_query($sql, array(':name' => $node->fmapname, ':featuremap_id' => $node->featuremap_id))->fetchObject();
  164. }
  165. else {
  166. $sql = "SELECT * FROM {featuremap} WHERE name = :name";
  167. $featuremap = chado_query($sql, array(':name' => $node->fmapname))->fetchObject();
  168. }
  169. if ($featuremap) {
  170. form_set_error('fmapname', t('The unique map name already exists. Please choose another'));
  171. }
  172. }
  173. /**
  174. * Implement hook_access().
  175. *
  176. * This hook allows node modules to limit access to the node types they define.
  177. *
  178. * @param $node
  179. * The node on which the operation is to be performed, or, if it does not yet exist, the
  180. * type of node to be created
  181. *
  182. * @param $op
  183. * The operation to be performed
  184. *
  185. * @param $account
  186. * A user object representing the user for whom the operation is to be performed
  187. *
  188. * @return
  189. * If the permission for the specified operation is not set then return FALSE. If the
  190. * permission is set then return NULL as this allows other modules to disable
  191. * access. The only exception is when the $op == 'create'. We will always
  192. * return TRUE if the permission is set.
  193. *
  194. * @ingroup tripal_featuremap
  195. */
  196. function chado_featuremap_node_access($node, $op, $account) {
  197. if ($op == 'create') {
  198. if (!user_access('create chado_featuremap content', $account)) {
  199. return FALSE;
  200. }
  201. return TRUE;
  202. }
  203. if ($op == 'update') {
  204. if (!user_access('edit any chado_featuremap content', $account) &&
  205. !user_access('edit own chado_featuremap content', $account)) {
  206. return FALSE;
  207. }
  208. if (user_access('edit own chado_featuremap content', $account) &&
  209. $account->uid != $node->uid) {
  210. return FALSE;
  211. }
  212. }
  213. if ($op == 'delete') {
  214. if (!user_access('delete any chado_featuremap content', $account) &&
  215. !user_access('delete own chado_featuremap content', $account)) {
  216. return FALSE;
  217. }
  218. if (user_access('delete own chado_featuremap content', $account) &&
  219. $account->uid != $node->uid) {
  220. return FALSE;
  221. }
  222. }
  223. return NULL;
  224. }
  225. /**
  226. * When a new chado_featuremap node is created we also need to add information
  227. * to our chado_featuremap table. This function is called on insert of a new node
  228. * of type 'chado_featuremap' and inserts the necessary information.
  229. *
  230. * @ingroup tripal_featuremap
  231. */
  232. function chado_featuremap_insert($node) {
  233. $node->fmapname = trim($node->fmapname);
  234. $node->description = trim($node->description);
  235. // if there is an featuremap_id in the $node object then this must be a sync so
  236. // we can skip adding the featuremap as it is already there, although
  237. // we do need to proceed with the rest of the insert
  238. if (!property_exists($node, 'featuremap_id')) {
  239. $values = array(
  240. 'name' => $node->fmapname,
  241. 'description' => $node->description,
  242. 'unittype_id' => $node->unittype_id
  243. );
  244. $featuremap = tripal_core_chado_insert('featuremap', $values);
  245. if(!$featuremap) {
  246. drupal_set_message(t('Unable to add featuremap.', 'warning'));
  247. watchdog('tripal_featuremap', 'Unable to create feature map where values: %values',
  248. array('%values' => print_r($values, TRUE)), WATCHDOG_WARNING);
  249. return;
  250. }
  251. $featuremap_id = $featuremap['featuremap_id'];
  252. // now add in the properties
  253. $properties = chado_node_properties_form_retreive($node);
  254. // We need to deal with the 'Map Dbxref' property specially
  255. $cvterm = tripal_core_chado_select(
  256. 'cvterm',
  257. array('cvterm_id'),
  258. array('name' => 'Map Dbxref', 'cv_id' => array('name' => 'featuremap_property'))
  259. );
  260. $map_dbxref_cvterm_id = $cvterm[0]->cvterm_id;
  261. if (isset($properties[$map_dbxref_cvterm_id])) {
  262. foreach ($properties[$map_dbxref_cvterm_id] as $rank => $value) {
  263. $featuremap_dbxref = tripal_featuremap_add_featuremap_dbxref($featuremap_id, $value);
  264. if (!$featuremap_dbxref) {
  265. drupal_set_message("Error cannot add featuremap cross reference: $value", "error");
  266. watchdog('t_featuremap', "Error cannot add featuremap cross reference: %ref",
  267. array('%ref' => $value), WATCHDOG_ERROR);
  268. }
  269. }
  270. unset($properties[$map_dbxref_cvterm_id]);
  271. }
  272. $details = array(
  273. 'property_table' => 'featuremapprop',
  274. 'base_table' => 'featuremap',
  275. 'foreignkey_name' => 'featuremap_id',
  276. 'foreignkey_value' => $featuremap_id
  277. );
  278. chado_node_properties_form_update_properties($node, $details, $properties);
  279. }
  280. else {
  281. $featuremap_id = $node->featuremap_id;
  282. }
  283. // Make sure the entry for this featuremap doesn't already exist in the
  284. // chado_featuremap table if it doesn't exist then we want to add it.
  285. $check_org_id = chado_get_id_for_node('featuremap', $node->nid);
  286. if (!$check_org_id) {
  287. $record = new stdClass();
  288. $record->nid = $node->nid;
  289. $record->vid = $node->vid;
  290. $record->featuremap_id = $featuremap_id;
  291. drupal_write_record('chado_featuremap', $record);
  292. }
  293. }
  294. /**
  295. * Update nodes
  296. *
  297. * @ingroup tripal_featuremap
  298. */
  299. function chado_featuremap_update($node) {
  300. $node->fmapname = trim($node->fmapname);
  301. $node->description = trim($node->description);
  302. $featuremap_id = chado_get_id_for_node('featuremap', $node->nid) ;
  303. // update the map record
  304. $match = array(
  305. 'featuremap_id' => $featuremap_id,
  306. );
  307. $values = array(
  308. 'name' => $node->fmapname,
  309. 'description' => $node->description,
  310. 'unittype_id' => $node->unittype_id
  311. );
  312. $status = tripal_core_chado_update('featuremap', $match, $values);
  313. if (!$status) {
  314. drupal_set_message("Error updating map", "error");
  315. watchdog('t_featuremap', "Error updating map", array(), WATCHDOG_ERROR);
  316. return;
  317. }
  318. // Update the properties
  319. $properties = chado_node_properties_form_retreive($node);
  320. // We need to deal with the 'Map Dbxref' property specially
  321. $cvterm = tripal_core_chado_select(
  322. 'cvterm',
  323. array('cvterm_id'),
  324. array('name' => 'Map Dbxref', 'cv_id' => array('name' => 'featuremap_property'))
  325. );
  326. $map_dbxref_cvterm_id = $cvterm[0]->cvterm_id;
  327. if (isset($properties[$map_dbxref_cvterm_id])) {
  328. foreach ($properties[$map_dbxref_cvterm_id] as $rank => $value) {
  329. $featuremap_dbxref = tripal_featuremap_add_featuremap_dbxref($featuremap_id, $value);
  330. if (!$featuremap_dbxref) {
  331. drupal_set_message("Error cannot add featuremap cross reference: $value", "error");
  332. watchdog('t_featuremap', "Error cannot add featuremap cross reference: %ref",
  333. array('%ref' => $value), WATCHDOG_ERROR);
  334. }
  335. }
  336. unset($properties[$map_dbxref_cvterm_id]);
  337. }
  338. $details = array(
  339. 'property_table' => 'featuremapprop',
  340. 'base_table' => 'featuremap',
  341. 'foreignkey_name' => 'featuremap_id',
  342. 'foreignkey_value' => $featuremap_id
  343. );
  344. chado_node_properties_form_update_properties($node, $details, $properties);
  345. }
  346. /**
  347. * When a node is requested by the user this function is called to allow us
  348. * to add auxiliary data to the node object.
  349. *
  350. * @ingroup tripal_featuremap
  351. */
  352. function chado_featuremap_load($nodes) {
  353. foreach ($nodes as $nid => $node) {
  354. // get the feature details from chado
  355. $featuremap_id = chado_get_id_for_node('featuremap', $node->nid);
  356. $values = array('featuremap_id' => $featuremap_id);
  357. $featuremap = tripal_core_generate_chado_var('featuremap', $values);
  358. // expand the description field as it is needed by the form
  359. $featuremap = tripal_core_expand_chado_vars($featuremap, 'field', 'featuremap.description');
  360. $nodes[$nid]->featuremap = $featuremap;
  361. }
  362. }
  363. /**
  364. * Delete data from drupal and chado databases when a node is deleted
  365. * @ingroup tripal_featuremap
  366. */
  367. function chado_featuremap_delete(&$node) {
  368. $featuremap_id = chado_get_id_for_node('featuremap', $node->nid);
  369. // if we don't have a map id for this node then this isn't a node of
  370. // type chado_featuremap or the entry in the chado_featuremap table was lost.
  371. if (!$featuremap_id) {
  372. return;
  373. }
  374. // Remove data from {chado_featuremap}, {node} and {node_revisions} tables of
  375. // drupal database
  376. $sql_del = "DELETE FROM {chado_featuremap} WHERE nid = :nid AND vid = :vid";
  377. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  378. $sql_del = "DELETE FROM {node} WHERE nid = :nid AND vid = :vid";
  379. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  380. $sql_del = "DELETE FROM {node_revisions} WHERE nid = :nid AND vid = :vid";
  381. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  382. // Remove data from map and mapprop tables of chado database as well
  383. chado_query("DELETE FROM {featuremapprop} WHERE featuremap_id = :featuremap_id", array(':featuremap_id' => $featuremap_id));
  384. chado_query("DELETE FROM {featuremap_dbxref} WHERE featuremap_id = :featuremap_id", array(':featuremap_id' => $featuremap_id));
  385. chado_query("DELETE FROM {featuremap} WHERE featuremap_id = :featuremap_id", array(':featuremap_id' => $featuremap_id));
  386. }
  387. /**
  388. *
  389. * @param $node
  390. */
  391. function tripal_featuremap_node_presave($node) {
  392. switch ($node->type) {
  393. case 'chado_featuremap':
  394. // for a form submission the 'fmapname' field will be set,
  395. // for a sync, we must pull from the featuremap object
  396. if(property_exists($node, 'fmapname')) {
  397. // set the title
  398. $node->title = $node->fmapname;
  399. }
  400. else {
  401. $node->title = $node->featuremap->name;
  402. }
  403. break;
  404. }
  405. }
  406. /**
  407. *
  408. * @ingroup tripal_feature
  409. */
  410. function tripal_featuremap_node_view($node, $view_mode, $langcode) {
  411. switch ($node->type) {
  412. case 'chado_featuremap':
  413. // Show feature browser and counts
  414. if ($view_mode == 'full') {
  415. $node->content['tripal_featuremap_base'] = array(
  416. '#markup' => theme('tripal_featuremap_base', array('node' => $node)),
  417. '#tripal_toc_id' => 'base',
  418. '#tripal_toc_title' => 'Overview',
  419. '#weight' => -100,
  420. );
  421. $node->content['tripal_featuremap_featurepos'] = array(
  422. '#markup' => theme('tripal_featuremap_featurepos', array('node' => $node)),
  423. '#tripal_toc_id' => 'featurepos',
  424. '#tripal_toc_title' => 'Map Features',
  425. );
  426. $node->content['tripal_featuremap_properties'] = array(
  427. '#markup' => theme('tripal_featuremap_properties', array('node' => $node)),
  428. '#tripal_toc_id' => 'properties',
  429. '#tripal_toc_title' => 'Properties',
  430. );
  431. $node->content['tripal_featuremap_publication'] = array(
  432. '#markup' => theme('tripal_featuremap_publication', array('node' => $node)),
  433. '#tripal_toc_id' => 'publications',
  434. '#tripal_toc_title' => 'Publications',
  435. );
  436. $node->content['tripal_featuremap_references'] = array(
  437. '#markup' => theme('tripal_featuremap_references', array('node' => $node)),
  438. '#tripal_toc_id' => 'references',
  439. '#tripal_toc_title' => 'Cross References',
  440. );
  441. }
  442. if ($view_mode == 'teaser') {
  443. $node->content['tripal_featuremap_teaser'] = array(
  444. '#markup' => theme('tripal_featuremap_teaser', array('node' => $node)),
  445. );
  446. }
  447. break;
  448. }
  449. }