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