|
@@ -536,26 +536,18 @@ function tripal_project_node_insert($node) {
|
|
|
switch ($node->type) {
|
|
|
case 'chado_project':
|
|
|
|
|
|
- // get the feature details from chado
|
|
|
+ // We still don't have a fully loaded node object in this hook. Therefore,
|
|
|
+ // we need to simulate one so that the right values are available for
|
|
|
+ // the URL to be determined.
|
|
|
$project_id = chado_get_id_from_nid('project', $node->nid);
|
|
|
- $values = array('project_id' => $project_id);
|
|
|
- $project = chado_generate_var('project', $values);
|
|
|
- $nodes->project = $project;
|
|
|
+ $node->project = chado_generate_var('project', array('project_id' => $project_id));
|
|
|
|
|
|
- // Now get the title
|
|
|
+ // Now get the title.
|
|
|
$node->title = chado_get_node_title($node);
|
|
|
|
|
|
- // on an insert we need to add the project_id to the node object
|
|
|
- // so that the tripal_project_get_project_url function can set the URL properly
|
|
|
- $node->project_id = $project_id;
|
|
|
-
|
|
|
- // remove any previous alias
|
|
|
- db_query("DELETE FROM {url_alias} WHERE source = :source", array(':source' => "node/$node->nid"));
|
|
|
+ // Now use the API to set the path.
|
|
|
+ chado_set_node_url($node);
|
|
|
|
|
|
- // set the URL for this project page
|
|
|
- $url_alias = tripal_project_get_project_url($node);
|
|
|
- $path_alias = array("source" => "node/$node->nid", "alias" => $url_alias);
|
|
|
- path_save($path_alias);
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
@@ -572,161 +564,16 @@ function tripal_project_node_update($node) {
|
|
|
switch ($node->type) {
|
|
|
case 'chado_project':
|
|
|
|
|
|
- // get the feature details from chado
|
|
|
- $project_id = chado_get_id_from_nid('project', $node->nid);
|
|
|
- $values = array('project_id' => $project_id);
|
|
|
- $project = chado_generate_var('project', $values);
|
|
|
- $nodes->project = $project;
|
|
|
-
|
|
|
// Now get the title
|
|
|
$node->title = chado_get_node_title($node);
|
|
|
|
|
|
- // remove any previous alias
|
|
|
- db_query("DELETE FROM {url_alias} WHERE source = :source", array(':source' => "node/$node->nid"));
|
|
|
+ // Now use the API to set the path.
|
|
|
+ chado_set_node_url($node);
|
|
|
|
|
|
- // set the URL for this project page
|
|
|
- $url_alias = tripal_project_get_project_url($node);
|
|
|
- $path_alias = array("source" => "node/$node->nid", "alias" => $url_alias);
|
|
|
- path_save($path_alias);
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-/**
|
|
|
- * Return the url alias for a project
|
|
|
- *
|
|
|
- * @param $node
|
|
|
- * A node object containing at least the project_id and nid
|
|
|
- * @param $url_alias
|
|
|
- * Optional. This should be the URL alias syntax string that contains
|
|
|
- * placeholders such as [id] and [name]. These placeholders will be substituted for actual values.
|
|
|
- * If this parameter is not provided then the value of the
|
|
|
- * chado_project_url_string Drupal variable will be used.
|
|
|
- *
|
|
|
- * @ingroup tripal_project
|
|
|
- */
|
|
|
-function tripal_project_get_project_url($node, $url_alias = NULL) {
|
|
|
- $length_project_name = 100;
|
|
|
-
|
|
|
- // get the starting URL alias
|
|
|
- if(!$url_alias) {
|
|
|
- $url_alias = variable_get('chado_project_url_string', '/project/[id]');
|
|
|
- if (!$url_alias) {
|
|
|
- $url_alias = '/project/[id]';
|
|
|
- }
|
|
|
- $url_alias = preg_replace('/^\//', '', $url_alias); // remove any preceeding forward slash
|
|
|
- }
|
|
|
-
|
|
|
- // get the project
|
|
|
- $values = array('project_id' => $node->project_id);
|
|
|
- $project = chado_select_record('project', array('*'), $values);
|
|
|
- if (!$project) {
|
|
|
- tripal_report_error('trp-seturl', TRIPAL_ERROR, "Cannot find project when setting URL alias for project: %id", array('%id' => $node->project_id));
|
|
|
- return FALSE;
|
|
|
- }
|
|
|
- $project = (object) $project[0];
|
|
|
-
|
|
|
- // Sanitize project name
|
|
|
- $project_name = str_replace(' ','-', $project->name);
|
|
|
- $project_name = str_replace(',','', $project_name);
|
|
|
- $project_name = str_replace('&','and', $project_name);
|
|
|
- $project_name = substr($project_name, 0, $length_project_name);
|
|
|
-
|
|
|
- // now substitute in the values
|
|
|
- $url_alias = preg_replace('/\[id\]/', $project->project_id, $url_alias);
|
|
|
- $url_alias = preg_replace('/\[name\]/', $project_name, $url_alias);
|
|
|
-
|
|
|
- // the dst field of the url_alias table is only 128 characters long.
|
|
|
- // if this is the case then simply return the node URL, we can't set this one
|
|
|
- if (strlen($url_alias) > 128) {
|
|
|
- tripal_report_error('trp-seturl', TRIPAL_ERROR, "Cannot set alias longer than 128 characters: %alias.", array('%alias' => $url_alias));
|
|
|
- return "node/" . $node->nid;
|
|
|
- }
|
|
|
-
|
|
|
- return $url_alias;
|
|
|
-}
|
|
|
-
|
|
|
-/**
|
|
|
- * Resets all of the URL alias for all projects. This function is meant to
|
|
|
- * be run using Tripal's job managmenet interface
|
|
|
- *
|
|
|
- * @param $na
|
|
|
- * Tripal expects all jobs to have at least one argument. For this function
|
|
|
- * we don't need any, so we have this dummy argument as a filler
|
|
|
- * @param $job_id
|
|
|
- *
|
|
|
- * @ingroup tripal_project
|
|
|
- */
|
|
|
-function tripal_project_set_urls($na = NULL, $job = NULL) {
|
|
|
-
|
|
|
- $transaction = db_transaction();
|
|
|
-
|
|
|
- print "\nNOTE: Setting of URLs is performed using a database transaction. \n" .
|
|
|
- "If the load fails or is terminated prematurely then the entire set of \n" .
|
|
|
- "new URLs will be rolled back and no changes will be made\n\n";
|
|
|
-
|
|
|
- try {
|
|
|
- // get the number of records we need to set URLs for
|
|
|
- $csql = "SELECT count(*) FROM {chado_project}";
|
|
|
- $num_nodes = db_query($csql)->fetchField();
|
|
|
-
|
|
|
- // calculate the interval at which we will print an update on the screen
|
|
|
- $num_set = 0;
|
|
|
- $num_per_interval = 100;
|
|
|
-
|
|
|
- // prepare the statements which will quickly add url alias. Because these
|
|
|
- // are not Chado tables we must manually prepare them
|
|
|
- $dsql = "DELETE FROM {url_alias} WHERE source = :source";
|
|
|
- $isql = "INSERT INTO url_alias (source, alias, language) VALUES (:source, :alias, :language)";
|
|
|
-
|
|
|
- // get the URL alias syntax string
|
|
|
- $url_alias = variable_get('chado_project_url_string', '/project/[id]');
|
|
|
- $url_alias = preg_replace('/^\//', '', $url_alias); // remove any preceeding forward slash
|
|
|
-
|
|
|
- // get the list of projects that have been synced
|
|
|
- $sql = "SELECT * FROM {chado_project}";
|
|
|
- $nodes = db_query($sql);
|
|
|
- foreach ($nodes as $node) {
|
|
|
-
|
|
|
- // get the URL alias
|
|
|
- $src = "node/$node->nid";
|
|
|
- $dst = tripal_project_get_project_url($node, $url_alias);
|
|
|
-
|
|
|
- // if the src and dst is the same (the URL alias couldn't be set)
|
|
|
- // then skip to the next one. There's nothing we can do about this one.
|
|
|
- if($src == $dst) {
|
|
|
- continue;
|
|
|
- }
|
|
|
-
|
|
|
- // remove any previous alias and then add the new one
|
|
|
- db_query($dsql, array(':source' => $src));
|
|
|
- db_query($isql, array(':source' => $src, ':alias' => $dst, ':language' => LANGUAGE_NONE));
|
|
|
-
|
|
|
- // update the job status every 1% projects
|
|
|
- if ($job and $num_set % $num_per_interval == 0) {
|
|
|
- $percent = ($num_set / $num_nodes) * 100;
|
|
|
- tripal_set_job_progress($job, intval($percent));
|
|
|
- $percent = sprintf("%.2f", $percent);
|
|
|
- print "Setting URLs (" . $percent . "%). Memory: " . number_format(memory_get_usage()) . " bytes.\r";
|
|
|
-
|
|
|
- }
|
|
|
- $num_set++;
|
|
|
- }
|
|
|
- $percent = ($num_set / $num_nodes) * 100;
|
|
|
- tripal_set_job_progress($job, intval($percent));
|
|
|
- $percent = sprintf("%.2f", $percent);
|
|
|
- print "Setting URLs (" . $percent . "%). Memory: " . number_format(memory_get_usage()) . " bytes.\r";
|
|
|
- print "\nDone. Set " . number_format($num_set) . " URLs\n";
|
|
|
-
|
|
|
- }
|
|
|
- catch (Exception $e) {
|
|
|
- $transaction->rollback();
|
|
|
- print "\n"; // make sure we start errors on new line
|
|
|
- watchdog_exception('tripal_project', $e);
|
|
|
- watchdog('trp-seturl', "Failed Removing URL Alias: %src", array('%src' => $src), WATCHDOG_ERROR);
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
/**
|
|
|
* Implements [content_type]_chado_node_default_title_format().
|
|
|
*
|
|
@@ -736,3 +583,12 @@ function tripal_project_set_urls($na = NULL, $job = NULL) {
|
|
|
function chado_project_chado_node_default_title_format() {
|
|
|
return '[project.name]';
|
|
|
}
|
|
|
+
|
|
|
+/**
|
|
|
+ * Implements hook_chado_node_default_url_format().
|
|
|
+ *
|
|
|
+ * Designates a default URL format for project nodes.
|
|
|
+ */
|
|
|
+function chado_project_chado_node_default_url_format() {
|
|
|
+ return '/project/[project.project_id]';
|
|
|
+}
|