File: D:/HostingSpaces/SBogers10/spire.komma-mediadesign.nl/wwwroot/kms/client/controllers/c_banners.php
<?php
/*
c_awards.php // Controller
First checks if any actions need to be and then guides user to the right page.
*/
require_once DOCUMENT_ROOT.'client/models/m_banners.php';
require_once DOCUMENT_ROOT.'app/models/m_display.php';
require_once DOCUMENT_ROOT.'app/models/m_dataSession.php';
require_once DOCUMENT_ROOT.'app/models/m_sorter.php';
require_once DOCUMENT_ROOT.'app/models/images/m_imageHandler.php';
/**
*
* Check if the "sub" GET variable is set
*/
if(defined('URL_SUB')) $sub = URL_SUB;
if(defined('URL_SUB2')) $sub2 = URL_SUB2;
/**
*
* Set a few hardcoded variables for this page
*/
$linkname = 'homebanners';
$pageId = 14;
$this->setdata('page_label', $this->lang['awards'], TRUE);
/**
*
* Image dimensions are set by an array( largeWidth(int), largeHeight(int), thumbwidth(int), $thumbHeight(int), kmsThumb(int), kmsThumb(int));
* A standard is set, but for every page we can set a different array.
*/
$std = array('lw' => 2560, 'lh' => null, 'tw' => 1440, 'th' => null, 'mw' => 60, 'mh' => 60);
switch($pageId)
{
default:
$imgDim = $std;
}
/**
*
* Add objects
*/
$BannerPage = new BannerPage();
$DataSession = new DataSession();
$ImageHandler = new ImageHandler($pageId,$imgDim);
$ImageHandler->setMaxImages(1);
$ImageStorage = new ImageStorage($pageId);
/***********************************************************************************/
/*
Check if any actions are needed
*/
/*
ADD / EDIT
*/
/**
*
* If user hits the "save" button. We call the Newspage::store function to validate and save the values.
* When this method returns false, we check what went wrong and display this errors to the user.
* When the method returns true, storage is succes so redirect to "dashboard".
*/
if(isset($_POST['sbm_save_new']))
{
if($id = $BannerPage->store())
{
if($ImageStorage->store($id, $_SESSION[$linkname . '_images_to_store']))
{
unset($_SESSION[$linkname . '_images_to_store']);
}
if(isset($_POST['x2'])) $this->redirect(LANG_ROOT.$linkname.'/x2/');
$this->redirect(LANG_ROOT.$linkname.'/');
}
else
{
foreach($BannerPage->getErrors() as $error)
{
$this->setAlert($error,'error');
}
$this->redirect(LANG_ROOT.$linkname.'/nieuw-item/');
}
}
/**
*
* If the user hits the "save" button in the "edit-form", or the image "upload" is submitted, two things can happen:
* We need to save the item, or we need to add images
* First save all data which was already filled, so the user does not have to refill these.
* If addImages returns false, we dont have to add images, so we need to update the item.
* We also need to make sure that images that are no longer in the session, should be removed from the database and the server.
* If item stored we return to the "dashboard", if images added we return to the "edit-form".
*/
else if(isset($_POST['sbm_save_edit']))
{
if(isset($sub2))
{
$id = $sub2;
if($BannerPage->store($id))
{
$ImageHandler->removeStoredImages($id);
if($ImageStorage->store($id, $_SESSION[$linkname . '_images_to_store']))
{
unset($_SESSION[$linkname . '_images_to_store']);
}
if(isset($_POST['x2'])) $this->redirect(LANG_ROOT.$linkname.'/x2/');
$this->redirect(LANG_ROOT.$linkname.'/');
}
else
{
foreach($BannerPage->getErrors() as $error)
{
$this->setAlert($error,'error');
}
$this->redirect(LANG_ROOT.$linkname.'/wijzig-item/'.$id.'/');
}
}
}
/**
*
* Validate images when the upload button is hit.
*
*/
else if( isset($_FILES['images']) )
{
$DataSession->savePost();
if( ! $ImageHandler->addImages())
{
// error handling ?
}
$this->returnToSessionUrl();
}
/*
REMOVE TO TRASH
*/
/**
*
* If the user hits the submit button to delete items, first thing we need to do is ask the user if he is sure about this.
* We grap an data array through the MealCategory::Get method.
* A string of list items is created and stored in the Template::data property.
* Also an array ids is stored.
*/
else if(isset($_POST['prepare_delete_items']))
{
if(isset($_POST['selected']))
{
if($data = $BannerPage->get($_POST['selected']))
{
$Display = new Display();
list($output, $ids) = $Display->createList($data, FALSE);
$this->setData('item_list', $output, TRUE);
$this->setData('list_ids', $ids, TRUE);
}
}
else
{
$this->setAlert($this->lang['general_nothing_selected'],'warning');
$this->redirect(LANG_ROOT.$linkname.'/');
}
}
/**
*
* If the user hits the button to confirm he wants to delete something, we add a new Trashcan object.
*/
else if(isset($_POST['sbm_delete_items']))
{
$ids = explode(',',$_POST['ids']);
$Trashcan = new Trashcan();
$Trashcan->setPageInfo(array('id'=>$pageId,'linkname'=>'categorien','label'=>'Categoriën'));
$Trashcan->insert($ids);
$this->redirect(LANG_ROOT.'/'.$linkname);
}
/*
CHANGE ORDER OF ITEMS
*/
/**
*
* When the user hits the order button in the order (items) form. An array of ids is grapped from the comma-seperated string in the order form.
* A new sorter object is created, this updates the order. We redirect back to the "dashboard".
*/
else if(isset($_POST['sbm_order_items']))
{
$ids = explode(',',$_POST['sort_order']);
$tn = $BannerPage->getTableName();
$Sorter = new Sorter($pageId, $tn, 'itemOrder');
$Sorter->update($ids);
$this->redirect(LANG_ROOT.$linkname.'/');
}
/*
ADD CROPPED IMAGES
*/
/**
*
* If the user hits the button submit after cropping his images. The imagesHandler calles the method ImageHandler::addCroppedImages.
* The images will be added to the current session "images_to_store".
*/
else if(isset($_POST['sbm_crop']))
{
$ImageHandler->addCroppedImages();
$this->returnToSessionUrl();
}
/**
*
* Check if there are any actions needed when a subpage is found
*/
else if(isset($sub))
{
switch($sub)
{
/**
*
* Show list of images in session
*/
case 'nieuw-item':
$_SESSION['return_to']['url'] = 'nieuw-item';
$ImageHandler->createList();
break;
/**
*
* When we are going to edit an item. The storage session needs to be filled with data from the database.
*/
case 'wijzig-item':
$_SESSION['return_to']['url'] = 'wijzig-item';
$_SESSION['return_to']['id'] = $sub2;
if(isset($sub2))
{
$BannerPage->setStorageData($sub2);
// add stored images to session (only once)
if(!isset($_SESSION['stored_added']))
{
if($arr = $ImageStorage->get('itemId',$sub2))
{
$session = $_SESSION[$linkname . '_images_to_store'];
$firstkey = key($arr);
if(is_array($arr[$firstkey]))
{
foreach($arr as $data)
{
$session->add($data);
}
}
else
{
$session->add($arr);
}
}
$_SESSION['stored_added'] = TRUE;
}
}
$ImageHandler->createList();
break;
/**
*
* This is the page where the user is asked to confirm his wish to delete (one) item.
* In order to show what item he clicked on, we get a the info by the id
*/
case 'verwijder-item':
if($item = $BannerPage->get(array($sub2)))
{
$Display = new Display();
list($output, $ids) = $Display->createList($item, FALSE);
$this->setData('item_list', $output, TRUE);
$this->setData('list_ids', $ids, TRUE);
}
else
{
$this->redirect(LANG_ROOT.$linkname.'/');
}
break;
/**
*
* This is the page where we change the order of items.
* First we get options to pass into het Multipage::get method. (The zero stands for no page 'LIMIT', so you get a list of all items)
* Then add a new sorter object and retrieve a list.
*/
case 'wijzig-volgorde':
$BannerPage->setMaxItems(0);
$items = $BannerPage->get();
$sorter = new Sorter($pageId);
$sorter->createList($items);
break;
/**
*
* This happens when the user clicks on the X to delete an image from the session.
* The session key is passed through $sub2. Redirect back to the page where the user came from. (for example "add-form" or "edit-form").
* Check this width the return_to session.
*/
case 'remove-image':
$DataSession->savePost();
if(isset($sub2) && is_numeric($sub2))
{
$_SESSION[$linkname . '_images_to_store']->remove($sub2);
}
$this->returnToSessionUrl();
break;
/**
*
* When a user is about to crop some images. We have to set the cropdata first.
*/
case 'crop':
foreach($_SESSION['images_to_crop'] as $key => $value)
{
$this->setdata('minW'.$key,$imgDim['tw']);
$this->setdata('minH'.$key,$imgDim['th']);
$this->setdata('maxW'.$key,0);
$this->setdata('maxH'.$key,0);
}
break;
case 'x2':
$data = $BannerPage->get();
$Display = new Display();
list($output, $ids) = $Display->createList($data);
$this->setData('item_list', $output, TRUE);
$this->setData('list_ids', $ids, TRUE);
$BannerPage->clean();
$ImageHandler->clean();
unset($_SESSION['stored_added']);
break;
}
}
/**
*
* Code below is used for the dashboard of the news page
* A string of list items is created and stored in the Template::data property.
* Also an array ids is stored.
*/
else
{
$data = $BannerPage->get();
$Display = new Display();
list($output, $ids) = $Display->createList($data);
$this->setData('item_list', $output, TRUE);
$this->setData('list_ids', $ids, TRUE);
$BannerPage->clean();
$ImageHandler->clean();
unset($_SESSION['stored_added']);
}
/***********************************************************************************/
/*
Guide user to page
*/
/**
*
* If a subpage is found, switch this and guide the user to the right page
* When no page found, header a 404-error and display a nice message to the user.
*/
if(isset($sub))
{
switch($sub)
{
case 'nieuw-item':
// form add item
$this->load(DOCUMENT_ROOT.'client/views/banners/v_banners_addedit.php', $this->lang['new_award'].' | '.$this->lang['homeBanner'].' | '.SITE_NAME);
break;
case 'wijzig-item':
// form edit item
$this->load(DOCUMENT_ROOT.'client/views/banners/v_banners_addedit.php', $this->lang['edit_award'].' | '.$this->lang['homeBanner'].' | '.SITE_NAME);
break;
case 'verwijder-items':
// ask user confirmation (delete multiple items)
$this->load(DOCUMENT_ROOT.'app/views/trashcan/v_kms_trashcan_delete.php', $this->lang['delete_awards'].' | '.$this->lang['homeBanner'].' | '.SITE_NAME);
break;
case 'verwijder-item':
// ask user confirmation (delete 1 item)
$this->load('app/views/trashcan/v_kms_trashcan_delete.php', $this->lang['delete_award'].' | '.$this->lang['homeBanner'].' | '.SITE_NAME);
break;
case 'wijzig-volgorde':
// sortable list
$this->load('app/views/sortorder/v_kms_order_items.php', $this->lang['order_awards'].' | '.$this->lang['homeBanner'].' | '.SITE_NAME);
break;
case 'crop':
$this->setData('page_linkname',$linkname);
$this->load('app/views/multipage/v_kms_multipage_crop.php', $this->lang['crop_images'].' | '.$this->lang['homeBanner'].' | '.SITE_NAME);
break;
case 'x2':
$this->load('client/views/banners/v_banners_dashb.php', $this->lang['homeBanner'].' | '.SITE_NAME);
break;
default:
// 404
$this->notFound();
}
}
else{
// dashboard
$this->load('client/views/banners/v_banners_dashb.php', $this->lang['homeBanner'].' | '.SITE_NAME);
}