View source
<?php
function flag_export_flags($flags = array(), $module = '', $indent = '') {
module_load_include('inc', 'features', 'features.export');
$output = $indent . '$flags = array();' . "\n";
foreach ($flags as $item) {
if (is_object($item)) {
$flag = $item;
}
else {
if (!($flag = flag_load($item, TRUE))) {
continue;
}
}
if (!$flag
->is_compatible()) {
drupal_set_message(t('Could not export flag %flag-name: Your flag was created by a different version of the Flag module than is now being used.', array(
'%flag-name' => $flag->name,
)), 'error');
continue;
}
$flag->api_version = FLAG_API_VERSION;
$new_flag = (array) $flag;
if (!empty($module)) {
$new_flag['module'] = $module;
$new_flag['locked'] = array(
'name',
);
}
drupal_alter('flag_export', $new_flag);
$unset_properties = array(
'fid',
'name',
'entity_info',
'roles',
'errors',
);
foreach ($unset_properties as $property) {
unset($new_flag[$property]);
}
$output .= $indent . '// Exported flag: "' . check_plain($flag
->get_title()) . '"' . ".\n";
$output .= $indent . '$flags[\'' . $flag->name . '\'] = ' . (function_exists('features_var_export') ? features_var_export($new_flag, $indent) : var_export($new_flag, TRUE)) . ";\n";
}
$output .= $indent . 'return $flags;' . "\n";
return $output;
}
function flag_import_form() {
$form = array();
$form['import'] = array(
'#title' => t('Flag import code'),
'#type' => 'textarea',
'#default_value' => '',
'#rows' => 15,
'#required' => TRUE,
'#description' => t('Paste the code from a <a href="@export-url">flag export</a> here to import it into you site. Flags imported with the same name will update existing flags. Flags with a new name will be created.', array(
'@export-url' => url(FLAG_ADMIN_PATH . '/export'),
)),
);
$form['submit'] = array(
'#value' => t('Import'),
'#type' => 'submit',
);
return $form;
}
function flag_import_form_validate($form, &$form_state) {
$flags = array();
ob_start();
eval($form_state['values']['import']);
ob_end_clean();
if (!isset($flags) || !is_array($flags)) {
form_set_error('import', t('A valid list of flags could not be found in the import code.'));
return;
}
foreach ($flags as $flag_name => $flag_info) {
$flag_info += array(
'name' => $flag_name,
);
$new_flag = flag_flag::factory_by_array($flag_info);
if ($existing_flag = flag_get_flag($new_flag->name)) {
$new_flag->fid = $existing_flag->fid;
}
if ($errors = $new_flag
->validate()) {
$message = t('The import of the %flag flag failed because the following errors were encountered during the import:', array(
'%flag' => $new_flag->name,
));
$message_errors = array();
foreach ($errors as $field => $field_errors) {
foreach ($field_errors as $error) {
$message_errors[] = $error['message'];
}
}
form_set_error('import', $message . theme('item_list', array(
'items' => $message_errors,
)));
}
else {
$form_state['flags'][] = $new_flag;
}
}
}
function flag_import_form_submit($form, &$form_state) {
module_load_include('inc', 'flag', 'includes/flag.admin');
$entity_types = array();
$new = FALSE;
foreach ($form_state['flags'] as $flag) {
$flag
->save();
if (!empty($flag->status)) {
$flag
->enable();
}
if ($flag->is_new) {
drupal_set_message(t('Flag @name has been imported.', array(
'@name' => $flag->name,
)));
$new = TRUE;
}
else {
drupal_set_message(t('Flag @name has been updated.', array(
'@name' => $flag->name,
)));
}
$entity_types[] = $flag->entity_type;
}
_flag_clear_cache($entity_types, $new);
$form_state['redirect'] = FLAG_ADMIN_PATH;
}
function flag_export_form($form, &$form_state, $flag = NULL) {
if ($flag) {
$flags = array(
$flag,
);
}
if (!isset($flags)) {
if (isset($form_state['values']['flags'])) {
$flags = array();
foreach ($form_state['values']['flags'] as $flag_name) {
if ($flag_name && ($flag = flag_get_flag($flag_name))) {
$flags[] = $flag;
}
}
}
else {
$form['flags'] = array(
'#type' => 'checkboxes',
'#title' => t('Flags to export'),
'#options' => drupal_map_assoc(array_keys(flag_get_flags())),
'#description' => t('Exporting your flags is useful for moving flags from one site to another, or when including your flag definitions in a module.'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Export'),
);
}
}
if (isset($flags)) {
$code = flag_export_flags($flags);
$features_link = module_exists('features') ? url('admin/build/features') : url('http://drupal.org/project/features');
$form['export'] = array(
'#type' => 'textarea',
'#title' => t('Flag exports'),
'#description' => t('Use the exported code to later <a href="@import-flag">import</a> it. Exports can be included in modules using <a href="http://drupal.org/node/305086#default-flags">hook_flag_default_flags()</a> or using the <a href="@features-url">Features</a> module.', array(
'@import-flag' => url(FLAG_ADMIN_PATH . '/import'),
'@features-url' => $features_link,
)),
'#value' => $code,
'#rows' => 15,
);
}
return $form;
}
function flag_export_form_submit($form, &$form_state) {
$form_state['rebuild'] = TRUE;
}
function flag_update_page($flag) {
if ($flag
->is_compatible()) {
drupal_set_message(t('The flag %name is already up-to-date with the latest Flag API and does not need upgrading.', array(
'%name' => $flag->name,
)));
drupal_goto(FLAG_ADMIN_PATH);
}
drupal_set_message(t('The flag %name is currently using the Flag API version @version, which is not compatible with the current version of Flag. You can upgrade this flag by pasting the below code into <em>@module_flag_default_flags()</em> function in the @module.module file.', array(
'%name' => $flag->name,
'@version' => $flag->api_version,
'@module' => $flag->module,
)), 'warning');
flag_update_export($flag);
return drupal_get_form('flag_export_form', $flag);
}
function flag_update_export(&$flag) {
if (empty($flag->api_version)) {
$flag->api_version = 1;
}
$classes = get_declared_classes();
$update_handlers = array();
foreach ($classes as $class) {
if (substr($class, 0, 11) == 'FlagUpdate_') {
$update_handler = new $class();
$version = (string) $update_handler->old_api_version;
$update_handlers[$version] = $update_handler;
}
}
uksort($update_handlers, 'version_compare');
foreach ($update_handlers as $old_api_version => $update_handler) {
if (version_compare($old_api_version, $flag->api_version, '<')) {
continue;
}
$update_handler
->update($flag);
$flag->api_version = $update_handler->new_api_version;
}
}
class FlagUpdate_2 {
public $old_api_version = 1;
public $new_api_version = 2;
static function update(&$flag) {
if (isset($flag->roles) && !isset($flag->roles['flag'])) {
$flag->roles = array(
'flag' => $flag->roles,
'unflag' => $flag->roles,
);
}
}
}
class FlagUpdate_3 {
public $old_api_version = 2;
public $new_api_version = 3;
static function update(&$flag) {
if (isset($flag->content_type)) {
$flag->entity_type = $flag->content_type;
unset($flag->content_type);
}
if (isset($flag->roles)) {
$flag->import_roles = $flag->roles;
}
if (!empty($flag->show_on_teaser)) {
$flag->show_in_links['teaser'] = TRUE;
unset($flag->show_on_teaser);
}
if (!empty($flag->show_on_page)) {
$flag->show_in_links['full'] = TRUE;
unset($flag->show_on_page);
}
if (!empty($flag->show_on_entity) || !empty($flag->show_on_comment)) {
if ($entity_info = entity_get_info($flag->entity_type)) {
foreach ($entity_info['view modes'] as $view_mode => $value) {
$flag->show_in_links[$view_mode] = TRUE;
}
}
unset($flag->show_on_entity, $flag->show_on_comment);
}
}
}