-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add update to set existing Annotations default permission value.
- Loading branch information
Showing
1 changed file
with
102 additions
and
0 deletions.
There are no files selected for viewing
102 changes: 102 additions & 0 deletions
102
profiles/pece/modules/pece/pece_annotations/pece_annotations.install
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
<?php | ||
/** | ||
* @file | ||
* Installation code for the PECE Annotations module. | ||
*/ | ||
|
||
/** | ||
* Helper to set Annotation's permission value. | ||
*/ | ||
function pece_annotations_set_default_permissions($annotation, $permission = 'open') { | ||
$annot_wrapper = entity_metadata_wrapper('node', $annotation); | ||
$annot_wrapper->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'])); | ||
} | ||
} |