diff --git a/profiles/pece/modules/pece/pece_annotations/pece_annotations.install b/profiles/pece/modules/pece/pece_annotations/pece_annotations.install new file mode 100644 index 000000000..e964d9136 --- /dev/null +++ b/profiles/pece/modules/pece/pece_annotations/pece_annotations.install @@ -0,0 +1,102 @@ +field_permissions->set($permission); + $annot_wrapper->save(); +} + +/** + * Set default permission to Open on existing Annotations. + */ +function pece_annotations_update_7101(&$sandbox) { + + // If this is the first pass through this update function then set some vars. + if (!isset($sandbox['progress'])) { + + // Get published Annotations. + $query = db_select('node', 'n'); + $query->fields('n', array('nid')) + ->condition('n.type', 'pece_annotation') + ->condition('n.status', 1); + $result = $query->execute(); + + $sandbox['progress'] = 0; + $sandbox['max'] = $result->rowCount(); + } + + // Prepares the vars for the db_query_range(). + $sql = variable_get('pece_annotations_perms_update_sql', NULL); + $sql_args = variable_get('pece_annotations_perms_update_sql_args', NULL); + + // Stores the query string for the following progresses. + if (empty($sql) && !empty($query)) { + $sql = $query->__toString(); + variable_set('pece_annotations_perms_update_sql', $sql); + } + + // Stores the query arguments for the following progresses. + if (empty($sql_args) && !empty($query)) { + $sql_args = $query->arguments(); + variable_set('pece_annotations_perms_update_sql_args', $sql_args); + } + + // Exit if no content available. + if (empty($sandbox['max'])) { + $sandbox['#finished'] = 1; + return t('No contents available for update.'); + } + + $nodes_per_pass = 50; + + // Get the nodes to process during this pass. + $result = db_query_range($sql, $sandbox['progress'], $nodes_per_pass, $sql_args); + + while ($row = $result->fetchAssoc()) { + // Load the node and updates permission field. + $node = node_load($row['nid']); + $node_info = array('@nid' => $node->nid, '@title' => $node->title); + if (!property_exists($node, 'field_permissions')) { + // Logs Error. + watchdog('pece_annotations', 'Annotation @title\'s (id: @nid) permissions field not available.', $node_info, WATCHDOG_WARNING); + continue; + } + // Update permissions field. + pece_annotations_set_default_permissions($node); + + // Update the progress information. + $sandbox['progress']++; + } + // Set the "finished" status, to tell batch engine whether this function + // needs to run again. If you set a float, this will indicate the progress of + // the batch so the progress bar will update. + $sandbox['#finished'] = ($sandbox['progress'] >= $sandbox['max']) ? 1 : ($sandbox['progress'] / $sandbox['max']); + + if ($sandbox['#finished'] === 1) { + // A final log of the number of nodes that were updated. + watchdog('pece_annotations', '!count annotations updated in total.', array('!count' => $sandbox['progress']), WATCHDOG_INFO); + + // Running through drush. + if (drupal_is_cli()) { + drupal_set_message(t('!count annotations updated in total.', array('!count' => $sandbox['progress']))); + } + + // Removes the variables that are not needed anymore. + variable_del('pece_annotations_perms_update_sql'); + variable_del('pece_annotations_perms_update_sql_args'); + + // Rebuild node permissions. + node_access_rebuild($batch_mode = TRUE); + + // hook_update_N() may optionally return a string which will be displayed + // to the user. + return t('!count annotations updated in total.', array('!count' => $sandbox['progress'])); + } +}