peid] = array( '#tree' => TRUE, ); $form['pageears'][$pageear->peid]['peid'] = array( '#type' => 'hidden', '#value' => $pageear->peid, ); $form['pageears'][$pageear->peid]['name'] = array( '#markup' => check_plain($pageear->name), ); $form['pageears'][$pageear->peid]['link'] = array( '#markup' => check_plain($pageear->link), ); $form['pageears'][$pageear->peid]['peel_position'] = array( '#markup' => pageear_get_options('peel_position', $pageear->peel_position), ); $form['pageears'][$pageear->peid]['status'] = array( '#type' => 'checkbox', '#default_value' => (bool) (isset($form_state['pageears'][$pageear->peid]['status']) ? $form_state['pageears'][$pageear->peid]['status'] : $pageear->status), ); $form['pageears'][$pageear->peid]['weight'] = array( '#type' => 'weight', '#delta' => 50, '#default_value' => isset($form_state['pageears'][$pageear->peid]['weight']) ? $form_state['pageears'][$pageear->peid]['weight'] : $pageear->weight, ); $form['pageears'][$pageear->peid]['clone'] = array( '#markup' => l(t('clone'), 'admin/structure/pageear/'. $pageear->peid .'/clone'), ); $form['pageears'][$pageear->peid]['edit'] = array( '#markup' => l(t('edit'), 'admin/structure/pageear/'. $pageear->peid .'/edit'), ); $form['pageears'][$pageear->peid]['delete'] = array( '#markup' => l(t('delete'), 'admin/structure/pageear/'. $pageear->peid .'/delete'), ); } $form['submit'] = array( '#type' => 'submit', '#value' => t('Save configuration'), ); } else { $form['empty'] = array('#markup' => t('There are no pageears yet. !Add_new a pageear first.', array('!Add_new' => l(t('Add'), 'admin/structure/pageear/add')))); } return $form; } /** * Theme: List of pageears. */ function theme_pageear_list_form($variables) { $form = $variables['form']; $header = array( t('Pageear'), t('Link'), t('Position'), array('data' => t('Enabled'), 'class' => array('checkbox')), t('Weight'), array('data' => t('Operations'), 'colspan' => '3'), ); $pageears = array(); foreach (element_children($form['pageears']) as $peid) { $form['pageears'][$peid]['weight']['#attributes']['class'] = array('pageear-weight'); $pageears[] = array( 'data' => array( drupal_render($form['pageears'][$peid]['name']), drupal_render($form['pageears'][$peid]['link']), drupal_render($form['pageears'][$peid]['peel_position']), array( 'data' => drupal_render($form['pageears'][$peid]['status']), 'class' => array('checkbox'), ), drupal_render($form['pageears'][$peid]['weight']), drupal_render($form['pageears'][$peid]['clone']), drupal_render($form['pageears'][$peid]['edit']), drupal_render($form['pageears'][$peid]['delete']), ), 'class' => array('draggable'), ); } $output = ''; if (count($pageears)) { $output .= theme('table', array('header' => $header, 'rows' => $pageears, 'attributes' => array('id' => 'order-pageears'))); } $output .= drupal_render_children($form); drupal_add_tabledrag('order-pageears', 'order', 'sibling', 'pageear-weight'); return $output; } /** * Submit: List of pageears. */ function pageear_list_form_submit($form, &$form_state) { $num_enabled_pageears = 0; // for each pageear update its status and its weight. foreach (element_children($form['pageears']) as $peid) { $pageear->peid = $form['pageears'][$peid]['peid']['#value']; $pageear->weight = $form['pageears'][$peid]['weight']['#value']; $pageear->status = $form['pageears'][$peid]['status']['#value']; if ($pageear->status) { $num_enabled_pageears++; } drupal_write_record('pageears', $pageear, 'peid'); } // Update the persistent variable numEnabledPageears that tracks number of pageears enabled variable_set('num_enabled_pageears', $num_enabled_pageears); } /** * Menu callback; displays the pageear configuration form. * * Using the same form for three different operations ($op): * - 'edit': edit an existing pageear (default) * - 'add': add a new pageear * - 'clone': clone an existing pageear */ function pageear_admin_edit($form, &$form_state, $pageear, $op = 'edit') { if ($op == 'add' || !$pageear->peid) { $pageear = pageear_get_default(); } if ($op == 'clone') { $pageear->peid = 0; } // @todo All sliders css styles should not be inline. Create stylesheet file. $form = array(); $form['#attributes'] = array('enctype' => "multipart/form-data"); $form['peid'] = array( '#type' => 'value', '#value' => $pageear->peid, ); // Info $form['info'] = array( '#type' => 'fieldset', '#title' => t('Pageear info'), '#collapsible' => TRUE, ); $form['info']['status'] = array( '#type' => 'checkbox', '#title' => t('Enable pageear'), '#default_value' => $pageear->status, '#description' => t('If this setting is enabled, the current pageear is shown on the configured pages (you can edit visibility settings below).'), ); // previous status of this pageear before starting configuration $form['info']['previous_status'] = array( '#type' => 'hidden', '#value' => $pageear->status, ); $form['info']['name'] = array( '#type' => 'textfield', '#title' => t('Pageear name'), '#default_value' => $pageear->name, '#required' => TRUE, '#maxlength' => 64, '#size' => 40, ); // End Info // Details $form['details'] = array( '#type' => 'vertical_tabs', //'#title' => t('Pageear details'), //'#collapsible' => TRUE, //'#collapsed' => TRUE, ); // Position settings. $form['details']['position_settings'] = array( '#type' => 'fieldset', '#title' => t('Position Settings'), '#collapsible' => TRUE, '#collapsed' => TRUE, ); // Set position of pageear ( topleft | topright | bottomleft | bottomright ). $form['details']['position_settings']['peel_position'] = array( '#type' => 'radios', '#title' => t('Corner'), '#description' => t('In what corner the ad will be displayed.'), '#options' => pageear_get_options('peel_position'), '#default_value' => $pageear->peel_position, '#required' => TRUE, '#attributes' => array('onclick' => 'checkPeelPosition()'), ); // Disable absolute position model if pageear is set on bottom corner $peel_position_check_js = "function checkPeelPosition() {"; $peel_position_check_js .= "(function($) {"; $peel_position_check_js .= "if ($('#edit-peel-position-bottomleft').attr('checked') || $('#edit-peel-position-bottomright').attr('checked')) {"; $peel_position_check_js .= "$('#edit-peel-position-model-absolute').attr('disabled', true);"; $peel_position_check_js .= "$('#edit-peel-position-model-fixed').attr('checked', true);"; $peel_position_check_js .= "} else {"; $peel_position_check_js .= "$('#edit-peel-position-model-absolute').removeAttr('disabled');"; $peel_position_check_js .= "}"; $peel_position_check_js .= "}) (jQuery);"; $peel_position_check_js .= "}"; $form['details']['position_settings']['peel_position']['#attached']['js'][] = array( 'data' => $peel_position_check_js, 'type' => 'inline', ); // Set position model of peageear ( absolute | fixed ). $form['details']['position_settings']['peel_position_model'] = array( '#type' => 'radios', '#title' => t('Position Model'), '#description' => t('The CSS Position model you would prefer to use.'), '#options' => pageear_get_options('peel_position_model'), '#default_value' => $pageear->peel_position_model, '#required' => TRUE, ); $peel_position_model_ini_js = "(function($){\n"; $peel_position_model_ini_js .= "$(document).ready(function() {\n"; $peel_position_model_ini_js .= "if ($('#edit-peelPosition-bottomleft').attr('checked') || $('#edit-peelPosition-bottomright').attr('checked')) {\n"; $peel_position_model_ini_js .= "$('#edit-peelPositionModel-absolute').attr('disabled', true);\n"; $peel_position_model_ini_js .= "}\n"; $peel_position_model_ini_js .= "});\n"; $peel_position_model_ini_js .= "}) (jQuery);"; $form['details']['position_settings']['peel_position_model']['#attached']['js'][] = array( 'data' => $peel_position_model_ini_js, 'type' => 'inline', ); // End Position settings. // Wait settings. $form['details']['wait_settings'] = array( '#type' => 'fieldset', '#title' => t('Wait Icon Settings'), '#collapsible' => TRUE, '#collapsed' => TRUE, ); $form['details']['wait_settings']['wait_enable'] = array( '#type' => 'checkbox', '#title' => t('Enable wait icon'), '#default_value' => $pageear->wait_enable, '#description' => t('If this setting is enabled, a wait icon will be displayed while pageear is loading.'), ); // Set wait icon width $form['details']['wait_settings']['wait_width'] = array( '#type' => 'textfield', '#title' => t('Wait Icon Width'), '#required' => TRUE, '#default_value' => $pageear->wait_width, '#maxlength' => 3, '#size' => 8, ); // Set wait icon height $form['details']['wait_settings']['wait_height'] = array( '#type' => 'textfield', '#title' => t('Wait Icon Height'), '#required' => TRUE, '#default_value' => $pageear->wait_height, '#maxlength' => 3, '#size' => 8, ); // URL to wait icon. $form['details']['wait_settings']['wait_url'] = array( '#type' => 'item', '#title' => t('Wait icon'), '#markup' => check_plain($pageear->wait_url), ); $form['details']['wait_settings']['wait_url_upload'] = array( '#type' => 'file', '#description' => t('Upload an image to show while pageear is loading (most common an animated GIF, Wait Icon Width x Wait Icon Height pixels).'), '#tree' => FALSE, '#size' => 40, ); // End wait settings. // Style settings. $form['details']['style_settings'] = array( '#type' => 'fieldset', '#title' => t('Style Settings'), '#collapsible' => TRUE, '#collapsed' => TRUE, ); // Style of flag ('style1' | 'style2'). $form['details']['style_settings']['flag_style'] = array( '#type' => 'select', '#title' => t('Flag Style'), '#options' => pageear_get_options('flag_style'), '#default_value' => $pageear->flag_style, '#required' => TRUE, ); // Style of peel ('style1' | 'style2'). $form['details']['style_settings']['peel_style'] = array( '#type' => 'select', '#title' => t('Peel Style'), '#options' => pageear_get_options('peel_style'), '#default_value' => $pageear->peel_style, '#required' => TRUE, ); // End Style settings. // Size Settings. $form['details']['size_settings'] = array( '#type' => 'fieldset', '#title' => t('Size Settings'), '#collapsible' => TRUE, '#collapsed' => TRUE, '#description' => t('Design sizes are 100x100px and 500x500px for flag and peel respectively. If you decide to use a custom size try to use logical values. For example it makes not sense to use a larger size for the flag than for the peel. For a further explanation refer to !pageear_size_manual.', array('!pageear_size_manual' => l(t('howto on home page'), 'http://www.pageeargpl.co.cc/content/size-settings', array('attributes' => array('target' => '_blank'))))), ); // Set flag width $form['details']['size_settings']['flag_width'] = array( '#type' => 'textfield', '#title' => t('Flag Width'), '#required' => TRUE, '#default_value' => $pageear->flag_width, '#maxlength' => 4, '#size' => 8, ); // Set flag height $form['details']['size_settings']['flag_height'] = array( '#type' => 'textfield', '#title' => t('Flag Height'), '#required' => TRUE, '#default_value' => $pageear->flag_height, '#maxlength' => 4, '#size' => 8, ); // Set peel width $form['details']['size_settings']['peel_width'] = array( '#type' => 'textfield', '#title' => t('Peel Width'), '#required' => TRUE, '#default_value' => $pageear->peel_width, '#maxlength' => 5, '#size' => 8, ); // Set peel height $form['details']['size_settings']['peel_height'] = array( '#type' => 'textfield', '#title' => t('Peel Height'), '#required' => TRUE, '#default_value' => $pageear->peel_height, '#maxlength' => 5, '#size' => 8, ); // End Size settings. // Image Settings. $form['details']['image_settings'] = array( '#type' => 'fieldset', '#title' => t('Image Settings'), '#collapsible' => TRUE, '#collapsed' => TRUE, ); // URL to small image. $form['details']['image_settings']['small_url'] = array( '#type' => 'item', '#title' => t('Unpeeled image'), '#markup' => check_plain($pageear->small_url), ); $form['details']['image_settings']['small_url_upload'] = array( '#type' => 'file', '#description' => t('Upload an image or flash movie to show when the ad is not peeled (JPG, GIF, PNG or SWF, Flag Width x Flag Height pixels).'), '#tree' => FALSE, '#size' => 40, ); // URL to big image. $form['details']['image_settings']['big_url'] = array( '#type' => 'item', '#title' => t('Peeled image'), '#markup' => check_plain($pageear->big_url), ); $form['details']['image_settings']['big_url_upload'] = array( '#type' => 'file', '#description' => t('Upload an image or flash movie to show when the ad is peeled (JPG, GIF, PNG or SWF, Peel Width x Peel Height pixels).'), '#tree' => FALSE, '#size' => 40, ); // Mirror image ( true | false ). $form['details']['image_settings']['mirror'] = array( '#type' => 'checkbox', '#title' => t('Back mirror'), '#default_value' => $pageear->mirror, '#description' => t('Mirror the ad on the back of the peeled page.'), ); // In Transition for pageear. $form['details']['image_settings']['in_transition'] = array( '#type' => 'select', '#title' => t('In Transition'), '#description' => t('In Transition for the pageear.'), '#options' => pageear_get_options('in_transition'), '#default_value' => $pageear->in_transition, '#required' => TRUE, ); // Duration of in transition (1-9). $form['details']['image_settings']['transition_duration'] = array( '#type' => 'hidden', ); $form['details']['image_settings']['transition_duration_slider'] = array( '#type' => 'item', '#title' => t('Transition Duration'), '#description' => t('Duration of in transition. (1-9)'), '#required' => TRUE, '#markup' => '
TRUE
(PHP-mode, experts only).');
$description .= ' '. t('If the PHP-mode is chosen, enter PHP code between %php. Note that executing incorrect PHP-code can break your Drupal site.', array('%php' => ''));
}
$form['visibility_settings']['page_vis_settings']['visibility'] = array(
'#type' => 'radios',
'#title' => t('Show pageear on specific pages'),
'#options' => $options,
'#default_value' => $pageear->visibility,
);
$form['visibility_settings']['page_vis_settings']['pages'] = array(
'#type' => 'textarea',
'#title' => t('Pages'),
'#default_value' => $pageear->pages,
'#description' => $description,
);
}
if ($op == 'edit') {
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
);
$form['cancel'] = array(
'#markup' => l(t('Cancel'), $_SERVER['HTTP_REFERER']),
);
}
elseif ($op == 'add' || $op == 'clone') {
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
'#submit' => array('pageear_admin_add_submit'),
);
$form['cancel'] = array(
'#markup' => l(t('Cancel'), $_SERVER['HTTP_REFERER']),
);
}
return $form;
}
/**
* hook_validate: validation function for the pageear configuration form.
*/
function pageear_admin_edit_validate($form, &$form_state) {
// Sizes validation
// TODO Review if I must restrict this more. For example test the ratio peel/flag.
if (!is_numeric($form_state['values']['wait_width']) || $form_state['values']['wait_width'] < 10) {
form_set_error('wait_width', t('Wait Icon Width must be a number greater than 10'));
}
if (!is_numeric($form_state['values']['wait_height']) || $form_state['values']['wait_height'] < 10) {
form_set_error('wait_height', t('Wait Icon Height must be a number greater than 10'));
}
if (!is_numeric($form_state['values']['flag_width']) || $form_state['values']['flag_width'] < 10) {
form_set_error('flag_width', t('Flag Width must be a number greater than 10'));
}
if (!is_numeric($form_state['values']['flag_height']) || $form_state['values']['flag_height'] < 10) {
form_set_error('flag_height', t('Flag Height must be a number greater than 10'));
}
if (!is_numeric($form_state['values']['peel_width']) || $form_state['values']['peel_width'] < 50) {
form_set_error('peel_width', t('Peel Width must be a number greater than 50'));
}
if (!is_numeric($form_state['values']['peel_height']) || $form_state['values']['peel_height'] < 50) {
form_set_error('peel_height', t('Peel Height must be a number greater than 50'));
}
// Duration of transition validation
if (!is_numeric($form_state['values']['transition_duration']) || $form_state['values']['transition_duration'] == 0) {
form_set_error('transition_duration', t('Transition duration must be a number between 1 and 9'));
}
// Peel custom color validation.
// If user sets color manually with input textfield wrong hex values would be ignored by conversion function.
$custom_rgb_peel_color = hex2rgb($form_state['values']['custom_color']);
form_set_value($form['details']['image_settings']['red_value'], $custom_rgb_peel_color[0], $form_state);
form_set_value($form['details']['image_settings']['green_value'], $custom_rgb_peel_color[1], $form_state);
form_set_value($form['details']['image_settings']['blue_value'], $custom_rgb_peel_color[2], $form_state);
// Flag speed validation
if (!is_numeric($form_state['values']['flag_speed']) || $form_state['values']['flag_speed'] == 0) {
form_set_error('flag_speed', t('Flag motion speed must be a number between 1 and 9'));
}
// Peel speed validation
if (!is_numeric($form_state['values']['peel_speed']) || $form_state['values']['peel_speed'] == 0) {
form_set_error('peel_speed', t('Peel motion speed must be a number between 1 and 9'));
}
// AutomaticOpen validation
if (!is_numeric($form_state['values']['automatic_open']) || $form_state['values']['automatic_open'] < 0) {
form_set_error('automatic_open', t('Automatically peel onload seconds must be a number between 0 and 999'));
}
// AutomaticClose validation
if (!is_numeric($form_state['values']['automatic_close']) || $form_state['values']['automatic_close'] < 0) {
form_set_error('automatic_close', t('Automatically peel onclose seconds must be a number between 0 and 999'));
}
// Close button color validation
// If user sets color manually with input textfield wrong hex values would be ignored by conversion function.
$custom_rgb_close_button_color = hex2rgb($form_state['values']['close_button_color']);
form_set_value($form['details']['closebutton_settings']['close_red_value'], $custom_rgb_close_button_color[0], $form_state);
form_set_value($form['details']['closebutton_settings']['close_green_value'], $custom_rgb_close_button_color[1], $form_state);
form_set_value($form['details']['closebutton_settings']['close_blue_value'], $custom_rgb_close_button_color[2], $form_state);
// Text on close button
if (preg_match("/^[a-z][a-z ]+$/", $form_state['values']['text_on_close_button']) == 0) {
form_set_error('text_on_close_button', t('Only lowercase letters and spaces are allowed for text on close button. It must start with a lowercase letter too.'));
}
// Code from here is to validate file uploads.
$directory_path = 'public://'. PAGEEAR_PATH_IMAGES;
// checks if directory exist and creates it if it don't
//file_prepare_directory($directory_path, FILE_CREATE_DIRECTORY);
file_prepare_directory($directory_path, FILE_CREATE_DIRECTORY);
if ($file = file_save_upload('wait_url_upload')) { //, array('file_validate_is_image' => array()))) {
$parts = pathinfo($file->filename);
if ($parts['extension'] != 'jpeg' && $parts['extension'] != 'jpg' && $parts['extension'] != 'gif' && $parts['extension'] != 'png') {
file_delete($file);
form_set_error('wait_url_upload', t('Uploaded file must be an image. JPEG, PNG or GIF'));
}
else {
$dest_filename = 'wait_url_'. $form_state['values']['peid'] .'.'. $parts['extension'];
$dest_uri = $directory_path .'/'. $dest_filename;
$source_path = $file->uri;
if ($dest_uri = file_unmanaged_copy($source_path, $dest_uri)) {
file_delete($file);
form_set_value($form['details']['wait_settings']['wait_url'], file_directory_path() .'/'. file_uri_target($dest_uri), $form_state);
}
else{
file_delete($file);
form_set_error('wait_url_upload', t('Wait icon upload failed. Please try again.'));
}
}
}
else {
form_set_value($form['details']['wait_settings']['wait_url'], $form['details']['wait_settings']['wait_url']['#markup'], $form_state);
}
// uploads images from image upload fields
foreach (array('small_url', 'big_url') as $image) {
if ($file = file_save_upload($image .'_upload')) { //, array('file_validate_is_image' => array()))) {
$parts = pathinfo($file->filename);
if ($parts['extension'] != 'jpeg' && $parts['extension'] != 'jpg' && $parts['extension'] != 'gif' && $parts['extension'] != 'png' && $parts['extension'] != 'swf') {
file_delete($file);
form_set_error($image .'_upload', t('Uploaded file must be an image or flash movie. JPEG, PNG, GIF or SWF'));
}
else {
$dest_filename = $directory_path .'/'. $image .'_'. $form_state['values']['peid'] .'.'. $parts['extension'];
$dest_uri = $directory_path .'/'. $dest_filename;
$source_path = $file->uri;
if ($dest_uri = file_unmanaged_copy($source_path, $dest_uri)) {
file_delete($file);
form_set_value($form['details']['image_settings'][$image], file_directory_path() .'/'. file_uri_target($dest_uri), $form_state);
}
else{
file_delete($file);
form_set_error($image .'_upload', t('Media uploaded failed. Please try again.'));
}
}
}
else {
form_set_value($form['details']['image_settings'][$image], $form['details']['image_settings'][$image]['#markup'], $form_state);
}
}
// uploads images from sound upload fields
foreach (array('load_sound_url', 'open_sound_url', 'close_sound_url') as $sound) {
if ($file = file_save_upload($sound .'_upload')) {
$parts = pathinfo($file->filename);
if ($parts['extension'] != 'mp3') {
file_delete($file);
form_set_error($sound .'_upload', t('Uploaded sound must be a MP3 file.'));
}
else {
$dest_filename = $sound .'_'. $form_state['values']['peid'] .'.'. $parts['extension'];
$dest_uri = $directory_path .'/'. $dest_filename;
$source_path = $file->uri;
if ($dest_uri = file_unmanaged_copy($source_path, $dest_uri)) {
file_delete($file);
form_set_value($form['details']['sound_settings'][$sound], file_directory_path() .'/'. file_uri_target($dest_uri), $form_state);
}
else {
file_delete($file);
form_set_error($sound .'_upload', t('Sound uploaded failed. Please try again.'));
}
}
}
else {
form_set_value($form['details']['sound_settings'][$sound], $form['details']['sound_settings'][$sound]['#markup'], $form_state);
}
}
// Remove sounds checked for removal
// TODO remove files too or wait till they are removed when the pageear is removed?
if ($form_state['values']['load_sound_url'] != '' && $form_state['values']['load_sound_remove']) {
form_set_value($form['details']['sound_settings']['load_sound_url'], '', $form_state);
}
if ($form_state['values']['open_sound_url'] != '' && $form_state['values']['open_sound_remove']) {
form_set_value($form['details']['sound_settings']['open_sound_url'], '', $form_state);
}
if ($form_state['values']['open_sound_url'] != '' && $form_state['values']['close_sound_remove']) {
form_set_value($form['details']['sound_settings']['close_sound_url'], '', $form_state);
}
}
/**
* hook_submit: submit function for the pageear configuration form.
*/
function pageear_admin_edit_submit($form, &$form_state) {
// Save the edited pageear
if (!form_get_errors()) {
if (module_exists('locale')) {
$languages = implode(',', $form_state['values']['languages']);
}
else {
$languages = '';
}
$roles = implode(',', $form_state['values']['roles']);
//$new_pageear = new stdClass();
$new_pageear->peid = $form_state['values']['peid'];
$new_pageear->status = $form_state['values']['status'];
$new_pageear->name = $form_state['values']['name'];
$new_pageear->peel_position = $form_state['values']['peel_position'];
$new_pageear->peel_position_model = $form_state['values']['peel_position_model'];
$new_pageear->wait_enable = $form_state['values']['wait_enable'];
$new_pageear->wait_width = $form_state['values']['wait_width'];
$new_pageear->wait_height = $form_state['values']['wait_height'];
$new_pageear->wait_url = $form_state['values']['wait_url'];
$new_pageear->flag_style = $form_state['values']['flag_style'];
$new_pageear->peel_style = $form_state['values']['peel_style'];
$new_pageear->flag_width = $form_state['values']['flag_width'];
$new_pageear->flag_height = $form_state['values']['flag_height'];
$new_pageear->peel_width = $form_state['values']['peel_width'];
$new_pageear->peel_height = $form_state['values']['peel_height'];
$new_pageear->small_url = $form_state['values']['small_url'];
$new_pageear->big_url = $form_state['values']['big_url'];
$new_pageear->mirror = $form_state['values']['mirror'];
$new_pageear->in_transition = $form_state['values']['in_transition'];
$new_pageear->transition_duration = $form_state['values']['transition_duration'];
$new_pageear->peel_color_style = $form_state['values']['peel_color_style'];
$new_pageear->peel_color = $form_state['values']['peel_color'];
$new_pageear->red_value = $form_state['values']['red_value'];
$new_pageear->green_value = $form_state['values']['green_value'];
$new_pageear->blue_value = $form_state['values']['blue_value'];
$new_pageear->link_enable = $form_state['values']['link_enable'];
$new_pageear->link_target = $form_state['values']['link_target'];
$new_pageear->link = $form_state['values']['link'];
$new_pageear->load_sound_url = $form_state['values']['load_sound_url'];
$new_pageear->open_sound_url = $form_state['values']['open_sound_url'];
$new_pageear->close_sound_url = $form_state['values']['close_sound_url'];
$new_pageear->flag_speed = $form_state['values']['flag_speed'];
$new_pageear->peel_speed = $form_state['values']['peel_speed'];
$new_pageear->automatic_open = $form_state['values']['automatic_open'];
$new_pageear->automatic_close = $form_state['values']['automatic_close'];
$new_pageear->close_button_enable = $form_state['values']['close_button_enable'];
$new_pageear->text_on_close_button = $form_state['values']['text_on_close_button'];
$new_pageear->close_red_value = $form_state['values']['close_red_value'];
$new_pageear->close_green_value = $form_state['values']['close_green_value'];
$new_pageear->close_blue_value = $form_state['values']['close_blue_value'];
$new_pageear->languages = $languages;
$new_pageear->roles = $roles;
$new_pageear->visibility = $form_state['values']['visibility'];
$new_pageear->pages = $form_state['values']['pages'];
drupal_write_record('pageears', $new_pageear, 'peid');
// Update the persistent variable numEnabledPageears that tracks number of pageears enabled
if ($form_state['values']['status'] != $form_state['values']['previous_status']) {
if ($form_state['values']['status']) {
variable_set('num_enabled_pageears', variable_get('num_enabled_pageears', 0) + 1);
}
else {
variable_set('num_enabled_pageears', variable_get('num_enabled_pageears', 1) - 1);
}
}
drupal_set_message(t('The pageear %name has been saved.', array('%name' => $form_state['values']['name'])));
$form_state['redirect'] = 'admin/structure/pageear';
}
return;
}
/**
* hook_submit: when adding new pageear.
*/
function pageear_admin_add_submit($form, &$form_state) {
if (module_exists('locale')) {
$languages = implode(',', $form_state['values']['languages']);
}
else {
$languages = '';
}
$roles = implode(',', $form_state['values']['roles']);
// Writes new pageear
//$new_pageear = new stdClass();
$new_pageear->status = $form_state['values']['status'];
$new_pageear->name = $form_state['values']['name'];
$new_pageear->peel_position = $form_state['values']['peel_position'];
$new_pageear->peel_position_model = $form_state['values']['peel_position_model'];
$new_pageear->wait_enable = $form_state['values']['wait_enable'];
$new_pageear->wait_width = $form_state['values']['wait_width'];
$new_pageear->wait_height = $form_state['values']['wait_height'];
$new_pageear->wait_url = $form_state['values']['wait_url'];
$new_pageear->flag_style = $form_state['values']['flag_style'];
$new_pageear->peel_style = $form_state['values']['peel_style'];
$new_pageear->flag_width = $form_state['values']['flag_width'];
$new_pageear->flag_height = $form_state['values']['flag_height'];
$new_pageear->peel_width = $form_state['values']['peel_width'];
$new_pageear->peel_height = $form_state['values']['peel_height'];
$new_pageear->small_url = $form_state['values']['small_url'];
$new_pageear->big_url = $form_state['values']['big_url'];
$new_pageear->mirror = $form_state['values']['mirror'];
$new_pageear->in_transition = $form_state['values']['in_transition'];
$new_pageear->transition_duration = $form_state['values']['transition_duration'];
$new_pageear->peel_color_style = $form_state['values']['peel_color_style'];
$new_pageear->peel_color = $form_state['values']['peel_color'];
$new_pageear->red_value = $form_state['values']['red_value'];
$new_pageear->green_value = $form_state['values']['green_value'];
$new_pageear->blue_value = $form_state['values']['blue_value'];
$new_pageear->link_enable = $form_state['values']['link_enable'];
$new_pageear->link_target = $form_state['values']['link_target'];
$new_pageear->link = $form_state['values']['link'];
$new_pageear->load_sound_url = $form_state['values']['load_sound_url'];
$new_pageear->open_sound_url = $form_state['values']['open_sound_url'];
$new_pageear->close_sound_url = $form_state['values']['close_sound_url'];
$new_pageear->flag_speed = $form_state['values']['flag_speed'];
$new_pageear->peel_speed = $form_state['values']['peel_speed'];
$new_pageear->automatic_open = $form_state['values']['automatic_open'];
$new_pageear->automatic_close = $form_state['values']['automatic_close'];
$new_pageear->close_button_enable = $form_state['values']['close_button_enable'];
$new_pageear->text_on_close_button = $form_state['values']['text_on_close_button'];
$new_pageear->close_red_value = $form_state['values']['close_red_value'];
$new_pageear->close_green_value = $form_state['values']['close_green_value'];
$new_pageear->close_blue_value = $form_state['values']['close_blue_value'];
$new_pageear->languages = $languages;
$new_pageear->roles = $roles;
$new_pageear->visibility = $form_state['values']['visibility'];
$new_pageear->pages = $form_state['values']['pages'];
drupal_write_record('pageears', $new_pageear);
// Move any images or sounds
// to reflect the peid on its names
//$directory_path = file_directory_path() .'/'. PAGEEAR_PATH_IMAGES;
$directory_path = 'public://'. PAGEEAR_PATH_IMAGES;
//$change_new_pageear = new stdClass();
$change_new_pageear->peid = $new_pageear->peid;
unset($new_pageear);
foreach (array('wait_url', 'small_url', 'big_url', 'load_sound_url', 'open_sound_url', 'close_sound_url') as $image_sound) {
if ($form_state['values'][$image_sound] != '') {
$source_filepath = $form_state['values'][$image_sound];
$stringparts = explode(".", $form_state['values'][$image_sound]);
$extension = $stringparts[count($stringparts)-1];
$dest_filename = $image_sound .'_'. $change_new_pageear->peid .'.'. $extension;
$dest_uri = $directory_path .'/'. $dest_filename;
$dest_uri = file_unmanaged_copy($source_filepath, $dest_uri, FILE_EXISTS_REPLACE);
$change_new_pageear->$image_sound = file_directory_path() .'/'. file_uri_target($dest_uri);
}
}
// Update the pageear with moved images and sounds.
drupal_write_record('pageears', $change_new_pageear, 'peid');
// Delete possible images sounds uploaded with peid=0
delete_images_sounds(0);
// Update the persistent variable numEnabledPageears that tracks number of pageears enabled
if ($form_state['values']['status']) {
variable_set('num_enabled_pageears', variable_get('num_enabled_pageears', 0) + 1);
}
drupal_set_message(t('The pageear %name has been created.', array('%name' => $form_state['values']['name'])));
$form_state['redirect'] = 'admin/structure/pageear';
return;
}
/**
* Menu callback: disable a specific pageear.
*/
function pageear_admin_disable($pageear) {
db_query("UPDATE {pageears} SET status = 0 WHERE peid = :peid", array(':peid' => $pageear->peid));
// Update the persistent variable numEnabledPageears that tracks number of pageears enabled
variable_set('num_enabled_pageears', variable_get('num_enabled_pageears', 1) - 1);
drupal_goto('admin/structure/pageear');
}
/**
* Menu callback: enable a specific pageear.
*/
function pageear_admin_enable($pageear) {
db_query("UPDATE {pageears} SET status = 1 WHERE peid = :peid", array(':peid' => $pageear->peid));
// Update the persistent variable numEnabledPageears that tracks number of pageears enabled
variable_set('num_enabled_pageears', variable_get('num_enabled_pageears', 0) + 1);
drupal_goto('admin/structure/pageear');
}
/**
* Deletes images and sounds for specific pageear.
*/
function delete_images_sounds($peid) {
//$directory_path = file_directory_path() .'/'. PAGEEAR_PATH_IMAGES;
$directory_path = 'public://'. PAGEEAR_PATH_IMAGES;
// if the directory where files reside does not exists nothing to do
if (!file_prepare_directory($directory_path)) {
return;
}
foreach (array('wait_url', 'small_url', 'big_url', 'load_sound_url', 'open_sound_url', 'close_sound_url') as $image_sound) {
if ($image_sound == 'wait_url' || $image_sound == 'small_url' || $image_sound == 'big_url') {
$extensions = array('.jpg', '.jpeg', '.gif', '.png', '.swf');
}
else {
$extensions = array('.mp3');
}
foreach ($extensions as $extension) {
$filename_noext = $image_sound .'_'. $peid;
$file_uri_noext = $directory_path .'/'. $filename_noext;
$file_uri = $file_uri_noext . $extension;
file_unmanaged_delete($file_uri);
$i = 0;
$file_uri = $file_uri_noext .'_'. $i . $extension;
while (file_exists(drupal_realpath($file_uri))) {
file_unmanaged_delete($file_uri);
$i += 1;
$file_uri = $file_uri_noext .'_'. $i . $extension;
}
}
}
}
/**
* Menu callback; confirm deletion of a pageear.
*/
function pageear_admin_delete($form, &$form_state, $pageear) {
$form['pageear'] = array('#type' => 'hidden', '#value' => serialize($pageear));
$params = array('%name' => $pageear->name);
return confirm_form($form, t('Are you sure you want to delete the pageear %name?', $params), 'admin/structure/pageear', NULL, t('Delete'), t('Cancel'));
}
function pageear_admin_delete_submit($form, &$form_state) {
$pageear = unserialize($form_state['values']['pageear']);
//db_query('DELETE FROM {pageears} WHERE peid = %d', array(':peid' => $pageear->peid));
db_delete('pageears')
->condition('peid', $pageear->peid)
->execute();
delete_images_sounds($pageear->peid);
drupal_set_message(t('The pageear %name has been removed.', array('%name' => $pageear->name)));
$form_state['redirect'] = 'admin/structure/pageear';
return;
}
/**
* Helper functions for color conversions
*/
function hex2rgb($color) {
if ($color[0] == '#') {
$color = drupal_substr($color, 1);
}
if (drupal_strlen($color) == 6) {
list($r, $g, $b) = array(
$color[0] . $color[1],
$color[2] . $color[3],
$color[4] . $color[5]
);
}
elseif (drupal_strlen($color) == 3) {
list($r, $g, $b) = array(
$color[0] . $color[0],
$color[1] . $color[1],
$color[2] . $color[2]
);
}
else {
return array(0, 0, 0);
}
$r = hexdec($r);
$g = hexdec($g);
$b = hexdec($b);
return array($r, $g, $b);
}
function rgb2hex($r, $g=-1, $b=-1) {
if (is_array($r) && sizeof($r) == 3) {
list($r, $g, $b) = $r;
}
$r = intval($r);
$g = intval($g);
$b = intval($b);
$r = dechex($r<0?0:($r>255?255:$r));
$g = dechex($g<0?0:($g>255?255:$g));
$b = dechex($b<0?0:($b>255?255:$b));
$color = (drupal_strlen($r) < 2?'0':'') . $r;
$color .= (drupal_strlen($g) < 2?'0':'') . $g;
$color .= (drupal_strlen($b) < 2?'0':'') . $b;
return '#'. $color;
}