File: D:/HostingSpaces/SBogers10/spire.komma-mediadesign.nl/wwwroot/kms/app/controllers/c_multipage.php
<?php
/*
c_multipage.php // Controller
Guides user to the right page within the multipage section
*/
require_once DOCUMENT_ROOT.'app/models/m_multipage.php';
require_once DOCUMENT_ROOT.'app/models/images/m_imageHandler.php';
require_once DOCUMENT_ROOT.'app/models/m_sorter.php';
/**
*
* Global page info for the current "multipage" such as label, id and linkname
*/
$label = $this->getData('page_label',FALSE);
$pageId = $this->getData('page_id',FALSE);
$linkName = $this->encodeUrl($label);
/**
*
* Check if the "sub" GET variable is set
*/
if(defined('URL_SUB')) $sub = URL_SUB;
/**
*
* 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' => 600, 'lh' => null, 'tw' => 300, 'th' => 200, 'mw' => 60, 'mh' => 60);
switch($pageId)
{
default:
$imgDim = $std;
}
/**
*
* Multipage objects
*/
$multipage = new Multipage($pageId);
$imageHandler = new ImageHandler($pageId, $imgDim);
$imageStorage = new ImageStorage($pageId);
/***********************************************************************************/
/*
Check if any actions are needed
*/
/*
ADD / EDIT
*/
/**
*
* If the user hits the "save" button in the "add-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 store the item.
* If item stored we return to the "dashboard", if images added we return to the "add-form".
*/
if(isset($_POST['sbm_save_new']) || isset($_POST['sbm_add_images']))
{
$multipage->saveSessionData();
if( ! $imageHandler->addImages())
{
if($itemId = $multipage->storeData())
{
if($imageStorage->store($itemId, $_SESSION[$linkName . '_images_to_store']))
{
unset($_SESSION[$linkName . '_images_to_store']);
}
$this->redirect(LANG_ROOT.$linkName.'/');
}
}
else
{
$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".
*/
if(isset($_POST['sbm_save_edit']) || isset($_POST['sbm_edit_images']))
{
$multipage->saveSessionData();
$id = $_POST['id'];
if( ! $imageHandler->addImages())
{
$multipage->updateStoredData($id);
$imageHandler->removeStoredImages($id);
if($imageStorage->store($id, $_SESSION[$linkName . '_images_to_store']))
{
unset($_SESSION[$linkName . '_images_to_store']);
}
$this->redirect(LANG_ROOT.$linkName.'/');
}
else
{
$this->redirect(LANG_ROOT.$linkName.'/wijzig-item/'.$id.'/');
}
}
/*
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 Multipage::GetIds 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($items = $multipage->getIds($_POST['selected']))
{
list($output, $ids) = $multipage->createList($items, FALSE);
$this->setData('item_list', $output, TRUE);
$this->setData('list_ids', $ids, TRUE);
}
}
else
{
$this->setAlert('U heeft geen items geselecteerd','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($pageId);
$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 = $multipage->getTableName();
$sorter = new Sorter($pageId, $tn, 'itemOrder');
$sorter->update($ids);
$this->redirect(LANG_ROOT.$linkName.'/');
}
/**
*
* When the user hits the order button in the order (images) form. An array of shortcodes is grapped from the comma-seperated string in the order form.
* A new sorter object is created, this updates the order.
* After that we need to check where to redirect. (for example to the "add-form" or to the "edit-form" This is stored in the "return_to" session.
* We redirect back to the "dashboard".
*/
else if(isset($_POST['sbm_order_images']))
{
$shortcodes = explode(',',$_POST['sort_order']);
$tn = $imageHandler->getTableName();
$sorter = new Sorter($pageId, $tn, 'imageOrder');
$sorter->updateImages($shortcodes,$_SESSION[$linkName . '_images_to_store']);
if(isset($_SESSION['return_to']['url']))
{
$url = $_SESSION['return_to']['url'];
$id = '';
if(isset($_SESSION['return_to']['id']))
{
$id = $_SESSION['return_to']['id'].'/';
}
$this->redirect(LANG_ROOT.$linkName.'/'.$url.'/'.$id);
}
else{
$this->redirect(LANG_ROOT.$linkName.'/nieuw-item/');
}
}
/*
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();
if(isset($_SESSION['return_to']['url']))
{
$url = $_SESSION['return_to']['url'];
$id = '';
if(isset($_SESSION['return_to']['id']))
{
$id = $_SESSION['return_to']['id'].'/';
}
$this->redirect(LANG_ROOT.$linkName.'/'.$url.'/'.$id);
}
else{
$this->redirect(LANG_ROOT.$linkName.'/nieuw-item/');
}
}
/**
*
* Check if there are any actions needed when a subpage is found
*/
else if(isset($sub))
{
$sub = $sub;
switch($sub)
{
/**
*
* This is the "add-form" The images handler creates a string of listitems for the images.
*/
case 'nieuw-item':
$_SESSION['return_to']['url'] = 'nieuw-item';
$imageHandler->createList();
break;
/**
*
* This is the "edit-form". When a page is editted, first thing to do is add all existing data from the database to the Session "data_to_store".
* Also for the images we need to set the array. This should happen only onces. (otherwise you get double images.)
* After this the imagesHandler creates a string of listitems for the images.
*/
case 'wijzig-item':
$_SESSION['return_to']['url'] = 'wijzig-item';
if(isset($sub2))
{
$_SESSION['return_to']['id'] = $sub2;
$multipage->setDataToStore($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'];
foreach($arr as $data)
{
$session->add($data);
}
$_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 = $multipage->getIds(array($sub2)))
{
list($output, $ids) = $multipage->createList($items, 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':
$options = $multipage->getOptions(0);
$items = $multipage->get($options);
$sorter = new Sorter($pageId);
$sorter->createList($items);
break;
/**
*
* This is the page where we change the order of images.
* First we need to get all images in the session.
* Then add a new sorter object and retrieve a list.
*/
case 'wijzig-volgorde-afbeeldingen':
$images = $_SESSION[$linkName . '_images_to_store']->get();
$sorter = new Sorter($pageId);
$sorter->createThumbList($images);
break;
/**
*
* This happens when the user wants to sort his list in the "dashboard". For example by "title" of by "added".
* On what condition we need to order is passed trough $sub2.
* Update the order and redirect back to the Dashboard.
*/
case 'sortby':
if(isset($sub2))
{
$multipage->updateSortBy($sub2);
$this->redirect(LANG_ROOT.$linkName.'/');
}
break;
/**
*
* This happens when the user clicks on "next page" or "previous page" in the "dashboard".
* Update the page width Multipage::updateThisPage. The param is 'nextpage' or 'prevpage'.
*/
case 'nextpage':
case 'prevpage':
$multipage->updateThisPage($sub);
$this->redirect(LANG_ROOT.$linkName.'/');
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':
if(isset($sub2) && is_numeric($sub2))
{
$_SESSION[$linkName . '_images_to_store']->remove($sub2);
if(isset($_SESSION['return_to']['url']))
{
$url = $_SESSION['return_to']['url'];
$id = '';
if(isset($_SESSION['return_to']['id']))
{
$id = $_SESSION['return_to']['id'].'/';
}
$this->redirect(LANG_ROOT.$linkName.'/'.$url.'/'.$id);
}
else{
$this->redirect(LANG_ROOT.$linkName.'/nieuw-item/');
}
}
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;
/**
*
* When a user wants to publish or unpublish an item. Call the Multipage::togglePublish method.
*/
case 'publish':
$id = $sub2;
$multipage->togglePublish($id);
$this->redirect(LANG_ROOTLANG_ROOT.$linkName.'/');
break;
}
}
/**
*
* Code below is used for the dashboard of the multipage
* A string of list items (max 20) is created and stored in the Template::data property.
* Also an array ids is stored. The Mutlipage::clean method deletes all the images left in the session.
*/
else
{
$options = $multipage->getOptions(20);
$items = $multipage->get($options);
$multipage->createHeader();
list($output, $ids) = $multipage->createList($items);
$this->setData('item_list', $output, TRUE);
$this->setData('list_ids', $ids, TRUE);
$multipage->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':
$this->load('app/views/multipage/v_kms_multipage_add.php', 'Item toevoegen - '.SITE_NAME);
break;
case 'wijzig-item':
$this->load('app/views/multipage/v_kms_multipage_edit.php', 'Item wijzigen - '.SITE_NAME);
break;
case 'verwijder-items':
$this->load('app/views/trashcan/v_kms_trashcan_delete.php', 'Items verwijderen - '.SITE_NAME);
break;
case 'verwijder-item':
$this->load('app/views/trashcan/v_kms_trashcan_delete.php', 'Item verwijderen - '.SITE_NAME);
break;
case 'wijzig-volgorde':
$this->load('app/views/sortorder/v_kms_order_items.php', 'Volgorde wijzigen - '.SITE_NAME);
break;
case 'wijzig-volgorde-afbeeldingen':
$this->load('app/views/sortorder/v_kms_order_images.php', 'Volgorde wijzigen - '.SITE_NAME);
break;
case 'crop':
$this->load('app/views/multipage/v_kms_multipage_crop.php', 'Afbeeldingen bijsnijden - '.SITE_NAME);
break;
default:
// 404
$this->notFound();
}
}
else
{
$this->load('app/views/multipage/v_kms_multipage_dashb.php', 'Dashboard - '.SITE_NAME);
}