tripal_project.chado_node.inc 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  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. // Now get the title
  361. $node->title = chado_get_node_title($node);
  362. }
  363. }
  364. /**
  365. * Implement hook_node_access().
  366. *
  367. * This hook allows node modules to limit access to the node types they define.
  368. *
  369. * @param $node
  370. * The node on which the operation is to be performed, or, if it does not yet exist, the
  371. * type of node to be created
  372. *
  373. * @param $op
  374. * The operation to be performed
  375. *
  376. *
  377. * @param $account
  378. * A user object representing the user for whom the operation is to be performed
  379. *
  380. * @return
  381. * If the permission for the specified operation is not set then return FALSE. If the
  382. * permission is set then return NULL as this allows other modules to disable
  383. * access. The only exception is when the $op == 'create'. We will always
  384. * return TRUE if the permission is set.
  385. *
  386. * @ingroup tripal_project
  387. */
  388. function chado_project_node_access($node, $op, $account) {
  389. $node_type = $node;
  390. if (is_object($node)) {
  391. $node_type = $node->type;
  392. }
  393. if($node_type == 'chado_project') {
  394. if ($op == 'create') {
  395. if (!user_access('create chado_project content', $account)) {
  396. return NODE_ACCESS_DENY;
  397. }
  398. return NODE_ACCESS_ALLOW;
  399. }
  400. if ($op == 'update') {
  401. if (!user_access('edit chado_project content', $account)) {
  402. return NODE_ACCESS_DENY;
  403. }
  404. }
  405. if ($op == 'delete') {
  406. if (!user_access('delete chado_project content', $account)) {
  407. return NODE_ACCESS_DENY;
  408. }
  409. }
  410. if ($op == 'view') {
  411. if (!user_access('access chado_project content', $account)) {
  412. return NODE_ACCESS_DENY;
  413. }
  414. }
  415. return NODE_ACCESS_IGNORE;
  416. }
  417. }
  418. /**
  419. * Implements hook_node_view().
  420. *
  421. * @ingroup tripal_project
  422. */
  423. function tripal_project_node_view($node, $view_mode, $langcode) {
  424. switch ($node->type) {
  425. case 'chado_project':
  426. // Show feature browser and counts
  427. if ($view_mode == 'full') {
  428. $node->content['tripal_project_base'] = array(
  429. '#markup' => theme('tripal_project_base', array('node' => $node)),
  430. '#tripal_toc_id' => 'base',
  431. '#tripal_toc_title' => 'Overview',
  432. '#weight' => -100,
  433. );
  434. $node->content['tripal_project_contact'] = array(
  435. '#markup' => theme('tripal_project_contact', array('node' => $node)),
  436. '#tripal_toc_id' => 'contacts',
  437. '#tripal_toc_title' => 'Contacts',
  438. );
  439. $node->content['tripal_project_properties'] = array(
  440. '#markup' => theme('tripal_project_properties', array('node' => $node)),
  441. '#tripal_toc_id' => 'properties',
  442. '#tripal_toc_title' => 'Properties',
  443. );
  444. $node->content['tripal_project_publications'] = array(
  445. '#markup' => theme('tripal_project_publications', array('node' => $node)),
  446. '#tripal_toc_id' => 'publications',
  447. '#tripal_toc_title' => 'Publications',
  448. );
  449. $node->content['tripal_project_relationships'] = array(
  450. '#markup' => theme('tripal_project_relationships', array('node' => $node)),
  451. '#tripal_toc_id' => 'relationships',
  452. '#tripal_toc_title' => 'Relationships',
  453. );
  454. }
  455. if ($view_mode == 'teaser') {
  456. $node->content['tripal_project_teaser'] = array(
  457. '#markup' => theme('tripal_project_teaser', array('node' => $node)),
  458. );
  459. }
  460. break;
  461. }
  462. }
  463. /**
  464. * Implements hook_node_insert().
  465. * Acts on all content types.
  466. *
  467. * @ingroup tripal_project
  468. */
  469. function tripal_project_node_insert($node) {
  470. // set the URL path after inserting. We do it here because we do not
  471. // know the project_id in the presave
  472. switch ($node->type) {
  473. case 'chado_project':
  474. // get the feature details from chado
  475. $project_id = chado_get_id_from_nid('project', $node->nid);
  476. $values = array('project_id' => $project_id);
  477. $project = chado_generate_var('project', $values);
  478. $nodes->project = $project;
  479. // Now get the title
  480. $node->title = chado_get_node_title($node);
  481. // on an insert we need to add the project_id to the node object
  482. // so that the tripal_project_get_project_url function can set the URL properly
  483. $node->project_id = $project_id;
  484. // remove any previous alias
  485. db_query("DELETE FROM {url_alias} WHERE source = :source", array(':source' => "node/$node->nid"));
  486. // set the URL for this project page
  487. $url_alias = tripal_project_get_project_url($node);
  488. $path_alias = array("source" => "node/$node->nid", "alias" => $url_alias);
  489. path_save($path_alias);
  490. break;
  491. }
  492. }
  493. /**
  494. * Implements hook_node_update().
  495. * Acts on all content types.
  496. *
  497. * @ingroup tripal_project
  498. */
  499. function tripal_project_node_update($node) {
  500. // add items to other nodes, build index and search results
  501. switch ($node->type) {
  502. case 'chado_project':
  503. // get the feature details from chado
  504. $project_id = chado_get_id_from_nid('project', $node->nid);
  505. $values = array('project_id' => $project_id);
  506. $project = chado_generate_var('project', $values);
  507. $nodes->project = $project;
  508. // Now get the title
  509. $node->title = chado_get_node_title($node);
  510. // remove any previous alias
  511. db_query("DELETE FROM {url_alias} WHERE source = :source", array(':source' => "node/$node->nid"));
  512. // set the URL for this project page
  513. $url_alias = tripal_project_get_project_url($node);
  514. $path_alias = array("source" => "node/$node->nid", "alias" => $url_alias);
  515. path_save($path_alias);
  516. break;
  517. }
  518. }
  519. /**
  520. * Return the url alias for a project
  521. *
  522. * @param $node
  523. * A node object containing at least the project_id and nid
  524. * @param $url_alias
  525. * Optional. This should be the URL alias syntax string that contains
  526. * placeholders such as [id] and [name]. These placeholders will be substituted for actual values.
  527. * If this parameter is not provided then the value of the
  528. * chado_project_url_string Drupal variable will be used.
  529. *
  530. * @ingroup tripal_project
  531. */
  532. function tripal_project_get_project_url($node, $url_alias = NULL) {
  533. $length_project_name = 100;
  534. // get the starting URL alias
  535. if(!$url_alias) {
  536. $url_alias = variable_get('chado_project_url_string', '/project/[id]');
  537. if (!$url_alias) {
  538. $url_alias = '/project/[id]';
  539. }
  540. $url_alias = preg_replace('/^\//', '', $url_alias); // remove any preceeding forward slash
  541. }
  542. // get the project
  543. $values = array('project_id' => $node->project_id);
  544. $project = chado_select_record('project', array('*'), $values);
  545. if (!$project) {
  546. tripal_report_error('trp-seturl', TRIPAL_ERROR, "Cannot find project when setting URL alias for project: %id", array('%id' => $node->project_id));
  547. return FALSE;
  548. }
  549. $project = (object) $project[0];
  550. // Sanitize project name
  551. $project_name = str_replace(' ','-', $project->name);
  552. $project_name = str_replace(',','', $project_name);
  553. $project_name = str_replace('&','and', $project_name);
  554. $project_name = substr($project_name, 0, $length_project_name);
  555. // now substitute in the values
  556. $url_alias = preg_replace('/\[id\]/', $project->project_id, $url_alias);
  557. $url_alias = preg_replace('/\[name\]/', $project_name, $url_alias);
  558. // the dst field of the url_alias table is only 128 characters long.
  559. // if this is the case then simply return the node URL, we can't set this one
  560. if (strlen($url_alias) > 128) {
  561. tripal_report_error('trp-seturl', TRIPAL_ERROR, "Cannot set alias longer than 128 characters: %alias.", array('%alias' => $url_alias));
  562. return "node/" . $node->nid;
  563. }
  564. return $url_alias;
  565. }
  566. /**
  567. * Resets all of the URL alias for all projects. This function is meant to
  568. * be run using Tripal's job managmenet interface
  569. *
  570. * @param $na
  571. * Tripal expects all jobs to have at least one argument. For this function
  572. * we don't need any, so we have this dummy argument as a filler
  573. * @param $job_id
  574. *
  575. * @ingroup tripal_project
  576. */
  577. function tripal_project_set_urls($na = NULL, $job = NULL) {
  578. $transaction = db_transaction();
  579. print "\nNOTE: Setting of URLs is performed using a database transaction. \n" .
  580. "If the load fails or is terminated prematurely then the entire set of \n" .
  581. "new URLs will be rolled back and no changes will be made\n\n";
  582. try {
  583. // get the number of records we need to set URLs for
  584. $csql = "SELECT count(*) FROM {chado_project}";
  585. $num_nodes = db_query($csql)->fetchField();
  586. // calculate the interval at which we will print an update on the screen
  587. $num_set = 0;
  588. $num_per_interval = 100;
  589. // prepare the statements which will quickly add url alias. Because these
  590. // are not Chado tables we must manually prepare them
  591. $dsql = "DELETE FROM {url_alias} WHERE source = :source";
  592. $isql = "INSERT INTO url_alias (source, alias, language) VALUES (:source, :alias, :language)";
  593. // get the URL alias syntax string
  594. $url_alias = variable_get('chado_project_url_string', '/project/[id]');
  595. $url_alias = preg_replace('/^\//', '', $url_alias); // remove any preceeding forward slash
  596. // get the list of projects that have been synced
  597. $sql = "SELECT * FROM {chado_project}";
  598. $nodes = db_query($sql);
  599. foreach ($nodes as $node) {
  600. // get the URL alias
  601. $src = "node/$node->nid";
  602. $dst = tripal_project_get_project_url($node, $url_alias);
  603. // if the src and dst is the same (the URL alias couldn't be set)
  604. // then skip to the next one. There's nothing we can do about this one.
  605. if($src == $dst) {
  606. continue;
  607. }
  608. // remove any previous alias and then add the new one
  609. db_query($dsql, array(':source' => $src));
  610. db_query($isql, array(':source' => $src, ':alias' => $dst, ':language' => LANGUAGE_NONE));
  611. // update the job status every 1% projects
  612. if ($job and $num_set % $num_per_interval == 0) {
  613. $percent = ($num_set / $num_nodes) * 100;
  614. tripal_set_job_progress($job, intval($percent));
  615. $percent = sprintf("%.2f", $percent);
  616. print "Setting URLs (" . $percent . "%). Memory: " . number_format(memory_get_usage()) . " bytes.\r";
  617. }
  618. $num_set++;
  619. }
  620. $percent = ($num_set / $num_nodes) * 100;
  621. tripal_set_job_progress($job, intval($percent));
  622. $percent = sprintf("%.2f", $percent);
  623. print "Setting URLs (" . $percent . "%). Memory: " . number_format(memory_get_usage()) . " bytes.\r";
  624. print "\nDone. Set " . number_format($num_set) . " URLs\n";
  625. }
  626. catch (Exception $e) {
  627. $transaction->rollback();
  628. print "\n"; // make sure we start errors on new line
  629. watchdog_exception('tripal_project', $e);
  630. watchdog('trp-seturl', "Failed Removing URL Alias: %src", array('%src' => $src), WATCHDOG_ERROR);
  631. }
  632. }
  633. /**
  634. * Implements [content_type]_chado_node_default_title_format().
  635. *
  636. * Defines a default title format for the Chado Node API to set the titles on
  637. * Chado project nodes based on chado fields.
  638. */
  639. function chado_project_chado_node_default_title_format() {
  640. return '[project.name]';
  641. }