tripal_project.chado_node.inc 13 KB

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