I figured this little snippet of code might be useful to those who are looking for a way to add/remove banners from the rotation without using FTP or SFTP. It adds the ability to upload a file to the banner repository as well as delete existing banners.

This is my modified code for the theme-settings.php file:


// custom theme settings: dropdown menu, css image preload

function phptemplate_settings($saved_settings) {

  $defaults = array(
    'menutype' => 0,
    'cssPreload' =>0
  );


  $settings = array_merge($defaults, $saved_settings);

	  $form['menutype'] = array(
	    '#type' => 'select',
	    '#title' => t('Do you want to use static or drop down Primary Links ?'),
	    '#default_value' => $settings['menutype'],
	    '#options' => array(
		0 => 'static',
		1 => 'drop down',
		)
	  );
	  
	  $form['cssPreload'] = array(
	    '#type' => 'radios',
	    '#title' => t('Do you want to use image preload ?'),
	    '#default_value' => $settings['cssPreload'],
	    '#options' => array(
		0 => 'no',
		1 => 'yes',
		)
	  );
      
      /**
       * Banner addition/deletion
       * Added by Timothy Trinidad 3/11/09
       */


      $form['#attributes'] = array('enctype' => "multipart/form-data");
      
      $form['add_banner'] = array(
        '#type' => 'file',
        '#title' => 'Upload Banner File',
        '#element_validate' => array('element_add_banner_validate'),
        '#description' => 'Upload an image for the site banner.  This image will rotate with other existing banners.  The space for banners is 970x200 pixels.',
      );
      
      // Delete banners functionality
      // Pass a snapshot of the files so that we can use array indexes since
      // form IDs can't use filenames
      $banners_files = file_scan_directory(file_create_path('banners'),'.*');                                   
      $form['banners_files'] = array(
        '#type' => 'hidden',
        '#value' => serialize(array_values($banners_files)),
      );
      $form['delete_banners'] = array(
        '#type' => 'fieldset',
        '#tree' => 1,
        '#title' => 'Delete Existing Banners',
        '#description' => 'Check the boxes next to the banners you would like to remove. This action cannot be undone.',
      );
      foreach($banners_files as $path => $attr){
            $form['delete_banners'][] = array(
                '#type' => 'checkbox',
                '#title' => '<img src="'.file_create_url($path).'" alt="'.$attr->name.'" width="200" />',
                '#element_validate' => array('element_delete_banner_validate'),
            );
      }
  
      return $form;                                                                         
}

// Save the uploaded banner file
function element_add_banner_validate($element, &$form_state){
    if($file = file_save_upload('add_banner', array('file_validate_is_image' => array()))){
        file_copy($file, 'banners', FILE_EXISTS_REPLACE);
    }
}

// Delete selected banners
function element_delete_banner_validate($element, &$form_state){
    static $banners_files = NULL;
    if(is_null($banners_files))
        $banners_files = unserialize($form_state['values']['banners_files']);
    
    if($element['#value']){ // if checked, delete the file
        $file = $banners_files[array_pop($element['#parents'])]->filename;
        if(!file_delete($file)){
            form_set_error('', 'There was a problem deleting the file '.$file.'.  Please check the file and folder permissions and try again.');   
        }
    }
}

Comments

tborrome’s picture

great feature!
is this going to be added to the theme for the next release?

tborrome’s picture

Hi I applied your snippet but it couldnt find the banner files. Your code seems to be pointed to the /sites/default/files/banners directory but the banners are in the sites/all/themes.marinelli/img/banners subdirectory? how did you make the theme point to /sites/default/files/banners for your banners?

benlotter’s picture

I'm also trying to create this functionality but with a different approach. So far I haven't had any success.

A) Repoint rotate.php to a /files/banners.
B) Create a content-type of Banner and save the images (CCK field) to /files/banners

I can't seem to get /sites/mysite.com/themes/marinelli/img/banners/rotate.php to point to /sites/mysite.com/files/banners. The OpenDir() and ReadDir() keep populating the errorlog with can't find the specified directory. I've tried every combination I can think of.

As a variation of the above I've also tried to keep rotate.php in the new /files/banners diretory but point graphics.css to the new rotate.php. This doesn't work either.

Any ideas? I have everything else working to upload the banners and ensure the proper size (imagecache...what else).

ramontravels’s picture

as a temporary fix, in order to not have banners, I basically set the header height to 0.
I went to the layout.css file in the marinelli folder and set the header which was previously 180 to basically 0.

I have a recipe website, and I loved the marinelli theme but just wanted to have the images for a particular recipe (not random banners)... This way I could just upload images separately for each menu link that corresponds to the content.

oadaeh’s picture

This issue is being closed because it is against a branch for a version of Drupal that is no longer supported.
If you feel that this issue is still valid, feel free to re-open and update it (and any possible patch) to work with the 7.x-4.x branch.
Thank you.

oadaeh’s picture

Status: Active » Closed (won't fix)