tripal_project.chado_node.inc 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  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 module. It specifies
  10. * that the title(Project Name) and body(Description) set to true so that they information can be
  11. * entered
  12. *
  13. * @ingroup tripal_project
  14. */
  15. function tripal_project_node_info() {
  16. return array(
  17. 'chado_project' => array(
  18. 'name' => t('Project'),
  19. 'base' => 'chado_project',
  20. 'description' => t('A project from the Chado database'),
  21. 'has_title' => TRUE,
  22. 'locked' => TRUE,
  23. 'chado_node_api' => array(
  24. 'base_table' => 'project',
  25. 'hook_prefix' => 'chado_project',
  26. 'record_type_title' => array(
  27. 'singular' => t('Project'),
  28. 'plural' => t('Projects')
  29. ),
  30. 'sync_filters' => array(
  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 user.
  42. *
  43. * @parm $node
  44. * The initialized node
  45. *
  46. * @parm $form_state
  47. * The state of the form, that has the user entered information that is neccessary for adding
  48. * information to the project
  49. *
  50. * @return $form
  51. * An array as described by the Drupal Form API
  52. *
  53. *
  54. * @ingroup tripal_project
  55. */
  56. function chado_project_form(&$node, $form_state) {
  57. $form = array();
  58. // Default values can come in the following ways:
  59. //
  60. // 1) as elements of the $node object. This occurs when editing an existing project
  61. // 2) in the $form_state['values'] array which occurs on a failed validation or
  62. // ajax callbacks from non submit form elements
  63. // 3) in the $form_state['input'[ array which occurs on ajax callbacks from submit
  64. // form elements and the form is being rebuilt
  65. //
  66. // set form field defaults
  67. $project_id = null;
  68. $title = '';
  69. $description = '';
  70. // if we are editing an existing node then the project is already part of the node
  71. if (property_exists($node, 'project')) {
  72. $project = $node->project;
  73. // get the project default values. When this module was first created
  74. // the project description was incorrectly stored in the $node->body field.
  75. // It is better to store it in the Chado tables. However, the 'description'
  76. // field of the project table is only 255 characters. So, we are going
  77. // to follow the same as the project module and store the description in
  78. // the projectprop table and leave the project.description field blank.
  79. // however, for backwards compatibitily, we check to see if the description
  80. // is in the $node->body field. If it is we'll use that. When the node is
  81. // edited the text will be moved out of the body and into the projectprop
  82. // table where it should belong.
  83. if (property_exists($node, 'body')) {
  84. $description = $node->body;
  85. }
  86. else {
  87. $description = $project->description;
  88. }
  89. if (!$description) {
  90. $projectprop = chado_get_property(
  91. 'project',
  92. $project->project_id,
  93. 'Project Description',
  94. 'project_property'
  95. );
  96. $description = $projectprop->value;
  97. }
  98. $title = $project->name;
  99. $project_id = $project->project_id;
  100. // keep track of the project id if we have. If we do have one then
  101. // this is an update as opposed to an insert.
  102. $form['project_id'] = array(
  103. '#type' => 'value',
  104. '#value' => $project_id,
  105. );
  106. }
  107. // if we are re constructing the form from a failed validation or ajax callback
  108. // then use the $form_state['values'] values
  109. if (array_key_exists('values', $form_state)) {
  110. $title = $form_state['values']['title'];
  111. $description = $form_state['values']['description'];
  112. }
  113. // if we are re building the form from after submission (from ajax call) then
  114. // the values are in the $form_state['input'] array
  115. if (array_key_exists('input', $form_state) and !empty($form_state['input'])) {
  116. $title = $form_state['input']['title'];
  117. $description = $form_state['input']['description'];
  118. }
  119. $form['title']= array(
  120. '#type' => 'textfield',
  121. '#title' => t('Project Title'),
  122. '#description' => t('Please enter the title for this project. This appears at the top of the project page.'),
  123. '#required' => TRUE,
  124. '#default_value' => $node->title,
  125. );
  126. $form['description']= array(
  127. '#type' => 'textarea',
  128. '#title' => t('Project Description'),
  129. '#description' => t('A brief description of the project'),
  130. '#required' => TRUE,
  131. '#default_value' => $description,
  132. );
  133. // Properties Form
  134. // ----------------------------------
  135. $select_options = array();
  136. $prop_cv = tripal_get_default_cv('projectprop', 'type_id');
  137. $cv_id = $prop_cv ? $prop_cv->cv_id : NULL;
  138. if ($prop_cv = 'project_property') {
  139. // if this is the project_property CV then
  140. // we want to exclude the project description from being loaded as a stored property
  141. // because we want to use the property to replace the project.description field as it is
  142. // only 255 characters which isn't large enough. We don't want the user to set it
  143. // as a property even though it will be stored as a property.
  144. $cv_result = chado_select_record('cv',array('cv_id'),array('name' => 'project_property'));
  145. $cv_id = $cv_result[0]->cv_id;
  146. $select_options = tripal_get_cvterm_select_options($cv_id);
  147. $descrip_id = array_search('Project Description', $select_options);
  148. unset($select_options[$descrip_id]);
  149. }
  150. $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") . ".");
  151. $details = array(
  152. 'property_table' => 'projectprop',
  153. 'chado_id' => $project_id,
  154. 'cv_id' => $cv_id,
  155. 'additional_instructions' => $instructions,
  156. 'select_options' => $select_options
  157. );
  158. chado_add_node_form_properties($form, $form_state, $details);
  159. // RELATIONSHIPS FORM
  160. //---------------------------------------------
  161. $relationship_cv = tripal_get_default_cv('project_relationship', 'type_id');
  162. $cv_id = $relationship_cv ? $relationship_cv->cv_id : NULL;
  163. $details = array(
  164. 'relationship_table' => 'project_relationship', // the name of the _relationship table
  165. 'base_table' => 'project', // the name of your chado base table
  166. 'base_foreign_key' => 'project_id', // the name of the key in your base chado table
  167. 'base_key_value' => $project_id, // the value of example_id for this record
  168. 'nodetype' => 'project', // the human-readable name of your node type
  169. 'cv_id' => $cv_id, // the cv.cv_id of the cv governing example_relationship.type_id
  170. 'base_name_field' => 'name', // the base table field you want to be used as the name
  171. 'subject_field_name' => 'subject_project_id',
  172. 'object_field_name' => 'object_project_id',
  173. 'select_options' => $select_options
  174. );
  175. // Adds the form elements to your current form
  176. chado_add_node_form_relationships($form, $form_state, $details);
  177. return $form;
  178. }
  179. /**
  180. * Implements hook_validate().
  181. * Validates submission of form when adding or updating a project node
  182. *
  183. * @ingroup tripal_project
  184. */
  185. function chado_project_validate($node, $form, &$form_state) {
  186. // if this is a delete then don't validate
  187. if($node->op == 'Delete') {
  188. return;
  189. }
  190. // we are syncing if we do not have a node ID but we do have a project_id. We don't
  191. // need to validate during syncing so just skip it.
  192. if (is_null($node->nid) and property_exists($node, 'project_id') and $node->project_id != 0) {
  193. return;
  194. }
  195. // trim white space from text fields
  196. $node->title = trim($node->title);
  197. $node->description = trim($node->description);
  198. $project = 0;
  199. // check to make sure the name on the project is unique
  200. // before we try to insert into chado.
  201. if (property_exists($node, 'project_id')) {
  202. $sql = "SELECT * FROM {project} WHERE name = :name AND NOT project_id = :project_id";
  203. $project = chado_query($sql, array(':name' => $node->title, ':project_id' => $node->project_id))->fetchObject();
  204. }
  205. else {
  206. $sql = "SELECT * FROM {project} WHERE name = :name";
  207. $project = chado_query($sql, array(':name' => $node->title))->fetchObject();
  208. }
  209. if ($project) {
  210. form_set_error('title', t('The unique project name already exists. Please choose another'));
  211. }
  212. }
  213. /**
  214. * Implementation of hook_insert().
  215. *
  216. * @parm $node
  217. * Then node that has the information stored within, accessed given the nid
  218. *
  219. * @ingroup tripal_project
  220. */
  221. function chado_project_insert($node) {
  222. $node->title = trim($node->title);
  223. $node->description = trim($node->description);
  224. // if there is an project_id in the $node object then this must be a sync so
  225. // we can skip adding the project as it is already there, although
  226. // we do need to proceed with the rest of the insert
  227. if (!property_exists($node, 'project_id')) {
  228. $values = array(
  229. 'name' => $node->title,
  230. 'description' => '',
  231. );
  232. $project = chado_insert_record('project', $values);
  233. if (!$project) {
  234. drupal_set_message(t('Unable to add project.', 'warning'));
  235. watchdog('tripal_project', 'Insert project: Unable to create project where values:%values',
  236. array('%values' => print_r($values, TRUE)), WATCHDOG_ERROR);
  237. return;
  238. }
  239. $project_id = $project['project_id'];
  240. // * Properties Form *
  241. // Add the description property
  242. $properties = chado_retrieve_node_form_properties($node);
  243. $descrip_id = tripal_get_cvterm(array(
  244. 'name' => 'Project Description',
  245. 'cv_id' => array('name' => 'project_property')
  246. ));
  247. $properties[$descrip_id->cvterm_id][0] = $node->description;
  248. $details = array(
  249. 'property_table' => 'projectprop',
  250. 'base_table' => 'project',
  251. 'foreignkey_name' => 'project_id',
  252. 'foreignkey_value' => $project_id
  253. );
  254. chado_update_node_form_properties($node, $details, $properties);
  255. // * Relationships Form *
  256. $details = array(
  257. 'relationship_table' => 'project_relationship', // name of the _relationship table
  258. 'foreignkey_value' => $project_id // value of the example_id key
  259. );
  260. chado_update_node_form_relationships($node, $details);
  261. }
  262. else {
  263. $project_id = $node->project_id;
  264. }
  265. // Make sure the entry for this project doesn't already exist in the
  266. // chado_project table if it doesn't exist then we want to add it.
  267. $check_org_id = chado_get_id_from_nid('project', $node->nid);
  268. if (!$check_org_id) {
  269. $record = new stdClass();
  270. $record->nid = $node->nid;
  271. $record->vid = $node->vid;
  272. $record->project_id = $project_id;
  273. drupal_write_record('chado_project', $record);
  274. }
  275. }
  276. /**
  277. * Implementation of hook_delete().
  278. *
  279. * @param $node
  280. * The node which is to be deleted, only chado project and chado_project need to be dealt with
  281. * since the drupal node is deleted automagically
  282. *
  283. * @ingroup tripal_project
  284. */
  285. function chado_project_delete($node) {
  286. $project_id = chado_get_id_from_nid('project', $node->nid);
  287. // if we don't have a project id for this node then this isn't a node of
  288. // type chado_project or the entry in the chado_project table was lost.
  289. if (!$project_id) {
  290. return;
  291. }
  292. // Remove data from {chado_project}, {node} and {node_revisions} tables of
  293. // drupal database
  294. $sql_del = "DELETE FROM {chado_project} WHERE nid = :nid AND vid = :vid";
  295. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  296. $sql_del = "DELETE FROM {node_revision} WHERE nid = :nid AND vid = :vid";
  297. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  298. $sql_del = "DELETE FROM {node} WHERE nid = :nid AND vid = :vid";
  299. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  300. // Remove data from project and projectprop tables of chado database as well
  301. chado_query("DELETE FROM {projectprop} WHERE project_id = :project_id", array(':project_id' => $project_id));
  302. chado_query("DELETE FROM {project} WHERE project_id = :project_id", array(':project_id' => $project_id));
  303. }
  304. /**
  305. * Implements hook_update().
  306. *
  307. * @param $node
  308. * The node which is to have its containing information updated when the user modifies information
  309. * pertaining to the specific project
  310. *
  311. * @ingroup tripal_project
  312. */
  313. function chado_project_update($node) {
  314. $node->title = trim($node->title);
  315. $node->description = trim($node->description);
  316. // update the project and the description
  317. $project_id = chado_get_id_from_nid('project', $node->nid) ;
  318. $match = array('project_id' => $project_id);
  319. $values = array(
  320. 'name' => $node->title,
  321. 'description' => '',
  322. );
  323. $status = chado_update_record('project', $match, $values);
  324. if (!$status) {
  325. drupal_set_message(t('Unable to update project.', 'warning'));
  326. watchdog('tripal_project', 'Update project: Unable to update project where values: %values',
  327. array('%values' => print_r($values, TRUE)), WATCHDOG_ERROR);
  328. }
  329. // * Properties Form *
  330. // Add the description property
  331. $properties = chado_retrieve_node_form_properties($node);
  332. $descrip_id = tripal_get_cvterm(array(
  333. 'name' => 'Project Description',
  334. 'cv_id' => array('name' => 'project_property')
  335. ));
  336. $properties[$descrip_id->cvterm_id][0] = $node->description;
  337. $details = array(
  338. 'property_table' => 'projectprop',
  339. 'base_table' => 'project',
  340. 'foreignkey_name' => 'project_id',
  341. 'foreignkey_value' => $project_id
  342. );
  343. chado_update_node_form_properties($node, $details, $properties);
  344. // * Relationships Form *
  345. $details = array(
  346. 'relationship_table' => 'project_relationship', // name of the _relationship table
  347. 'foreignkey_value' => $project_id // value of the example_id key
  348. );
  349. chado_update_node_form_relationships($node, $details);
  350. }
  351. /**
  352. * Implementation of hook_load().
  353. *
  354. * @param $node
  355. * The node that is to have its containing information loaded
  356. *
  357. * @ingroup tripal_project
  358. */
  359. function chado_project_load($nodes) {
  360. foreach ($nodes as $nid => $node) {
  361. // get the feature details from chado
  362. $project_id = chado_get_id_from_nid('project', $node->nid);
  363. // if the nid does not have a matching record then skip this node.
  364. // this can happen with orphaned nodes.
  365. if (!$project_id) {
  366. continue;
  367. }
  368. $values = array('project_id' => $project_id);
  369. $project = chado_generate_var('project', $values);
  370. $nodes[$nid]->project = $project;
  371. // Now get the title
  372. $node->title = chado_get_node_title($node);
  373. }
  374. }
  375. /**
  376. * Implement hook_node_access().
  377. *
  378. * This hook allows node modules to limit access to the node types they define.
  379. *
  380. * @param $node
  381. * The node on which the operation is to be performed, or, if it does not yet exist, the
  382. * type of node to be created
  383. *
  384. * @param $op
  385. * The operation to be performed
  386. *
  387. *
  388. * @param $account
  389. * A user object representing the user for whom the operation is to be performed
  390. *
  391. * @return
  392. * If the permission for the specified operation is not set then return FALSE. If the
  393. * permission is set then return NULL as this allows other modules to disable
  394. * access. The only exception is when the $op == 'create'. We will always
  395. * return TRUE if the permission is set.
  396. *
  397. * @ingroup tripal_project
  398. */
  399. function chado_project_node_access($node, $op, $account) {
  400. $node_type = $node;
  401. if (is_object($node)) {
  402. $node_type = $node->type;
  403. }
  404. if($node_type == 'chado_project') {
  405. if ($op == 'create') {
  406. if (!user_access('create chado_project content', $account)) {
  407. return NODE_ACCESS_DENY;
  408. }
  409. return NODE_ACCESS_ALLOW;
  410. }
  411. if ($op == 'update') {
  412. if (!user_access('edit chado_project content', $account)) {
  413. return NODE_ACCESS_DENY;
  414. }
  415. }
  416. if ($op == 'delete') {
  417. if (!user_access('delete chado_project content', $account)) {
  418. return NODE_ACCESS_DENY;
  419. }
  420. }
  421. if ($op == 'view') {
  422. if (!user_access('access chado_project content', $account)) {
  423. return NODE_ACCESS_DENY;
  424. }
  425. }
  426. return NODE_ACCESS_IGNORE;
  427. }
  428. }
  429. /**
  430. * Implements hook_node_view().
  431. *
  432. * @ingroup tripal_project
  433. */
  434. function tripal_project_node_view($node, $view_mode, $langcode) {
  435. switch ($node->type) {
  436. case 'chado_project':
  437. // Show feature browser and counts
  438. if ($view_mode == 'full') {
  439. $node->content['tripal_project_base'] = array(
  440. '#markup' => theme('tripal_project_base', array('node' => $node)),
  441. '#tripal_toc_id' => 'base',
  442. '#tripal_toc_title' => 'Overview',
  443. '#weight' => -100,
  444. );
  445. $node->content['tripal_project_contact'] = array(
  446. '#markup' => theme('tripal_project_contact', array('node' => $node)),
  447. '#tripal_toc_id' => 'contacts',
  448. '#tripal_toc_title' => 'Contacts',
  449. );
  450. $node->content['tripal_project_properties'] = array(
  451. '#markup' => theme('tripal_project_properties', array('node' => $node)),
  452. '#tripal_toc_id' => 'properties',
  453. '#tripal_toc_title' => 'Properties',
  454. );
  455. $node->content['tripal_project_publications'] = array(
  456. '#markup' => theme('tripal_project_publications', array('node' => $node)),
  457. '#tripal_toc_id' => 'publications',
  458. '#tripal_toc_title' => 'Publications',
  459. );
  460. $node->content['tripal_project_relationships'] = array(
  461. '#markup' => theme('tripal_project_relationships', array('node' => $node)),
  462. '#tripal_toc_id' => 'relationships',
  463. '#tripal_toc_title' => 'Relationships',
  464. );
  465. }
  466. if ($view_mode == 'teaser') {
  467. $node->content['tripal_project_teaser'] = array(
  468. '#markup' => theme('tripal_project_teaser', array('node' => $node)),
  469. );
  470. }
  471. break;
  472. }
  473. }
  474. /**
  475. * Implements hook_node_insert().
  476. * Acts on all content types.
  477. *
  478. * @ingroup tripal_project
  479. */
  480. function tripal_project_node_insert($node) {
  481. // set the URL path after inserting. We do it here because we do not
  482. // know the project_id in the presave
  483. switch ($node->type) {
  484. case 'chado_project':
  485. // get the feature details from chado
  486. $project_id = chado_get_id_from_nid('project', $node->nid);
  487. $values = array('project_id' => $project_id);
  488. $project = chado_generate_var('project', $values);
  489. $nodes->project = $project;
  490. // Now get the title
  491. $node->title = chado_get_node_title($node);
  492. // on an insert we need to add the project_id to the node object
  493. // so that the tripal_project_get_project_url function can set the URL properly
  494. $node->project_id = $project_id;
  495. // remove any previous alias
  496. db_query("DELETE FROM {url_alias} WHERE source = :source", array(':source' => "node/$node->nid"));
  497. // set the URL for this project page
  498. $url_alias = tripal_project_get_project_url($node);
  499. $path_alias = array("source" => "node/$node->nid", "alias" => $url_alias);
  500. path_save($path_alias);
  501. break;
  502. }
  503. }
  504. /**
  505. * Implements hook_node_update().
  506. * Acts on all content types.
  507. *
  508. * @ingroup tripal_project
  509. */
  510. function tripal_project_node_update($node) {
  511. // add items to other nodes, build index and search results
  512. switch ($node->type) {
  513. case 'chado_project':
  514. // get the feature details from chado
  515. $project_id = chado_get_id_from_nid('project', $node->nid);
  516. $values = array('project_id' => $project_id);
  517. $project = chado_generate_var('project', $values);
  518. $nodes->project = $project;
  519. // Now get the title
  520. $node->title = chado_get_node_title($node);
  521. // remove any previous alias
  522. db_query("DELETE FROM {url_alias} WHERE source = :source", array(':source' => "node/$node->nid"));
  523. // set the URL for this project page
  524. $url_alias = tripal_project_get_project_url($node);
  525. $path_alias = array("source" => "node/$node->nid", "alias" => $url_alias);
  526. path_save($path_alias);
  527. break;
  528. }
  529. }
  530. /**
  531. * Return the url alias for a project
  532. *
  533. * @param $node
  534. * A node object containing at least the project_id and nid
  535. * @param $url_alias
  536. * Optional. This should be the URL alias syntax string that contains
  537. * placeholders such as [id] and [name]. These placeholders will be substituted for actual values.
  538. * If this parameter is not provided then the value of the
  539. * chado_project_url_string Drupal variable will be used.
  540. *
  541. * @ingroup tripal_project
  542. */
  543. function tripal_project_get_project_url($node, $url_alias = NULL) {
  544. $length_project_name = 100;
  545. // get the starting URL alias
  546. if(!$url_alias) {
  547. $url_alias = variable_get('chado_project_url_string', '/project/[id]');
  548. if (!$url_alias) {
  549. $url_alias = '/project/[id]';
  550. }
  551. $url_alias = preg_replace('/^\//', '', $url_alias); // remove any preceeding forward slash
  552. }
  553. // get the project
  554. $values = array('project_id' => $node->project_id);
  555. $project = chado_select_record('project', array('*'), $values);
  556. if (!$project) {
  557. tripal_report_error('trp-seturl', TRIPAL_ERROR, "Cannot find project when setting URL alias for project: %id", array('%id' => $node->project_id));
  558. return FALSE;
  559. }
  560. $project = (object) $project[0];
  561. // Sanitize project name
  562. $project_name = str_replace(' ','-', $project->name);
  563. $project_name = str_replace(',','', $project_name);
  564. $project_name = str_replace('&','and', $project_name);
  565. $project_name = substr($project_name, 0, $length_project_name);
  566. // now substitute in the values
  567. $url_alias = preg_replace('/\[id\]/', $project->project_id, $url_alias);
  568. $url_alias = preg_replace('/\[name\]/', $project_name, $url_alias);
  569. // the dst field of the url_alias table is only 128 characters long.
  570. // if this is the case then simply return the node URL, we can't set this one
  571. if (strlen($url_alias) > 128) {
  572. tripal_report_error('trp-seturl', TRIPAL_ERROR, "Cannot set alias longer than 128 characters: %alias.", array('%alias' => $url_alias));
  573. return "node/" . $node->nid;
  574. }
  575. return $url_alias;
  576. }
  577. /**
  578. * Resets all of the URL alias for all projects. This function is meant to
  579. * be run using Tripal's job managmenet interface
  580. *
  581. * @param $na
  582. * Tripal expects all jobs to have at least one argument. For this function
  583. * we don't need any, so we have this dummy argument as a filler
  584. * @param $job_id
  585. *
  586. * @ingroup tripal_project
  587. */
  588. function tripal_project_set_urls($na = NULL, $job = NULL) {
  589. $transaction = db_transaction();
  590. print "\nNOTE: Setting of URLs is performed using a database transaction. \n" .
  591. "If the load fails or is terminated prematurely then the entire set of \n" .
  592. "new URLs will be rolled back and no changes will be made\n\n";
  593. try {
  594. // get the number of records we need to set URLs for
  595. $csql = "SELECT count(*) FROM {chado_project}";
  596. $num_nodes = db_query($csql)->fetchField();
  597. // calculate the interval at which we will print an update on the screen
  598. $num_set = 0;
  599. $num_per_interval = 100;
  600. // prepare the statements which will quickly add url alias. Because these
  601. // are not Chado tables we must manually prepare them
  602. $dsql = "DELETE FROM {url_alias} WHERE source = :source";
  603. $isql = "INSERT INTO url_alias (source, alias, language) VALUES (:source, :alias, :language)";
  604. // get the URL alias syntax string
  605. $url_alias = variable_get('chado_project_url_string', '/project/[id]');
  606. $url_alias = preg_replace('/^\//', '', $url_alias); // remove any preceeding forward slash
  607. // get the list of projects that have been synced
  608. $sql = "SELECT * FROM {chado_project}";
  609. $nodes = db_query($sql);
  610. foreach ($nodes as $node) {
  611. // get the URL alias
  612. $src = "node/$node->nid";
  613. $dst = tripal_project_get_project_url($node, $url_alias);
  614. // if the src and dst is the same (the URL alias couldn't be set)
  615. // then skip to the next one. There's nothing we can do about this one.
  616. if($src == $dst) {
  617. continue;
  618. }
  619. // remove any previous alias and then add the new one
  620. db_query($dsql, array(':source' => $src));
  621. db_query($isql, array(':source' => $src, ':alias' => $dst, ':language' => LANGUAGE_NONE));
  622. // update the job status every 1% projects
  623. if ($job and $num_set % $num_per_interval == 0) {
  624. $percent = ($num_set / $num_nodes) * 100;
  625. tripal_set_job_progress($job, intval($percent));
  626. $percent = sprintf("%.2f", $percent);
  627. print "Setting URLs (" . $percent . "%). Memory: " . number_format(memory_get_usage()) . " bytes.\r";
  628. }
  629. $num_set++;
  630. }
  631. $percent = ($num_set / $num_nodes) * 100;
  632. tripal_set_job_progress($job, intval($percent));
  633. $percent = sprintf("%.2f", $percent);
  634. print "Setting URLs (" . $percent . "%). Memory: " . number_format(memory_get_usage()) . " bytes.\r";
  635. print "\nDone. Set " . number_format($num_set) . " URLs\n";
  636. }
  637. catch (Exception $e) {
  638. $transaction->rollback();
  639. print "\n"; // make sure we start errors on new line
  640. watchdog_exception('tripal_project', $e);
  641. watchdog('trp-seturl', "Failed Removing URL Alias: %src", array('%src' => $src), WATCHDOG_ERROR);
  642. }
  643. }
  644. /**
  645. * Implements [content_type]_chado_node_default_title_format().
  646. *
  647. * Defines a default title format for the Chado Node API to set the titles on
  648. * Chado project nodes based on chado fields.
  649. */
  650. function chado_project_chado_node_default_title_format() {
  651. return '[project.name]';
  652. }