Browse Source

Merge branch '7.x-2.x' of git.drupal.org:sandbox/spficklin/1337878 into 7.x-2.x

Lacey Sanderson 11 years ago
parent
commit
fa78158bfb

+ 3 - 0
tripal_analysis/tripal_analysis.views_default.inc

@@ -52,6 +52,9 @@ function tripal_analysis_defaultvalue_user_analysis() {
   $view->display['page_1']->display_options['menu']['type'] = 'normal';
   unset($view->display['page_1']->display_options['tab_options']);
 
+  // Change the permission
+  $view->display['default']->display_options['access']['perm'] = 'access chado_analysis content';
+
   // Remove edit/delete node
   unset(
     $view->display['default']->display_options['fields']['delete_node'],

+ 3 - 0
tripal_contact/tripal_contact.views_default.inc

@@ -56,6 +56,9 @@ function tripal_contact_defaultvalue_user_contacts() {
   );
   unset($view->display['page_1']->display_options['tab_options']);
 
+  // Change the permission
+  $view->display['default']->display_options['access']['perm'] = 'access chado_contact content';
+
   // Remove edit/delete node
   unset(
     $view->display['default']->display_options['fields']['delete_node'],

+ 2 - 2
tripal_feature/includes/tripal_feature.gff_loader.inc

@@ -316,7 +316,7 @@ function tripal_feature_gff3_load_form_submit($form, &$form_state) {
  * @param $update
  *   Set to 1 to update existing features. New features will be added. Attributes 
  *   for a feature that are not present in the GFF but which are present in the 
- *   database will not be altered. Default is 0
+ *   database will not be altered. Default is 1
  * @param $refresh
  *   Set to 1 to update existing features. New features will be added. Attributes 
  *   for a feature that are not present in the GFF but which are present in the 
@@ -374,7 +374,7 @@ function tripal_feature_gff3_load_form_submit($form, &$form_state) {
  * @ingroup gff3_loader
  */
 function tripal_feature_load_gff3($gff_file, $organism_id, $analysis_id,
-  $add_only = 0, $update = 0, $refresh = 0, $remove = 0, $use_transaction = 1,
+  $add_only = 0, $update = 1, $refresh = 0, $remove = 0, $use_transaction = 1,
   $target_organism_id = NULL, $target_type = NULL,  $create_target = 0,
   $start_line = 1, $landmark_type = '', $alt_id_attr = '', $create_organism = FALSE,
   $job = NULL) {

+ 3 - 0
tripal_feature/tripal_feature.views_default.inc

@@ -54,6 +54,9 @@ function tripal_feature_defaultvalue_user_features() {
   );
   unset($view->display['page_1']->display_options['tab_options']);
 
+  // Change the permission
+  $view->display['default']->display_options['access']['perm'] = 'access chado_feature content';
+
   // Remove edit/delete node
   unset(
     $view->display['default']->display_options['fields']['delete_node'],

+ 9 - 9
tripal_featuremap/includes/tripal_featuremap.chado_node.inc

@@ -107,10 +107,10 @@ function chado_featuremap_form($node, &$form_state) {
     '#default_value' => $description,
   );
 
-  // get the list of unit types  
+  // get the list of unit types
   $units = tripal_get_cvterm_default_select_options('featuremap', 'unittype_id', 'map unit types');
   $units[0] = 'Select a Type';
-  
+
   $form['unittype_id'] = array(
     '#title'       => t('Map Units'),
     '#type'        => t('select'),
@@ -170,7 +170,7 @@ function chado_featuremap_validate($node, $form, &$form_state) {
   // trim white space from text fields
   $node->fmapname       = trim($node->fmapname);
   $node->description    = trim($node->description);
-  
+
   $featuremap = 0;
   // check to make sure the unique name on the map is unique
   // before we try to insert into chado. If this is an update then we will
@@ -221,26 +221,26 @@ function chado_featuremap_node_access($node, $op, $account) {
   if (is_object($node)) {
     $node_type = $node->type;
   }
-  
+
   if($node_type == 'chado_featuremap') {
     if ($op == 'create') {
-      if (!user_access('create chado_featuremap', $account)) {
+      if (!user_access('create chado_featuremap content', $account)) {
         return NODE_ACCESS_DENY;
       }
       return NODE_ACCESS_ALLOW;
     }
     if ($op == 'update') {
-      if (!user_access('edit chado_featuremap', $account)) {
+      if (!user_access('edit chado_featuremap content', $account)) {
         return NODE_ACCESS_DENY;
       }
     }
     if ($op == 'delete') {
-      if (!user_access('delete chado_featuremap', $account)) {
+      if (!user_access('delete chado_featuremap content', $account)) {
         return NODE_ACCESS_DENY;
       }
     }
     if ($op == 'view') {
-      if (!user_access('access chado_featuremap', $account)) {
+      if (!user_access('access chado_featuremap content', $account)) {
         return NODE_ACCESS_DENY;
       }
     }
@@ -418,7 +418,7 @@ function chado_featuremap_load($nodes) {
     if (!$featuremap_id) {
       continue;
     }
-    
+
     $values = array('featuremap_id' => $featuremap_id);
     $featuremap = chado_generate_var('featuremap', $values);
 

+ 3 - 0
tripal_featuremap/tripal_featuremap.views_default.inc

@@ -54,6 +54,9 @@ function tripal_featuremap_defaultvalue_user_featuremaps() {
   );
   unset($view->display['page_1']->display_options['tab_options']);
 
+  // Change the permission
+  $view->display['default']->display_options['access']['perm'] = 'access chado_featuremap content';
+
   // Remove edit/delete node
   unset(
     $view->display['default']->display_options['fields']['delete_node'],

+ 9 - 9
tripal_library/includes/tripal_library.chado_node.inc

@@ -118,7 +118,7 @@ function chado_library_form($node, &$form_state) {
     '#default_value' => $uniquename,
   );
 
-  // get the list of library types  
+  // get the list of library types
   $types = tripal_get_cvterm_default_select_options('library', 'type_id', 'library types');
   $types[0] = 'Select a Type';
 
@@ -208,13 +208,13 @@ function chado_library_validate($node, $form, &$form_state) {
   if($node->op == 'Delete') {
     return;
   }
-  
+
   // we are syncing if we do not have a node ID but we do have a featuremap_id. We don't
   // need to validate during syncing so just skip it.
   if (is_null($node->nid) and property_exists($node, 'library_id') and $node->library_id != 0) {
     return;
   }
-  
+
   // trim white space from text fields
   $node->libraryname = trim($node->libraryname);
   $node->uniquename  = trim($node->uniquename);
@@ -386,7 +386,7 @@ function chado_library_load($nodes) {
     if (!$library_id) {
       continue;
     }
-    
+
     $values = array('library_id' => $library_id);
     $library = chado_generate_var('library', $values);
 
@@ -456,26 +456,26 @@ function chado_library_node_access($node, $op, $account) {
   if (is_object($node)) {
     $node_type = $node->type;
   }
-  
+
   if($node_type == 'chado_library') {
     if ($op == 'create') {
-      if (!user_access('create chado_library', $account)) {
+      if (!user_access('create chado_library content', $account)) {
         return NODE_ACCESS_DENY;
       }
       return NODE_ACCESS_ALLOW;
     }
     if ($op == 'update') {
-      if (!user_access('edit chado_library', $account)) {
+      if (!user_access('edit chado_library content', $account)) {
         return NODE_ACCESS_DENY;
       }
     }
     if ($op == 'delete') {
-      if (!user_access('delete chado_library', $account)) {
+      if (!user_access('delete chado_library content', $account)) {
         return NODE_ACCESS_DENY;
       }
     }
     if ($op == 'view') {
-      if (!user_access('access chado_library', $account)) {
+      if (!user_access('access chado_library content', $account)) {
         return NODE_ACCESS_DENY;
       }
     }

+ 1 - 1
tripal_library/tripal_library.module

@@ -28,7 +28,7 @@ require_once 'includes/tripal_library.chado_node.inc';
  *
  * @ingroup tripal_library
  */
-function tripal_library_permisssion() {
+function tripal_library_permission() {
   return array(
     'access chado_library content' => array(
       'title' => t('View Libraries'),

+ 3 - 0
tripal_library/tripal_library.views_default.inc

@@ -54,6 +54,9 @@ function tripal_library_defaultvalue_user_library() {
   );
   unset($view->display['page_1']->display_options['tab_options']);
 
+  // Change the permission
+  $view->display['default']->display_options['access']['perm'] = 'access chado_library content';
+
   // Remove edit/delete node
   unset(
     $view->display['default']->display_options['fields']['delete_node'],

+ 7 - 7
tripal_organism/includes/tripal_organism.chado_node.inc

@@ -66,26 +66,26 @@ function chado_organism_node_access($node, $op, $account) {
   if (is_object($node)) {
     $node_type = $node->type;
   }
-  
+
   if($node_type == 'chado_organism') {
     if ($op == 'create') {
-      if (!user_access('create chado_organism', $account)) {
+      if (!user_access('create chado_organism content', $account)) {
         return NODE_ACCESS_DENY;
       }
       return NODE_ACCESS_ALLOW;
     }
     if ($op == 'update') {
-      if (!user_access('edit chado_organism', $account)) {
+      if (!user_access('edit chado_organism content', $account)) {
         return NODE_ACCESS_DENY;
       }
     }
     if ($op == 'delete') {
-      if (!user_access('delete chado_organism', $account)) {
+      if (!user_access('delete chado_organism content', $account)) {
         return NODE_ACCESS_DENY;
       }
     }
     if ($op == 'view') {
-      if (!user_access('access chado_organism', $account)) {
+      if (!user_access('access chado_organism content', $account)) {
         return NODE_ACCESS_DENY;
       }
     }
@@ -230,7 +230,7 @@ function chado_organism_validate($node, $form, &$form_state) {
   $node->abbreviation = trim($node->abbreviation);
   $node->common_name  = trim($node->common_name);
   $node->description  = trim($node->description);
-  
+
   // Validating for an update
   if (property_exists($node, 'organism_id')) {
     $sql = "
@@ -486,7 +486,7 @@ function chado_organism_load($nodes) {
   foreach ($nodes as $nid => $node) {
     // find the organism and add in the details
     $organism_id = chado_get_id_from_nid('organism', $nid);
-    
+
     // if the nid does not have a matching record then skip this node.
     // this can happen with orphaned nodes.
     if (!$organism_id) {

+ 3 - 0
tripal_organism/tripal_organism.views_default.inc

@@ -54,6 +54,9 @@ function tripal_organism_defaultvalue_user_organisms() {
   );
   unset($view->display['page_1']->display_options['tab_options']);
 
+  // Change the permission
+  $view->display['default']->display_options['access']['perm'] = 'access chado_organism content';
+
   // Remove edit/delete node
   unset(
     $view->display['default']->display_options['fields']['delete_node'],

+ 46 - 2
tripal_project/includes/tripal_project.admin.inc

@@ -46,8 +46,43 @@ function tripal_project_admin_project_view() {
 function tripal_project_admin($form, $form_state) {
   $form = array();
 
-  $form['nothing'] = array(
-    '#markup' => t('There are currently no settings to configure.')
+  // project URL PATHS
+  $form['url'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('Project URL Path'),
+    '#collapsible' => TRUE,
+    '#collapsed' => FALSE,
+  );
+
+  $options = array(
+    'project'      => 'project:' . t('Chado table name'),
+    'SID[id]'      => '[id]:' . t('The Chado project_id'),
+    '[name]'       => '[name]:' . t('The project name'),
+  );
+
+  $form['url']['options'] = array(
+    '#type' => 'item',
+    '#title' => 'Available Tokens',
+    '#markup' => '<ul><li>' . implode('</li><li>', $options) . '</li></ul>'
+  );
+
+  $form['url']['chado_project_url_string'] = array(
+    '#title' => 'URL Syntax',
+    '#type' => 'textfield',
+    '#description' => t('You may rearrange elements in this text box to '.
+      'customize the URLs.  The available tags are listed below. You can separate or '.
+      'include any text between the tags. Click the "Set project URLs" button to '.
+      'reset the URLs for all project pages.  Click the "Save Configuration" button to '.
+      'simply save this setup. <b>Important</b>: be sure that whatever you choose will '.
+      'always be unique even considering future data that may be added.  If you include '.
+      'the Chado table name and id you are guaranteed to have a unique URL. For example project/[id]'),
+    '#size' => 150,
+    '#default_value' => variable_get('chado_project_url_string', '/project/[id]'),
+  );
+
+  $form['url']['button'] = array(
+    '#type' => 'submit',
+    '#value' => t('Set Project URLs'),
   );
 
   return system_settings_form($form);
@@ -59,5 +94,14 @@ function tripal_project_admin($form, $form_state) {
  * @ingroup tripal_project
  */
 function tripal_project_admin_validate($form, &$form_state) {
+  global $user;  // we need access to the user info
+  $job_args = array();
 
+  switch ($form_state['values']['op']) {
+    case t('Set Project URLs') :
+      variable_set('chado_project_url', $form_state['values']['chado_project_url_string']);
+      tripal_add_job('Set Project URLs', 'tripal_project',
+        'tripal_project_set_urls', $job_args, $user->uid);
+      break;
+  }
 }

+ 193 - 8
tripal_project/includes/tripal_project.chado_node.inc

@@ -208,7 +208,7 @@ function chado_project_validate($node, $form, &$form_state) {
   // trim white space from text fields
   $node->title = trim($node->title);
   $node->description = trim($node->description);
-  
+
   $project = 0;
   // check to make sure the name on the project is unique
   // before we try to insert into chado.
@@ -395,8 +395,8 @@ function chado_project_load($nodes) {
     if (!$project_id) {
       continue;
     }
-    
-    
+
+
     $values = array('project_id' => $project_id);
     $project = chado_generate_var('project', $values);
 
@@ -435,26 +435,26 @@ function chado_project_node_access($node, $op, $account) {
   if (is_object($node)) {
     $node_type = $node->type;
   }
-  
+
   if($node_type == 'chado_project') {
     if ($op == 'create') {
-      if (!user_access('create chado_project', $account)) {
+      if (!user_access('create chado_project content', $account)) {
         return NODE_ACCESS_DENY;
       }
       return NODE_ACCESS_ALLOW;
     }
     if ($op == 'update') {
-      if (!user_access('edit chado_project', $account)) {
+      if (!user_access('edit chado_project content', $account)) {
         return NODE_ACCESS_DENY;
       }
     }
     if ($op == 'delete') {
-      if (!user_access('delete chado_project', $account)) {
+      if (!user_access('delete chado_project content', $account)) {
         return NODE_ACCESS_DENY;
       }
     }
     if ($op == 'view') {
-      if (!user_access('access chado_project', $account)) {
+      if (!user_access('access chado_project content', $account)) {
         return NODE_ACCESS_DENY;
       }
     }
@@ -507,3 +507,188 @@ function tripal_project_node_view($node, $view_mode, $langcode) {
       break;
   }
 }
+
+/**
+ * Implements hook_node_insert().
+ * Acts on all content types.
+ *
+ * @ingroup tripal_project
+ */
+function tripal_project_node_insert($node) {
+
+  // set the URL path after inserting.  We do it here because we do not
+  // know the project_id in the presave
+  switch ($node->type) {
+    case 'chado_project':
+
+      // 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 = chado_get_id_from_nid('project', $node->nid);
+
+      // remove any previous alias
+      db_query("DELETE FROM {url_alias} WHERE source = :source", array(':source' => "node/$node->nid"));
+
+      // 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;
+  }
+}
+
+/**
+ * Implements hook_node_update().
+ * Acts on all content types.
+ *
+ * @ingroup tripal_project
+ */
+function tripal_project_node_update($node) {
+
+  // add items to other nodes, build index and search results
+  switch ($node->type) {
+    case 'chado_project':
+      // remove any previous alias
+      db_query("DELETE FROM {url_alias} WHERE source = :source", array(':source' => "node/$node->nid"));
+
+      // 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_job_set_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_job_set_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);
+  }
+}

+ 5 - 1
tripal_project/tripal_project.views_default.inc

@@ -54,6 +54,9 @@ function tripal_project_defaultvalue_user_projects() {
   );
   unset($view->display['page']->display_options['tab_options']);
 
+  // Change the permission
+  $view->display['default']->display_options['access']['perm'] = 'access chado_project content';
+
   // Remove edit/delete node
   unset(
     $view->display['default']->display_options['fields']['delete_node'],
@@ -88,7 +91,8 @@ function tripal_project_defaultview_admin_projects() {
   $handler = $view->new_display('default', 'Master', 'default');
   $handler->display->display_options['title'] = 'Projects';
   $handler->display->display_options['use_more_always'] = FALSE;
-  $handler->display->display_options['access']['type'] = 'none';
+$handler->display->display_options['access']['type'] = 'perm';
+$handler->display->display_options['access']['perm'] = 'adminster tripal project';
   $handler->display->display_options['cache']['type'] = 'none';
   $handler->display->display_options['query']['type'] = 'views_query';
   $handler->display->display_options['exposed_form']['type'] = 'basic';

+ 0 - 5
tripal_stock/includes/tripal_stock.admin.inc

@@ -161,11 +161,6 @@ function tripal_stock_admin_validate($form, &$form_state) {
   global $user;  // we need access to the user info
   $job_args = array();
 
-  variable_set('chado_stock_types_cv', $form_state['values']['stock_types_cv']);
-  variable_set('chado_stock_prop_types_cv', $form_state['values']['stock_prop_types_cv']);
-  variable_set('chado_stock_relationship_cv', $form_state['values']['stock_relationship_cv']);
-  variable_set('chado_stock_url_string', $form_state['values']['chado_stock_url_string']);
-
   switch ($form_state['values']['op']) {
     case  t('Set Controlled Vacabularies') :
       break;

+ 19 - 17
tripal_stock/includes/tripal_stock.chado_node.inc

@@ -54,10 +54,10 @@ function chado_stock_load($nodes) {
 
   $new_nodes = array();
   foreach ($nodes as $nid => $node) {
-    
+
     // get the stock details from chado
     $stock_id = chado_get_id_from_nid('stock', $nid);
-    
+
     // if the nid does not have a matching record then skip this node.
     // this can happen with orphaned nodes.
     if (!$stock_id) {
@@ -67,7 +67,7 @@ function chado_stock_load($nodes) {
     // build the variable with all the stock details
     $values = array('stock_id' => $stock_id);
     $stock = chado_generate_var('stock', $values);
-    
+
     // add in the uniquename and the description as these are both text fields
     $stock = chado_expand_var($stock, 'field', 'stock.uniquename');
     $stock = chado_expand_var($stock, 'field', 'stock.description');
@@ -204,7 +204,7 @@ function chado_stock_form($node, $form_state) {
     $dbxref_description = $form_state['input']['db_description'];
     $dbxref_database = $form_state['input']['database'];
   }
-  
+
   $form['names'] = array(
     '#type' => 'fieldset',
     '#title' => t('Stock Name')
@@ -224,10 +224,12 @@ function chado_stock_form($node, $form_state) {
     '#required'       => TRUE
   );
 
-  $form['names']['stock_id'] = array(
-    '#type' => 'hidden',
-    '#value' => $stock_id,
-  );
+  if ($stock_id > 0) {
+    $form['names']['stock_id'] = array(
+      '#type' => 'hidden',
+      '#value' => $stock_id,
+    );
+  }
 
   $form['details'] = array(
     '#type' => 'fieldset',
@@ -236,7 +238,7 @@ function chado_stock_form($node, $form_state) {
 
   $type_options = tripal_get_cvterm_default_select_options('stock', 'type_id', 'stock types');
   $type_options[0] = 'Select a Type';
-  
+
   $form['details']['type_id'] = array(
     '#type' => 'select',
     '#title' => t('Type of Stock'),
@@ -364,14 +366,14 @@ function chado_stock_validate(&$node, $form, &$form_state) {
   $node->stock_description = trim($node->stock_description);
   $node->accession = trim($node->accession);
   $node->db_description = trim($node->db_description);
-  
+
   $int_in_chado_sql = "SELECT count(*) as count FROM {:table} WHERE :column = :value";
   $string_in_chado_sql = "SELECT count(*) as count FROM {:table} WHERE :column = :value";
 
   // if this is an update, we want to make sure that a different stock for
   // the organism doesn't already have this uniquename. We don't want to give
   // two sequences the same uniquename
-  if (property_exists($node, 'nid')) {
+  if (property_exists($node, 'nid') and property_exists($node, 'stock_id')) {
     $sql = "
       SELECT *
       FROM {stock} S
@@ -465,7 +467,7 @@ function chado_stock_validate(&$node, $form, &$form_state) {
  * @ingroup tripal_stock
  */
 function chado_stock_insert($node) {
-  
+
   $node->uniquename   = trim($node->uniquename);
   $node->sname        = trim($node->sname);
 
@@ -473,7 +475,7 @@ function chado_stock_insert($node) {
   // we can skip adding the stock to chado as it is already there, although
   // we do need to proceed with the rest of the insert
   if (!property_exists($node, 'stock_id')) {
-  
+
     // before we can add the stock, we must add the dbxref if one has been
     // provided by the user.
     $dbxref_status = 0;
@@ -558,7 +560,7 @@ function chado_stock_insert($node) {
       // Now add in relationships
       $details = array(
         'relationship_table' => 'stock_relationship',
-        'foreignkey_value' => $node->stock_id
+        'foreignkey_value' => $stock_id
       );
       chado_update_node_form_relationships($node, $details);
     }
@@ -605,7 +607,7 @@ function chado_stock_insert($node) {
  * @ingroup tripal_stock
  */
 function chado_stock_update($node) {
-  
+
   $node->uniquename   = trim($node->uniquename);
   $node->sname        = trim($node->sname);
 
@@ -898,7 +900,7 @@ function tripal_stock_node_insert($node) {
       // on an insert we need to add the stock_id to the node object
       // so that the tripal_stock_get_stock_url function can set the URL properly
       $node->stock_id = chado_get_id_from_nid('stock', $node->nid);
-      
+
       // remove any previous alias
       db_query("DELETE FROM {url_alias} WHERE source = :source", array(':source' => "node/$node->nid"));
 
@@ -1042,7 +1044,7 @@ function tripal_stock_set_urls($na = NULL, $job = NULL) {
     $sql = "SELECT * FROM {chado_stock}";
     $nodes = db_query($sql);
     foreach ($nodes as $node) {
-       
+
       // get the URL alias
       $src = "node/$node->nid";
       $dst = tripal_stock_get_stock_url($node, $url_alias);

+ 3 - 0
tripal_stock/tripal_stock.views_default.inc

@@ -54,6 +54,9 @@ function tripal_stock_defaultvalue_user_stocks() {
   );
   unset($view->display['page_1']->display_options['tab_options']);
 
+  // Change the permission
+  $view->display['default']->display_options['access']['perm'] = 'access chado_stock content';
+
   // Remove edit/delete node
   unset(
     $view->display['default']->display_options['fields']['delete_node'],