tripal_project.chado_node.inc 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733
  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_get_cvterm_select_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_get_cvterm(array(
  239. 'name' => 'Project Description',
  240. 'cv_id' => array('name' => 'project_property')
  241. ));
  242. $properties[$descrip_id->cvterm_id][0] = $node->description;
  243. $details = array(
  244. 'property_table' => 'projectprop',
  245. 'base_table' => 'project',
  246. 'foreignkey_name' => 'project_id',
  247. 'foreignkey_value' => $project_id
  248. );
  249. chado_update_node_form_properties($node, $details, $properties);
  250. // * Relationships Form *
  251. $details = array(
  252. 'relationship_table' => 'project_relationship', // name of the _relationship table
  253. 'foreignkey_value' => $project_id // value of the example_id key
  254. );
  255. chado_update_node_form_relationships($node, $details);
  256. }
  257. else {
  258. $project_id = $node->project_id;
  259. }
  260. // Make sure the entry for this project doesn't already exist in the
  261. // chado_project table if it doesn't exist then we want to add it.
  262. $check_org_id = chado_get_id_from_nid('project', $node->nid);
  263. if (!$check_org_id) {
  264. $record = new stdClass();
  265. $record->nid = $node->nid;
  266. $record->vid = $node->vid;
  267. $record->project_id = $project_id;
  268. drupal_write_record('chado_project', $record);
  269. }
  270. }
  271. /**
  272. * Implementation of hook_delete().
  273. *
  274. * @param $node
  275. * The node which is to be deleted, only chado project and chado_project need to be dealt with
  276. * since the drupal node is deleted automagically
  277. *
  278. * @ingroup tripal_project
  279. */
  280. function chado_project_delete($node) {
  281. $project_id = chado_get_id_from_nid('project', $node->nid);
  282. // if we don't have a project id for this node then this isn't a node of
  283. // type chado_project or the entry in the chado_project table was lost.
  284. if (!$project_id) {
  285. return;
  286. }
  287. // Remove data from {chado_project}, {node} and {node_revisions} tables of
  288. // drupal database
  289. $sql_del = "DELETE FROM {chado_project} WHERE nid = :nid AND vid = :vid";
  290. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  291. $sql_del = "DELETE FROM {node_revision} WHERE nid = :nid AND vid = :vid";
  292. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  293. $sql_del = "DELETE FROM {node} WHERE nid = :nid AND vid = :vid";
  294. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  295. // Remove data from project and projectprop tables of chado database as well
  296. chado_query("DELETE FROM {projectprop} WHERE project_id = :project_id", array(':project_id' => $project_id));
  297. chado_query("DELETE FROM {project} WHERE project_id = :project_id", array(':project_id' => $project_id));
  298. }
  299. /**
  300. * Implements hook_update().
  301. *
  302. * @param $node
  303. * The node which is to have its containing information updated when the user modifies information
  304. * pertaining to the specific project
  305. *
  306. * @ingroup tripal_project
  307. */
  308. function chado_project_update($node) {
  309. $node->title = trim($node->title);
  310. $node->description = trim($node->description);
  311. // update the project and the description
  312. $project_id = chado_get_id_from_nid('project', $node->nid) ;
  313. $match = array('project_id' => $project_id);
  314. $values = array(
  315. 'name' => $node->title,
  316. 'description' => '',
  317. );
  318. $status = chado_update_record('project', $match, $values);
  319. if (!$status) {
  320. drupal_set_message(t('Unable to update project.', 'warning'));
  321. watchdog('tripal_project', 'Update project: Unable to update project where values: %values',
  322. array('%values' => print_r($values, TRUE)), WATCHDOG_ERROR);
  323. }
  324. // * Properties Form *
  325. // Add the description property
  326. $properties = chado_retrieve_node_form_properties($node);
  327. $descrip_id = tripal_get_cvterm(array(
  328. 'name' => 'Project Description',
  329. 'cv_id' => array('name' => 'project_property')
  330. ));
  331. $properties[$descrip_id->cvterm_id][0] = $node->description;
  332. $details = array(
  333. 'property_table' => 'projectprop',
  334. 'base_table' => 'project',
  335. 'foreignkey_name' => 'project_id',
  336. 'foreignkey_value' => $project_id
  337. );
  338. chado_update_node_form_properties($node, $details, $properties);
  339. // * Relationships Form *
  340. $details = array(
  341. 'relationship_table' => 'project_relationship', // name of the _relationship table
  342. 'foreignkey_value' => $project_id // value of the example_id key
  343. );
  344. chado_update_node_form_relationships($node, $details);
  345. }
  346. /**
  347. * Implementation of hook_load().
  348. *
  349. * @param $node
  350. * The node that is to have its containing information loaded
  351. *
  352. * @ingroup tripal_project
  353. */
  354. function chado_project_load($nodes) {
  355. foreach ($nodes as $nid => $node) {
  356. // get the feature details from chado
  357. $project_id = chado_get_id_from_nid('project', $node->nid);
  358. // if the nid does not have a matching record then skip this node.
  359. // this can happen with orphaned nodes.
  360. if (!$project_id) {
  361. continue;
  362. }
  363. $values = array('project_id' => $project_id);
  364. $project = chado_generate_var('project', $values);
  365. $nodes[$nid]->project = $project;
  366. // Now get the title
  367. $node->title = chado_get_node_title($node);
  368. }
  369. }
  370. /**
  371. * Implement hook_node_access().
  372. *
  373. * This hook allows node modules to limit access to the node types they define.
  374. *
  375. * @param $node
  376. * The node on which the operation is to be performed, or, if it does not yet exist, the
  377. * type of node to be created
  378. *
  379. * @param $op
  380. * The operation to be performed
  381. *
  382. *
  383. * @param $account
  384. * A user object representing the user for whom the operation is to be performed
  385. *
  386. * @return
  387. * If the permission for the specified operation is not set then return FALSE. If the
  388. * permission is set then return NULL as this allows other modules to disable
  389. * access. The only exception is when the $op == 'create'. We will always
  390. * return TRUE if the permission is set.
  391. *
  392. * @ingroup tripal_project
  393. */
  394. function chado_project_node_access($node, $op, $account) {
  395. $node_type = $node;
  396. if (is_object($node)) {
  397. $node_type = $node->type;
  398. }
  399. if($node_type == 'chado_project') {
  400. if ($op == 'create') {
  401. if (!user_access('create chado_project content', $account)) {
  402. return NODE_ACCESS_DENY;
  403. }
  404. return NODE_ACCESS_ALLOW;
  405. }
  406. if ($op == 'update') {
  407. if (!user_access('edit chado_project content', $account)) {
  408. return NODE_ACCESS_DENY;
  409. }
  410. }
  411. if ($op == 'delete') {
  412. if (!user_access('delete chado_project content', $account)) {
  413. return NODE_ACCESS_DENY;
  414. }
  415. }
  416. if ($op == 'view') {
  417. if (!user_access('access chado_project content', $account)) {
  418. return NODE_ACCESS_DENY;
  419. }
  420. }
  421. return NODE_ACCESS_IGNORE;
  422. }
  423. }
  424. /**
  425. * Implements hook_node_view().
  426. *
  427. * @ingroup tripal_project
  428. */
  429. function tripal_project_node_view($node, $view_mode, $langcode) {
  430. switch ($node->type) {
  431. case 'chado_project':
  432. // Show feature browser and counts
  433. if ($view_mode == 'full') {
  434. $node->content['tripal_project_base'] = array(
  435. '#markup' => theme('tripal_project_base', array('node' => $node)),
  436. '#tripal_toc_id' => 'base',
  437. '#tripal_toc_title' => 'Overview',
  438. '#weight' => -100,
  439. );
  440. $node->content['tripal_project_contact'] = array(
  441. '#markup' => theme('tripal_project_contact', array('node' => $node)),
  442. '#tripal_toc_id' => 'contacts',
  443. '#tripal_toc_title' => 'Contacts',
  444. );
  445. $node->content['tripal_project_properties'] = array(
  446. '#markup' => theme('tripal_project_properties', array('node' => $node)),
  447. '#tripal_toc_id' => 'properties',
  448. '#tripal_toc_title' => 'Properties',
  449. );
  450. $node->content['tripal_project_publications'] = array(
  451. '#markup' => theme('tripal_project_publications', array('node' => $node)),
  452. '#tripal_toc_id' => 'publications',
  453. '#tripal_toc_title' => 'Publications',
  454. );
  455. $node->content['tripal_project_relationships'] = array(
  456. '#markup' => theme('tripal_project_relationships', array('node' => $node)),
  457. '#tripal_toc_id' => 'relationships',
  458. '#tripal_toc_title' => 'Relationships',
  459. );
  460. }
  461. if ($view_mode == 'teaser') {
  462. $node->content['tripal_project_teaser'] = array(
  463. '#markup' => theme('tripal_project_teaser', array('node' => $node)),
  464. );
  465. }
  466. break;
  467. }
  468. }
  469. /**
  470. * Implements hook_node_insert().
  471. * Acts on all content types.
  472. *
  473. * @ingroup tripal_project
  474. */
  475. function tripal_project_node_insert($node) {
  476. // set the URL path after inserting. We do it here because we do not
  477. // know the project_id in the presave
  478. switch ($node->type) {
  479. case 'chado_project':
  480. // get the feature details from chado
  481. $project_id = chado_get_id_from_nid('project', $node->nid);
  482. $values = array('project_id' => $project_id);
  483. $project = chado_generate_var('project', $values);
  484. $nodes->project = $project;
  485. // Now get the title
  486. $node->title = chado_get_node_title($node);
  487. // on an insert we need to add the project_id to the node object
  488. // so that the tripal_project_get_project_url function can set the URL properly
  489. $node->project_id = $project_id;
  490. // remove any previous alias
  491. db_query("DELETE FROM {url_alias} WHERE source = :source", array(':source' => "node/$node->nid"));
  492. // set the URL for this project page
  493. $url_alias = tripal_project_get_project_url($node);
  494. $path_alias = array("source" => "node/$node->nid", "alias" => $url_alias);
  495. path_save($path_alias);
  496. break;
  497. }
  498. }
  499. /**
  500. * Implements hook_node_update().
  501. * Acts on all content types.
  502. *
  503. * @ingroup tripal_project
  504. */
  505. function tripal_project_node_update($node) {
  506. // add items to other nodes, build index and search results
  507. switch ($node->type) {
  508. case 'chado_project':
  509. // get the feature details from chado
  510. $project_id = chado_get_id_from_nid('project', $node->nid);
  511. $values = array('project_id' => $project_id);
  512. $project = chado_generate_var('project', $values);
  513. $nodes->project = $project;
  514. // Now get the title
  515. $node->title = chado_get_node_title($node);
  516. // remove any previous alias
  517. db_query("DELETE FROM {url_alias} WHERE source = :source", array(':source' => "node/$node->nid"));
  518. // set the URL for this project page
  519. $url_alias = tripal_project_get_project_url($node);
  520. $path_alias = array("source" => "node/$node->nid", "alias" => $url_alias);
  521. path_save($path_alias);
  522. break;
  523. }
  524. }
  525. /**
  526. * Return the url alias for a project
  527. *
  528. * @param $node
  529. * A node object containing at least the project_id and nid
  530. * @param $url_alias
  531. * Optional. This should be the URL alias syntax string that contains
  532. * placeholders such as [id] and [name]. These placeholders will be substituted for actual values.
  533. * If this parameter is not provided then the value of the
  534. * chado_project_url_string Drupal variable will be used.
  535. *
  536. * @ingroup tripal_project
  537. */
  538. function tripal_project_get_project_url($node, $url_alias = NULL) {
  539. $length_project_name = 100;
  540. // get the starting URL alias
  541. if(!$url_alias) {
  542. $url_alias = variable_get('chado_project_url_string', '/project/[id]');
  543. if (!$url_alias) {
  544. $url_alias = '/project/[id]';
  545. }
  546. $url_alias = preg_replace('/^\//', '', $url_alias); // remove any preceeding forward slash
  547. }
  548. // get the project
  549. $values = array('project_id' => $node->project_id);
  550. $project = chado_select_record('project', array('*'), $values);
  551. if (!$project) {
  552. tripal_report_error('trp-seturl', TRIPAL_ERROR, "Cannot find project when setting URL alias for project: %id", array('%id' => $node->project_id));
  553. return FALSE;
  554. }
  555. $project = (object) $project[0];
  556. // Sanitize project name
  557. $project_name = str_replace(' ','-', $project->name);
  558. $project_name = str_replace(',','', $project_name);
  559. $project_name = str_replace('&','and', $project_name);
  560. $project_name = substr($project_name, 0, $length_project_name);
  561. // now substitute in the values
  562. $url_alias = preg_replace('/\[id\]/', $project->project_id, $url_alias);
  563. $url_alias = preg_replace('/\[name\]/', $project_name, $url_alias);
  564. // the dst field of the url_alias table is only 128 characters long.
  565. // if this is the case then simply return the node URL, we can't set this one
  566. if (strlen($url_alias) > 128) {
  567. tripal_report_error('trp-seturl', TRIPAL_ERROR, "Cannot set alias longer than 128 characters: %alias.", array('%alias' => $url_alias));
  568. return "node/" . $node->nid;
  569. }
  570. return $url_alias;
  571. }
  572. /**
  573. * Resets all of the URL alias for all projects. This function is meant to
  574. * be run using Tripal's job managmenet interface
  575. *
  576. * @param $na
  577. * Tripal expects all jobs to have at least one argument. For this function
  578. * we don't need any, so we have this dummy argument as a filler
  579. * @param $job_id
  580. *
  581. * @ingroup tripal_project
  582. */
  583. function tripal_project_set_urls($na = NULL, $job = NULL) {
  584. $transaction = db_transaction();
  585. print "\nNOTE: Setting of URLs is performed using a database transaction. \n" .
  586. "If the load fails or is terminated prematurely then the entire set of \n" .
  587. "new URLs will be rolled back and no changes will be made\n\n";
  588. try {
  589. // get the number of records we need to set URLs for
  590. $csql = "SELECT count(*) FROM {chado_project}";
  591. $num_nodes = db_query($csql)->fetchField();
  592. // calculate the interval at which we will print an update on the screen
  593. $num_set = 0;
  594. $num_per_interval = 100;
  595. // prepare the statements which will quickly add url alias. Because these
  596. // are not Chado tables we must manually prepare them
  597. $dsql = "DELETE FROM {url_alias} WHERE source = :source";
  598. $isql = "INSERT INTO url_alias (source, alias, language) VALUES (:source, :alias, :language)";
  599. // get the URL alias syntax string
  600. $url_alias = variable_get('chado_project_url_string', '/project/[id]');
  601. $url_alias = preg_replace('/^\//', '', $url_alias); // remove any preceeding forward slash
  602. // get the list of projects that have been synced
  603. $sql = "SELECT * FROM {chado_project}";
  604. $nodes = db_query($sql);
  605. foreach ($nodes as $node) {
  606. // get the URL alias
  607. $src = "node/$node->nid";
  608. $dst = tripal_project_get_project_url($node, $url_alias);
  609. // if the src and dst is the same (the URL alias couldn't be set)
  610. // then skip to the next one. There's nothing we can do about this one.
  611. if($src == $dst) {
  612. continue;
  613. }
  614. // remove any previous alias and then add the new one
  615. db_query($dsql, array(':source' => $src));
  616. db_query($isql, array(':source' => $src, ':alias' => $dst, ':language' => LANGUAGE_NONE));
  617. // update the job status every 1% projects
  618. if ($job and $num_set % $num_per_interval == 0) {
  619. $percent = ($num_set / $num_nodes) * 100;
  620. tripal_set_job_progress($job, intval($percent));
  621. $percent = sprintf("%.2f", $percent);
  622. print "Setting URLs (" . $percent . "%). Memory: " . number_format(memory_get_usage()) . " bytes.\r";
  623. }
  624. $num_set++;
  625. }
  626. $percent = ($num_set / $num_nodes) * 100;
  627. tripal_set_job_progress($job, intval($percent));
  628. $percent = sprintf("%.2f", $percent);
  629. print "Setting URLs (" . $percent . "%). Memory: " . number_format(memory_get_usage()) . " bytes.\r";
  630. print "\nDone. Set " . number_format($num_set) . " URLs\n";
  631. }
  632. catch (Exception $e) {
  633. $transaction->rollback();
  634. print "\n"; // make sure we start errors on new line
  635. watchdog_exception('tripal_project', $e);
  636. watchdog('trp-seturl', "Failed Removing URL Alias: %src", array('%src' => $src), WATCHDOG_ERROR);
  637. }
  638. }
  639. /**
  640. * Implements [content_type]_chado_node_default_title_format().
  641. *
  642. * Defines a default title format for the Chado Node API to set the titles on
  643. * Chado project nodes based on chado fields.
  644. */
  645. function chado_project_chado_node_default_title_format() {
  646. return '[project.name]';
  647. }