tripal_project.chado_node.inc 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  1. <?php
  2. /**
  3. * @file
  4. * Implement the project node content type
  5. */
  6. /**
  7. * Implementation of hook_node_info().
  8. *
  9. * This node_info, is a simple node that describes the functionallity of the
  10. * module. It specifies that the title(Project Name) and body(Description) set
  11. * to true so that they information can be entered
  12. *
  13. * @ingroup tripal_legacy_project
  14. */
  15. function tripal_project_node_info() {
  16. return [
  17. 'chado_project' => [
  18. 'name' => t('Project (Tripal v2 legacy)'),
  19. 'base' => 'chado_project',
  20. 'description' => t('A project from the Chado database'),
  21. 'has_title' => TRUE,
  22. 'locked' => TRUE,
  23. 'chado_node_api' => [
  24. 'base_table' => 'project',
  25. 'hook_prefix' => 'chado_project',
  26. 'record_type_title' => [
  27. 'singular' => t('Project'),
  28. 'plural' => t('Projects'),
  29. ],
  30. 'sync_filters' => [
  31. 'type_id' => FALSE,
  32. 'organism_id' => FALSE,
  33. ],
  34. ],
  35. ],
  36. ];
  37. }
  38. /**
  39. * Implementation of hook_form().
  40. *
  41. * This form takes the Project Title information and description from the
  42. * user.
  43. *
  44. * @parm $node
  45. * The initialized node
  46. *
  47. * @parm $form_state
  48. * The state of the form, that has the user entered information that is
  49. * neccessary for adding information to the project
  50. *
  51. * @return $form
  52. * An array as described by the Drupal Form API
  53. *
  54. *
  55. * @ingroup tripal_legacy_project
  56. */
  57. function chado_project_form(&$node, $form_state) {
  58. $form = [];
  59. // Default values can come in the following ways:
  60. //
  61. // 1) as elements of the $node object. This occurs when editing an existing project
  62. // 2) in the $form_state['values'] array which occurs on a failed validation or
  63. // ajax callbacks from non submit form elements
  64. // 3) in the $form_state['input'[ array which occurs on ajax callbacks from submit
  65. // form elements and the form is being rebuilt
  66. //
  67. // set form field defaults
  68. $project_id = NULL;
  69. $title = '';
  70. $description = '';
  71. // if we are editing an existing node then the project is already part of the node
  72. if (property_exists($node, 'project')) {
  73. $project = $node->project;
  74. // get the project default values. When this module was first created
  75. // the project description was incorrectly stored in the $node->body field.
  76. // It is better to store it in the Chado tables. However, the 'description'
  77. // field of the project table is only 255 characters. So, we are going
  78. // to follow the same as the project module and store the description in
  79. // the projectprop table and leave the project.description field blank.
  80. // however, for backwards compatibitily, we check to see if the description
  81. // is in the $node->body field. If it is we'll use that. When the node is
  82. // edited the text will be moved out of the body and into the projectprop
  83. // table where it should belong.
  84. if (property_exists($node, 'body')) {
  85. $description = $node->body;
  86. }
  87. else {
  88. $description = $project->description;
  89. }
  90. if (!$description) {
  91. $projectprop = chado_get_property(
  92. ['table' => 'project', 'id' => $project->project_id],
  93. ['type_name' => 'Project Description', 'cv_name' => 'project_property']
  94. );
  95. $description = $projectprop->value;
  96. }
  97. $title = $project->name;
  98. $project_id = $project->project_id;
  99. // keep track of the project id if we have. If we do have one then
  100. // this is an update as opposed to an insert.
  101. $form['project_id'] = [
  102. '#type' => 'value',
  103. '#value' => $project_id,
  104. ];
  105. }
  106. // if we are re constructing the form from a failed validation or ajax callback
  107. // then use the $form_state['values'] values
  108. if (array_key_exists('values', $form_state)) {
  109. $title = $form_state['values']['title'];
  110. $description = $form_state['values']['description'];
  111. }
  112. // if we are re building the form from after submission (from ajax call) then
  113. // the values are in the $form_state['input'] array
  114. if (array_key_exists('input', $form_state) and !empty($form_state['input'])) {
  115. $title = $form_state['input']['title'];
  116. $description = $form_state['input']['description'];
  117. }
  118. $form['title'] = [
  119. '#type' => 'textfield',
  120. '#title' => t('Project Title'),
  121. '#description' => t('Please enter the title for this project. This appears at the top of the project page.'),
  122. '#required' => TRUE,
  123. '#default_value' => $node->title,
  124. ];
  125. $form['description'] = [
  126. '#type' => 'text_format',
  127. '#title' => t('Project Description'),
  128. '#description' => t('A brief description of the project'),
  129. '#required' => TRUE,
  130. '#default_value' => $description,
  131. ];
  132. // Properties Form
  133. // ----------------------------------
  134. $select_options = [];
  135. $prop_cv = tripal_get_default_cv('projectprop', 'type_id');
  136. $cv_id = $prop_cv ? $prop_cv->cv_id : NULL;
  137. if ($prop_cv = 'project_property') {
  138. // if this is the project_property CV then
  139. // we want to exclude the project description from being loaded as a stored property
  140. // because we want to use the property to replace the project.description field as it is
  141. // only 255 characters which isn't large enough. We don't want the user to set it
  142. // as a property even though it will be stored as a property.
  143. $cv_result = chado_select_record('cv', ['cv_id'], ['name' => 'project_property']);
  144. $cv_id = $cv_result[0]->cv_id;
  145. $select_options = tripal_get_cvterm_select_options($cv_id);
  146. $descrip_id = array_search('Project Description', $select_options);
  147. unset($select_options[$descrip_id]);
  148. }
  149. $instructions = t('To add properties to the drop down list, you must ' . l("add terms to the project_property vocabulary", "admin/tripal/vocab/cvterm/add") . ".");
  150. $details = [
  151. 'property_table' => 'projectprop',
  152. 'chado_id' => $project_id,
  153. 'cv_id' => $cv_id,
  154. 'additional_instructions' => $instructions,
  155. 'select_options' => $select_options,
  156. ];
  157. chado_add_node_form_properties($form, $form_state, $details);
  158. // RELATIONSHIPS FORM
  159. //---------------------------------------------
  160. $relationship_cv = tripal_get_default_cv('project_relationship', 'type_id');
  161. $cv_id = $relationship_cv ? $relationship_cv->cv_id : NULL;
  162. $details = [
  163. 'relationship_table' => 'project_relationship',
  164. // the name of the _relationship table
  165. 'base_table' => 'project',
  166. // the name of your chado base table
  167. 'base_foreign_key' => 'project_id',
  168. // the name of the key in your base chado table
  169. 'base_key_value' => $project_id,
  170. // the value of example_id for this record
  171. 'nodetype' => 'project',
  172. // the human-readable name of your node type
  173. 'cv_id' => $cv_id,
  174. // the cv.cv_id of the cv governing example_relationship.type_id
  175. 'base_name_field' => 'name',
  176. // the base table field you want to be used as the name
  177. 'subject_field_name' => 'subject_project_id',
  178. 'object_field_name' => 'object_project_id',
  179. ];
  180. // Adds the form elements to your current form
  181. chado_add_node_form_relationships($form, $form_state, $details);
  182. return $form;
  183. }
  184. /**
  185. * Implements hook_validate().
  186. * Validates submission of form when adding or updating a project node
  187. *
  188. * @ingroup tripal_legacy_project
  189. */
  190. function chado_project_validate($node, $form, &$form_state) {
  191. // We only want to validate when the node is saved.
  192. // Since this validate can be called on AJAX and Deletion of the node
  193. // we need to make this check to ensure queries are not executed
  194. // without the proper values.
  195. if (property_exists($node, "op") and $node->op != 'Save') {
  196. return;
  197. }
  198. // we are syncing if we do not have a node ID but we do have a project_id. We don't
  199. // need to validate during syncing so just skip it.
  200. if (!property_exists($node, 'nid') and property_exists($node, 'project_id') and $node->project_id != 0) {
  201. return;
  202. }
  203. // trim white space from text fields
  204. $node->title = property_exists($node, 'title') ? trim($node->title) : '';
  205. $project = 0;
  206. // check to make sure the name on the project is unique
  207. // before we try to insert into chado.
  208. if (property_exists($node, 'project_id')) {
  209. $sql = "SELECT * FROM {project} WHERE name = :name AND NOT project_id = :project_id";
  210. $project = chado_query($sql, [
  211. ':name' => $node->title,
  212. ':project_id' => $node->project_id,
  213. ])->fetchObject();
  214. }
  215. else {
  216. $sql = "SELECT * FROM {project} WHERE name = :name";
  217. $project = chado_query($sql, [':name' => $node->title])->fetchObject();
  218. }
  219. if ($project) {
  220. form_set_error('title', t('The unique project name already exists. Please choose another'));
  221. }
  222. }
  223. /**
  224. * Implementation of hook_insert().
  225. *
  226. * @parm $node
  227. * Then node that has the information stored within, accessed given the nid
  228. *
  229. * @ingroup tripal_legacy_project
  230. */
  231. function chado_project_insert($node) {
  232. $project_id = '';
  233. // if there is an project_id in the $node object then this must be a sync so
  234. // we can skip adding the project as it is already there, although
  235. // we do need to proceed with insertion into the chado/drupal linking table.
  236. if (!property_exists($node, 'project_id')) {
  237. $node->title = trim($node->title);
  238. $node->description = trim($node->description['value']);
  239. $values = [
  240. 'name' => $node->title,
  241. 'description' => '',
  242. ];
  243. $project = chado_insert_record('project', $values);
  244. if (!$project) {
  245. drupal_set_message(t('Unable to add project.', 'warning'));
  246. watchdog('tripal_project', 'Insert project: Unable to create project where values:%values',
  247. ['%values' => print_r($values, TRUE)], WATCHDOG_ERROR);
  248. return;
  249. }
  250. $project_id = $project['project_id'];
  251. // * Properties Form *
  252. // Add the description property
  253. $properties = chado_retrieve_node_form_properties($node);
  254. $descrip_id = tripal_get_cvterm([
  255. 'name' => 'Project Description',
  256. 'cv_id' => ['name' => 'project_property'],
  257. ]);
  258. $properties[$descrip_id->cvterm_id][0] = $node->description;
  259. $details = [
  260. 'property_table' => 'projectprop',
  261. 'base_table' => 'project',
  262. 'foreignkey_name' => 'project_id',
  263. 'foreignkey_value' => $project_id,
  264. ];
  265. chado_update_node_form_properties($node, $details, $properties);
  266. // * Relationships Form *
  267. $details = [
  268. 'relationship_table' => 'project_relationship',
  269. // name of the _relationship table
  270. 'foreignkey_value' => $project_id
  271. // value of the example_id key
  272. ];
  273. chado_update_node_form_relationships($node, $details);
  274. }
  275. else {
  276. $project_id = $node->project_id;
  277. }
  278. // Make sure the entry for this project doesn't already exist in the
  279. // chado_project table if it doesn't exist then we want to add it.
  280. $check_org_id = chado_get_id_from_nid('project', $node->nid);
  281. if (!$check_org_id) {
  282. $record = new stdClass();
  283. $record->nid = $node->nid;
  284. $record->vid = $node->vid;
  285. $record->project_id = $project_id;
  286. drupal_write_record('chado_project', $record);
  287. }
  288. }
  289. /**
  290. * Implementation of hook_delete().
  291. *
  292. * @param $node
  293. * The node which is to be deleted, only chado project and chado_project need
  294. * to be dealt with since the drupal node is deleted automagically
  295. *
  296. * @ingroup tripal_legacy_project
  297. */
  298. function chado_project_delete($node) {
  299. $project_id = chado_get_id_from_nid('project', $node->nid);
  300. // if we don't have a project id for this node then this isn't a node of
  301. // type chado_project or the entry in the chado_project table was lost.
  302. if (!$project_id) {
  303. return;
  304. }
  305. // Remove data from {chado_project}, {node} and {node_revisions} tables of
  306. // drupal database
  307. $sql_del = "DELETE FROM {chado_project} WHERE nid = :nid AND vid = :vid";
  308. db_query($sql_del, [':nid' => $node->nid, ':vid' => $node->vid]);
  309. $sql_del = "DELETE FROM {node_revision} WHERE nid = :nid AND vid = :vid";
  310. db_query($sql_del, [':nid' => $node->nid, ':vid' => $node->vid]);
  311. $sql_del = "DELETE FROM {node} WHERE nid = :nid AND vid = :vid";
  312. db_query($sql_del, [':nid' => $node->nid, ':vid' => $node->vid]);
  313. // Remove data from project and projectprop tables of chado database as well
  314. chado_query("DELETE FROM {projectprop} WHERE project_id = :project_id", [':project_id' => $project_id]);
  315. chado_query("DELETE FROM {project} WHERE project_id = :project_id", [':project_id' => $project_id]);
  316. }
  317. /**
  318. * Implements hook_update().
  319. *
  320. * @param $node
  321. * The node which is to have its containing information updated when the user
  322. * modifies information pertaining to the specific project
  323. *
  324. * @ingroup tripal_legacy_project
  325. */
  326. function chado_project_update($node) {
  327. $node->title = trim($node->title);
  328. $node->description = trim($node->description['value']);
  329. // update the project and the description
  330. $project_id = chado_get_id_from_nid('project', $node->nid);
  331. $match = ['project_id' => $project_id];
  332. $values = [
  333. 'name' => $node->title,
  334. 'description' => '',
  335. ];
  336. $status = chado_update_record('project', $match, $values);
  337. if (!$status) {
  338. drupal_set_message(t('Unable to update project.', 'warning'));
  339. watchdog('tripal_project', 'Update project: Unable to update project where values: %values',
  340. ['%values' => print_r($values, TRUE)], WATCHDOG_ERROR);
  341. }
  342. // * Properties Form *
  343. // Add the description property
  344. $properties = chado_retrieve_node_form_properties($node);
  345. $descrip_id = tripal_get_cvterm([
  346. 'name' => 'Project Description',
  347. 'cv_id' => ['name' => 'project_property'],
  348. ]);
  349. $properties[$descrip_id->cvterm_id][0] = $node->description;
  350. $details = [
  351. 'property_table' => 'projectprop',
  352. 'base_table' => 'project',
  353. 'foreignkey_name' => 'project_id',
  354. 'foreignkey_value' => $project_id,
  355. ];
  356. chado_update_node_form_properties($node, $details, $properties);
  357. // * Relationships Form *
  358. $details = [
  359. 'relationship_table' => 'project_relationship',
  360. // name of the _relationship table
  361. 'foreignkey_value' => $project_id
  362. // value of the example_id key
  363. ];
  364. chado_update_node_form_relationships($node, $details);
  365. }
  366. /**
  367. * Implementation of hook_load().
  368. *
  369. * @param $node
  370. * The node that is to have its containing information loaded
  371. *
  372. * @ingroup tripal_legacy_project
  373. */
  374. function chado_project_load($nodes) {
  375. foreach ($nodes as $nid => $node) {
  376. // get the feature details from chado
  377. $project_id = chado_get_id_from_nid('project', $node->nid);
  378. // if the nid does not have a matching record then skip this node.
  379. // this can happen with orphaned nodes.
  380. if (!$project_id) {
  381. continue;
  382. }
  383. $values = ['project_id' => $project_id];
  384. $project = chado_generate_var('project', $values);
  385. $nodes[$nid]->project = $project;
  386. // Now get the title
  387. $node->title = chado_get_node_title($node);
  388. }
  389. }
  390. /**
  391. * Implement hook_node_access().
  392. *
  393. * This hook allows node modules to limit access to the node types they define.
  394. *
  395. * @param $node
  396. * The node on which the operation is to be performed, or, if it does not yet
  397. * exist, the type of node to be created
  398. *
  399. * @param $op
  400. * The operation to be performed
  401. *
  402. *
  403. * @param $account
  404. * A user object representing the user for whom the operation is to be
  405. * performed
  406. *
  407. * @return
  408. * If the permission for the specified operation is not set then return FALSE.
  409. * If the permission is set then return NULL as this allows other modules to
  410. * disable access. The only exception is when the $op == 'create'. We will
  411. * always return TRUE if the permission is set.
  412. *
  413. * @ingroup tripal_legacy_project
  414. */
  415. function tripal_project_node_access($node, $op, $account) {
  416. $node_type = $node;
  417. if (is_object($node)) {
  418. $node_type = $node->type;
  419. }
  420. if ($node_type == 'chado_project') {
  421. if ($op == 'create') {
  422. if (!user_access('create chado_project content', $account)) {
  423. return NODE_ACCESS_DENY;
  424. }
  425. return NODE_ACCESS_ALLOW;
  426. }
  427. if ($op == 'update') {
  428. if (!user_access('edit chado_project content', $account)) {
  429. return NODE_ACCESS_DENY;
  430. }
  431. }
  432. if ($op == 'delete') {
  433. if (!user_access('delete chado_project content', $account)) {
  434. return NODE_ACCESS_DENY;
  435. }
  436. }
  437. if ($op == 'view') {
  438. if (!user_access('access chado_project content', $account)) {
  439. return NODE_ACCESS_DENY;
  440. }
  441. }
  442. return NODE_ACCESS_IGNORE;
  443. }
  444. }
  445. /**
  446. * Implements hook_node_view().
  447. *
  448. * @ingroup tripal_legacy_project
  449. */
  450. function tripal_project_node_view($node, $view_mode, $langcode) {
  451. switch ($node->type) {
  452. case 'chado_project':
  453. // Show feature browser and counts
  454. if ($view_mode == 'full') {
  455. $node->content['tripal_project_base'] = [
  456. '#theme' => 'tripal_project_base',
  457. '#node' => $node,
  458. '#tripal_toc_id' => 'base',
  459. '#tripal_toc_title' => 'Overview',
  460. '#weight' => -100,
  461. ];
  462. $node->content['tripal_project_contact'] = [
  463. '#theme' => 'tripal_project_contact',
  464. '#node' => $node,
  465. '#tripal_toc_id' => 'contacts',
  466. '#tripal_toc_title' => 'Contacts',
  467. ];
  468. $node->content['tripal_project_properties'] = [
  469. '#theme' => 'tripal_project_properties',
  470. '#node' => $node,
  471. '#tripal_toc_id' => 'properties',
  472. '#tripal_toc_title' => 'Properties',
  473. ];
  474. $node->content['tripal_project_publications'] = [
  475. '#theme' => 'tripal_project_publications',
  476. '#node' => $node,
  477. '#tripal_toc_id' => 'publications',
  478. '#tripal_toc_title' => 'Publications',
  479. ];
  480. $node->content['tripal_project_relationships'] = [
  481. '#theme' => 'tripal_project_relationships',
  482. '#node' => $node,
  483. '#tripal_toc_id' => 'relationships',
  484. '#tripal_toc_title' => 'Relationships',
  485. ];
  486. }
  487. if ($view_mode == 'teaser') {
  488. $node->content['tripal_project_teaser'] = [
  489. '#theme' => 'tripal_project_teaser',
  490. '#node' => $node,
  491. ];
  492. }
  493. break;
  494. }
  495. }
  496. /**
  497. * Implements hook_node_insert().
  498. * Acts on all content types.
  499. *
  500. * @ingroup tripal_legacy_project
  501. */
  502. function tripal_project_node_insert($node) {
  503. // set the URL path after inserting. We do it here because we do not
  504. // know the project_id in the presave
  505. switch ($node->type) {
  506. case 'chado_project':
  507. // We still don't have a fully loaded node object in this hook. Therefore,
  508. // we need to simulate one so that the right values are available for
  509. // the URL to be determined.
  510. $project_id = chado_get_id_from_nid('project', $node->nid);
  511. $node->project = chado_generate_var('project', ['project_id' => $project_id]);
  512. // Now get the title.
  513. $node->title = chado_get_node_title($node);
  514. // Now use the API to set the path.
  515. chado_set_node_url($node);
  516. break;
  517. }
  518. }
  519. /**
  520. * Implements hook_node_update().
  521. * Acts on all content types.
  522. *
  523. * @ingroup tripal_legacy_project
  524. */
  525. function tripal_project_node_update($node) {
  526. // add items to other nodes, build index and search results
  527. switch ($node->type) {
  528. case 'chado_project':
  529. // Now get the title
  530. $node->title = chado_get_node_title($node);
  531. // Now use the API to set the path.
  532. chado_set_node_url($node);
  533. break;
  534. }
  535. }
  536. /**
  537. * Implements [content_type]_chado_node_default_title_format().
  538. *
  539. * Defines a default title format for the Chado Node API to set the titles on
  540. * Chado project nodes based on chado fields.
  541. */
  542. function chado_project_chado_node_default_title_format() {
  543. return '[project.name]';
  544. }
  545. /**
  546. * Implements hook_chado_node_default_url_format().
  547. *
  548. * Designates a default URL format for project nodes.
  549. */
  550. function chado_project_chado_node_default_url_format() {
  551. return '/project/[project.project_id]';
  552. }