tripal_project.chado_node.inc 24 KB

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