tripal_featuremap.chado_node.inc 19 KB

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