'radios',
'#title' => t('Status'),
'#description' => t(
"If you don't want to use the CDN to serve files to your visitors yet,
but you do want to see if it's working well for your site, then enable
testing mode.
Users will only get the files from the CDN if they
have the %cdn-testing-mode-permission permission.",
array('%cdn-testing-mode-permission' => 'access files on CDN when in testing mode')
),
'#required' => TRUE,
'#options' => array(
CDN_DISABLED => t('Disabled'),
CDN_TESTING => t('Testing mode'),
CDN_ENABLED => t('Enabled'),
),
'#default_value' => variable_get(CDN_STATUS_VARIABLE, CDN_DISABLED),
);
$form[CDN_STATS_VARIABLE] = array(
'#type' => 'checkbox',
'#title' => t('Display statistics'),
'#description' => t(
'Shows the CDN integration statistics of the current page, to users with
the %access-per-page-statistics permission.',
array('%access-per-page-statistics' => 'access per-page statistics')
),
'#return_value' => CDN_ENABLED,
'#default_value' => variable_get(CDN_STATS_VARIABLE, CDN_DISABLED),
'#states' => array(
'invisible' => array(
':input[name="' . CDN_STATUS_VARIABLE . '"]' => array('value' => '' . CDN_DISABLED),
),
),
);
return system_settings_form($form);
}
/**
* Form definition; details.
*/
function cdn_admin_details_form($form, &$form_state) {
// Prevent requirement errors from showing up twice.
if (empty($form_state['post'])) {
_cdn_admin_check_requirements();
}
$form['#submit'] = array('cdn_admin_settings_submit_show_cache_warning');
$form[CDN_MODE_VARIABLE] = array(
'#type' => 'radios',
'#title' => t('Mode'),
'#description' => t(
"It is essential that you understand the key
properties of a CDN, most importantly the differences between an
Origin Pull CDN and a Push CDN. If you understand that,
continue to make a choice between the two supported modes:
!mode-explanation-list", array(
'!cdn-article-url' => 'http://wimleers.com/article/key-properties-of-a-cdn',
'!mode-explanation-list' => theme('item_list', array('items' => array(
t("Origin Pull mode replaces your domain name with
the Origin Pull CDN's domain name in the file URL."),
t("File Conveyor mode uses the File Conveyor
daemon to synchronize files and can be used with both Origin Pull
and Push CDNs. If you're using an Origin Pull CDN and want to
optimize files before they're synced to the CDN, it is also
recommended to use this mode.")
))),
)
),
'#required' => TRUE,
'#options' => array(
CDN_MODE_BASIC => t('Origin Pull'),
CDN_MODE_ADVANCED => t('File Conveyor'),
),
'#default_value' => variable_get(CDN_MODE_VARIABLE, CDN_MODE_BASIC),
);
$form['settings'] = array(
'#type' => 'fieldset',
'#title' => t('Mode-specific settings'),
'#description' => t('Settings specific to the mode you have currently selected.'),
);
//
// Basic mode settings.
//
$format_variables = array(
'@format-url' => '<' . t('URL') . '>',
'@format-extensions' => '<' . t('extensions') . '>',
);
$format_explanation_list_items = array(
t("@format-url is the CDN URL that should be used. E.g.:
'http://cdn-a.com
'.", $format_variables),
t("@format-extensions is an optional
setting to limit which files should be served from a CDN. E.g.:
'.css .jpg .jpeg .png
'.", $format_variables),
);
$example_list_items = array(
t('This would serve all files from a single CDN:
http://cdn-a.com
'),
t("This would serve CSS and images from CDN A, .zip files from CDN B and
everything else from CDN C:
http://cdn-a.com|.css .jpg .jpeg .png\nhttp://cdn-b.com|.zip\nhttp://cdn-c.com
"),
);
$form['settings'][CDN_BASIC_MAPPING_VARIABLE] = array(
'#type' => 'textarea',
'#title' => t('CDN mapping'),
'#description' => t("Define which files are mapped to which CDN.*.js
for all JavaScript files and
mytheme/*.css
for all CSS files in the mytheme
directory."
);
$form['exceptions'] = array(
'#type' => 'fieldset',
'#title' => t('Exceptions'),
'#description' => t('Which files should be served from a CDN is not as
simple as it seems: there are bound to be exceptions.
You can easily define those exceptions here, either
by file URL, Drupal path or by Drupal path for
authenticated users.'),
);
$form['exceptions']['file_path'] = array(
'#type' => 'fieldset',
'#title' => t('File URL'),
'#description' => t("Files that are marked to not be served from the CDN
because of a match in the blacklist, can be overridden to be served from
the CDN after all, if they have a match in the whitelist.cdn_pick_server()
function (consult the
included README for examples). However, to lower the
barrier to start using it, it has been made possible
to just enter the body of that function right here,
in the settings of the CDN module, so you would not
have to create a small module for it.'),
);
if (function_exists('cdn_pick_server')) {
$form['cdn_pick_server']['defined_in_code'] = array(
'#type' => 'item',
'#title' => t('Defined in code'),
'#markup' => 'The cdn_pick_server()
function is already defined in code.',
);
}
else {
$php_code = variable_get(CDN_PICK_SERVER_PHP_CODE_VARIABLE, '');
$form['cdn_pick_server'][CDN_PICK_SERVER_PHP_CODE_VARIABLE] = array(
'#type' => 'textarea',
'#title' => t('PHP code for cdn_pick_server()'),
'#description' => t('The parameter $servers_for_file
is
available to you (which is an array of servers),
you should return one of them (or return an empty
array if you do not want any of them to be used).
$servers_for_file
looks
like this:
$servers_for_file[0] = array(\'url\' => \'http://cdn1.com/image.jpg\', \'server\' => \'cdn1.com\');
$servers_for_file[1] = array(\'url\' => \'http://cdn2.net/image.jpg\', \'server\' => \'cdn2.net\');
Eventually, you should always end your code with
something like
return $servers_for_file[$some_index];
'),
'#cols' => 60,
'#rows' => ($php_code != '') ? count(explode("\n", $php_code)) + 1 : 5,
'#default_value' => $php_code,
);
}
return system_settings_form($form);
}
/**
* Default validate callback for the details form.
*/
function cdn_admin_details_form_validate($form, &$form_state) {
// If ctools_dependent supported required dependent form items, this ugly
// piece of code would not be necessary.
if ($form_state['values'][CDN_MODE_VARIABLE] == CDN_MODE_BASIC) {
if (empty($form_state['values'][CDN_BASIC_MAPPING_VARIABLE])) {
form_error($form['settings'][CDN_BASIC_MAPPING_VARIABLE], t('!name field is required.', array('!name' => $form['settings'][CDN_BASIC_MAPPING_VARIABLE]['#title'])));
}
}
else {
if (empty($form_state['values'][CDN_ADVANCED_SYNCED_FILES_DB_VARIABLE])) {
form_error($form['settings'][CDN_ADVANCED_SYNCED_FILES_DB_VARIABLE], t('!name field is required.', array('!name' => $form['settings'][CDN_ADVANCED_SYNCED_FILES_DB_VARIABLE]['#title'])));
}
}
// When in advanced mode, validate the synced files DB.
if ($form_state['values'][CDN_MODE_VARIABLE] == CDN_MODE_ADVANCED) {
$synced_files_db = $form_state['values'][CDN_ADVANCED_SYNCED_FILES_DB_VARIABLE];
_cdn_admin_validate_synced_files_db($synced_files_db, CDN_ADVANCED_SYNCED_FILES_DB_VARIABLE);
}
}
/**
* Default validate callback for the other settings form.
*/
function cdn_admin_other_settings_form_validate($form, &$form_state) {
if ($form_state['values'][CDN_PICK_SERVER_PHP_CODE_VARIABLE] !== '') {
$php_code = $form_state['values'][CDN_PICK_SERVER_PHP_CODE_VARIABLE];
$servers = array();
if (_cdn_eval_pick_server($php_code, $servers) === FALSE) {
form_error($form['cdn_pick_server'][CDN_PICK_SERVER_PHP_CODE_VARIABLE], t('Your cdn_pick_server()
cannot be evaluated. Please correct it or leave it empty.'));
}
}
}
/**
* Submit callback for all settings forms, that shows a warning whenever
* settings have been changed: the caches might need to be cleared.
*/
function cdn_admin_settings_submit_show_cache_warning($form, &$form_state) {
drupal_set_message(t('You have just changed some settings. This might affect
the file URLs that have been cached in nodes, views,
or elsewhere.