tripal_project.chado_node.inc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. <?php
  2. /**
  3. * Implementation of hook_node_info().
  4. *
  5. * This node_info, is a simple node that describes the functionallity of the module. It specifies
  6. * that the title(Project Name) and body(Description) set to true so that they information can be
  7. * entered
  8. *
  9. *
  10. * @ingroup tripal_project
  11. */
  12. function tripal_project_node_info() {
  13. return array(
  14. 'chado_project' => array(
  15. 'name' => t('Project'),
  16. 'base' => 'chado_project',
  17. 'description' => t('A project from the Chado database'),
  18. 'has_title' => TRUE,
  19. 'title_label' => t('Project Name'),
  20. 'had_body' => TRUE,
  21. 'body_label' => t('Full Description'),
  22. 'chado_node_api' => array(
  23. 'base_table' => 'project',
  24. 'hook_prefix' => 'chado_project',
  25. 'record_type_title' => array(
  26. 'singular' => t('Project'),
  27. 'plural' => t('Projects')
  28. ),
  29. 'sync_filters' => array(
  30. 'type_id' => FALSE,
  31. 'organism_id' => FALSE
  32. ),
  33. ),
  34. ),
  35. );
  36. }
  37. /**
  38. * Implementation of hook_form().
  39. *
  40. * This form takes the Project Title information and description from the user.
  41. *
  42. * @parm $node
  43. * The initialized node
  44. *
  45. * @parm $form_state
  46. * The state of the form, that has the user entered information that is neccessary for adding
  47. * information to the project
  48. *
  49. * @return $form
  50. * An array as described by the Drupal Form API
  51. *
  52. *
  53. * @ingroup tripal_project
  54. */
  55. function chado_project_form(&$node, $form_state) {
  56. $form = array();
  57. // Default values can come in the following ways:
  58. //
  59. // 1) as elements of the $node object. This occurs when editing an existing project
  60. // 2) in the $form_state['values'] array which occurs on a failed validation or
  61. // ajax callbacks from non submit form elements
  62. // 3) in the $form_state['input'[ array which occurs on ajax callbacks from submit
  63. // form elements and the form is being rebuilt
  64. //
  65. // set form field defaults
  66. $project_id = null;
  67. $title = '';
  68. $description = '';
  69. // if we are editing an existing node then the project is already part of the node
  70. if (property_exists($node, 'project')) {
  71. $project = $node->project;
  72. // get the project default values. When this module was first created
  73. // the project description was incorrectly stored in the $node->body field.
  74. // It is better to store it in the Chado tables. However, the 'description'
  75. // field of the project table is only 255 characters. So, we are going
  76. // to follow the same as the project module and store the description in
  77. // the projectprop table and leave the project.description field blank.
  78. // however, for backwards compatibitily, we check to see if the description
  79. // is in the $node->body field. If it is we'll use that. When the node is
  80. // edited the text will be moved out of the body and into the projectprop
  81. // table where it should belong.
  82. if (property_exists($node, 'body')) {
  83. $description = $node->body;
  84. }
  85. else {
  86. $description = $project->description;
  87. }
  88. if (!$description) {
  89. $projectprop = tripal_project_get_property($project->project_id, 'Project Description');
  90. $description = $projectprop->value;
  91. }
  92. $title = $project->name;
  93. $project_id = $project->project_id;
  94. // keep track of the project id if we have. If we do have one then
  95. // this is an update as opposed to an insert.
  96. $form['project_id'] = array(
  97. '#type' => 'value',
  98. '#value' => $project_id,
  99. );
  100. }
  101. // if we are re constructing the form from a failed validation or ajax callback
  102. // then use the $form_state['values'] values
  103. if (array_key_exists('values', $form_state)) {
  104. $title = $form_state['values']['title'];
  105. $description = $form_state['values']['description'];
  106. }
  107. // if we are re building the form from after submission (from ajax call) then
  108. // the values are in the $form_state['input'] array
  109. if (array_key_exists('input', $form_state) and !empty($form_state['input'])) {
  110. $title = $form_state['input']['title'];
  111. $description = $form_state['input']['description'];
  112. }
  113. $form['title']= array(
  114. '#type' => 'textfield',
  115. '#title' => t('Project Title'),
  116. '#description' => t('Please enter the title for this project. This appears at the top of the project page.'),
  117. '#required' => TRUE,
  118. '#default_value' => $node->title,
  119. );
  120. $form['description']= array(
  121. '#type' => 'textarea',
  122. '#title' => t('Project Description'),
  123. '#description' => t('A brief description of the project'),
  124. '#required' => TRUE,
  125. '#default_value' => $description,
  126. );
  127. // get the project properties
  128. $properties = array();
  129. $properties[] = 'Select a Property';
  130. $sql = "
  131. SELECT DISTINCT CVT.cvterm_id, CVT.name, CVT.definition
  132. FROM {cvterm} CVT
  133. INNER JOIN {cv} ON CVT.cv_id = CV.cv_id
  134. WHERE
  135. CV.name = 'project_property' AND
  136. NOT CVT.is_obsolete = 1
  137. ORDER BY CVT.name ASC
  138. ";
  139. $prop_types = chado_query($sql);
  140. while ($prop = $prop_types->fetchObject()) {
  141. // because we are using the project description property as
  142. // a replacement for the project.description field and we want the
  143. // user to add a description in the field above, we remove the property
  144. // from the list.
  145. if (strcmp($prop->name, "Project Description")==0) {
  146. continue;
  147. }
  148. $properties[$prop->cvterm_id] = $prop->name;
  149. }
  150. // we want to exclude the project description from being loaded as a stored property
  151. // because we want to use the property to replace the project.description field as it is
  152. // only 255 characters which isn't large enough. We don't want the user to set it
  153. // as a property even though it will be stored as a property.
  154. $exclude = array('Project Description');
  155. $include = array();
  156. $instructions = t('To add properties to the drop down list, you must ' . l("add terms to the project_property vocabulary", "admin/tripal/chado/tripal_cv/cvterm/add") . ".");
  157. tripal_core_properties_form($form, $form_state, 'projectprop', 'project_id', 'project_property',
  158. $properties, $project_id, $exclude, $include, $instructions, 'Properties');
  159. return $form;
  160. }
  161. /**
  162. * validates submission of form when adding or updating a project node
  163. *
  164. * @ingroup tripal_project
  165. */
  166. function chado_project_validate($node, $form, &$form_state) {
  167. $node->title = trim($node->title);
  168. $node->description = trim($node->description);
  169. // if this is a delete then don't validate
  170. if($node->op == 'Delete') {
  171. return;
  172. }
  173. // we are syncing if we do not have a node ID but we do have a project_id. We don't
  174. // need to validate during syncing so just skip it.
  175. if (is_null($node->nid) and property_exists($node, 'project_id') and $node->project_id != 0) {
  176. return;
  177. }
  178. $project = 0;
  179. // check to make sure the name on the project is unique
  180. // before we try to insert into chado.
  181. if (property_exists($node, 'project_id')) {
  182. $sql = "SELECT * FROM {project} WHERE name = :name AND NOT project_id = :project_id";
  183. $project = chado_query($sql, array(':name' => $node->title, ':project_id' => $node->project_id))->fetchObject();
  184. }
  185. else {
  186. $sql = "SELECT * FROM {project} WHERE name = :name";
  187. $project = chado_query($sql, array(':name' => $node->title))->fetchObject();
  188. }
  189. if ($project) {
  190. form_set_error('title', t('The unique project name already exists. Please choose another'));
  191. }
  192. }
  193. /**
  194. * Implementation of hook_insert().
  195. *
  196. * @parm $node
  197. * Then node that has the information stored within, accessed given the nid
  198. *
  199. *
  200. * @ingroup tripal_project
  201. */
  202. function chado_project_insert($node) {
  203. $node->title = trim($node->title);
  204. $node->description = trim($node->description);
  205. // if there is an project_id in the $node object then this must be a sync so
  206. // we can skip adding the project as it is already there, although
  207. // we do need to proceed with the rest of the insert
  208. if (!property_exists($node, 'project_id')) {
  209. $values = array(
  210. 'name' => $node->title,
  211. 'description' => '',
  212. );
  213. $project = tripal_core_chado_insert('project', $values);
  214. if (!$project) {
  215. drupal_set_message(t('Unable to add project.', 'warning'));
  216. watchdog('tripal_project', 'Insert project: Unable to create project where values:%values',
  217. array('%values' => print_r($values, TRUE)), WATCHDOG_ERROR);
  218. return;
  219. }
  220. $project_id = $project['project_id'];
  221. // now add in the properties
  222. $properties = tripal_core_properties_form_retreive($node, 'project_property');
  223. foreach ($properties as $property => $elements) {
  224. foreach ($elements as $rank => $value) {
  225. $status = tripal_project_insert_property($project_id, $property, $value, FALSE, 'project_property');
  226. if (!$status) {
  227. drupal_set_message("Error cannot add property: $property", "error");
  228. watchdog('t_project', "Error cannot add property: %prop",
  229. array('%property' => $property), WATCHDOG_ERROR);
  230. }
  231. }
  232. }
  233. // add in the description as a separate property
  234. tripal_project_insert_property($project_id, 'Project Description', $node->description, FALSE);
  235. }
  236. else {
  237. $project_id = $node->project_id;
  238. }
  239. // Make sure the entry for this project doesn't already exist in the
  240. // chado_project table if it doesn't exist then we want to add it.
  241. $check_org_id = chado_get_id_for_node('project', $node->nid);
  242. if (!$check_org_id) {
  243. $record = new stdClass();
  244. $record->nid = $node->nid;
  245. $record->vid = $node->vid;
  246. $record->project_id = $project_id;
  247. drupal_write_record('chado_project', $record);
  248. }
  249. }
  250. /**
  251. *
  252. * Implementation of hook_delete().
  253. *
  254. * @param $node
  255. * The node which is to be deleted, only chado project and chado_project need to be dealt with
  256. * since the drupal node is deleted automagically
  257. *
  258. *
  259. * @ingroup tripal_project
  260. */
  261. function chado_project_delete($node) {
  262. $project_id = chado_get_id_for_node('project', $node->nid);
  263. // if we don't have a project id for this node then this isn't a node of
  264. // type chado_project or the entry in the chado_project table was lost.
  265. if (!$project_id) {
  266. return;
  267. }
  268. // Remove data from {chado_project}, {node} and {node_revisions} tables of
  269. // drupal database
  270. $sql_del = "DELETE FROM {chado_project} WHERE nid = :nid AND vid = :vid";
  271. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  272. $sql_del = "DELETE FROM {node_revision} WHERE nid = :nid AND vid = :vod";
  273. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  274. $sql_del = "DELETE FROM {node} WHERE nid = :nid AND vid = :vid";
  275. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  276. // Remove data from project and projectprop tables of chado database as well
  277. chado_query("DELETE FROM {projectprop} WHERE project_id = :project_id", array(':project_id' => $project_id));
  278. chado_query("DELETE FROM {project} WHERE project_id = :project_id", array(':project_id' => $project_id));
  279. }
  280. /**
  281. * Implements hook_update().
  282. *
  283. * @param $node
  284. * The node which is to have its containing information updated when the user modifies information
  285. * pertaining to the specific project
  286. *
  287. *
  288. * @ingroup tripal_project
  289. */
  290. function chado_project_update($node) {
  291. $node->title = trim($node->title);
  292. $node->description = trim($node->description);
  293. // update the project and the description
  294. $project_id = chado_get_id_for_node('project', $node->nid) ;
  295. $match = array('project_id' => $project_id);
  296. $values = array(
  297. 'name' => $node->title,
  298. 'description' => '',
  299. );
  300. $status = tripal_core_chado_update('project', $match, $values);
  301. if (!$status) {
  302. drupal_set_message(t('Unable to update project.', 'warning'));
  303. watchdog('tripal_project', 'Update project: Unable to update project where values: %values',
  304. array('%values' => print_r($values, TRUE)), WATCHDOG_ERROR);
  305. }
  306. // now add in the properties by first removing any the project
  307. // already has and adding the ones we have
  308. tripal_core_chado_delete('projectprop', array('project_id' => $project_id));
  309. $properties = tripal_core_properties_form_retreive($node, 'project_property');
  310. foreach ($properties as $property => $elements) {
  311. foreach ($elements as $rank => $value) {
  312. $status = tripal_project_insert_property($project_id, $property, $value, FALSE, 'project_property');
  313. if (!$status) {
  314. drupal_set_message("Error cannot add property: '$property'", "error");
  315. watchdog('t_project', "Error cannot add property: '%prop'",
  316. array('%prop' => $property), WATCHDOG_ERROR);
  317. }
  318. }
  319. }
  320. // add the project description as a property
  321. tripal_project_update_property($project_id, 'Project Description', $node->description, 1);
  322. }
  323. /**
  324. * Implementation of node_load().
  325. *
  326. * @param $node
  327. * The node that is to have its containing information loaded
  328. *
  329. * @return $node
  330. * The node, containing the loaded project with the current nid
  331. *
  332. *
  333. * @ingroup tripal_project
  334. */
  335. function chado_project_load($nodes) {
  336. foreach ($nodes as $nid => $node) {
  337. // get the feature details from chado
  338. $project_id = chado_get_id_for_node('project', $node->nid);
  339. $values = array('project_id' => $project_id);
  340. $project = tripal_core_generate_chado_var('project', $values);
  341. $nodes[$nid]->project = $project;
  342. }
  343. }
  344. /**
  345. * Implement hook_access().
  346. *
  347. * This hook allows node modules to limit access to the node types they define.
  348. *
  349. * @param $node
  350. * The node on which the operation is to be performed, or, if it does not yet exist, the
  351. * type of node to be created
  352. *
  353. * @param $op
  354. * The operation to be performed
  355. *
  356. *
  357. * @param $account
  358. * A user object representing the user for whom the operation is to be performed
  359. *
  360. * @return
  361. * If the permission for the specified operation is not set then return FALSE. If the
  362. * permission is set then return NULL as this allows other modules to disable
  363. * access. The only exception is when the $op == 'create'. We will always
  364. * return TRUE if the permission is set.
  365. *
  366. * @ingroup tripal_project
  367. */
  368. function chado_project_node_access($node, $op, $account) {
  369. if ($op == 'create') {
  370. if (!user_access('create chado_projects content', $account)) {
  371. return FALSE;
  372. }
  373. return TRUE;
  374. }
  375. if ($op == 'update') {
  376. if (!user_access('edit chado_projects content', $account)) {
  377. return FALSE;
  378. }
  379. }
  380. if ($op == 'delete') {
  381. if (!user_access('delete chado_projects content', $account)) {
  382. return FALSE;
  383. }
  384. }
  385. if ($op == 'view') {
  386. if (!user_access('access chado_projects content', $account)) {
  387. return FALSE;
  388. }
  389. }
  390. return NULL;
  391. }
  392. /**
  393. *
  394. * @ingroup tripal_feature
  395. */
  396. function tripal_project_node_view($node, $view_mode, $langcode) {
  397. switch ($node->type) {
  398. case 'chado_project':
  399. // Show feature browser and counts
  400. if ($view_mode == 'full') {
  401. $node->content['tripal_project_base'] = array(
  402. '#value' => theme('tripal_project_base', array('node' => $node)),
  403. );
  404. $node->content['tripal_project_contact'] = array(
  405. '#value' => theme('tripal_project_contact', array('node' => $node)),
  406. );
  407. $node->content['tripal_project_properties'] = array(
  408. '#value' => theme('tripal_project_properties', array('node' => $node)),
  409. );
  410. $node->content['tripal_project_publications'] = array(
  411. '#value' => theme('tripal_project_publications', array('node' => $node)),
  412. );
  413. $node->content['tripal_project_relationships'] = array(
  414. '#value' => theme('tripal_project_relationships', array('node' => $node)),
  415. );
  416. }
  417. if ($view_mode == 'teaser') {
  418. $node->content['tripal_project_teaser'] = array(
  419. '#value' => theme('tripal_project_teaser', array('node' => $node)),
  420. );
  421. }
  422. break;
  423. }
  424. }