File: D:/HostingSpaces/SBogers13/rie-jeanne.nl/wwwroot/kms/app/controllers/c_basic_page.class.php
<?php
/**
* c_basic_page.php
* Created by Komma Mediadesign.
* Author: mike
* Date: 4/10/13
*/
class Basic_Page extends Controller
{
/*
* @property id
* Page id in the database.
*/
private $_id;
/*
* @property name
* Unique name f.e. used in sessions and table names.
*/
private $_name;
/*
* @property label
* Displayable name of the page.
*/
private $_label;
public function __construct($name)
{
parent::__construct();
$this->_name = $name;
$this->config();
}
/*
* Set Id and Label
*/
private function config()
{
// What fields to get
$fields = array('id','label');
$data = array_fill_keys($fields,'');
// Get fields from the database
$Dbh = new DatabaseHandler();
$Dbh->setTableName('kms_basic_pages');
$Dbh->setData($data);
$Dbh->addRule('name',$this->_name);
$result = $Dbh->select();
if( ! empty($result))
{
// Add values to properties
$this->_id = $result['id'];
}
}
/*
PAGES
*/
/*
* Dashboard Page; Landing dashboard
*/
public function index()
{
// Create a url pin for redirection
Url_Pin::set('dash');
$noScope = array('clients');
$scope = null;
$this->Model->config('_id',$this->_id);
$this->Model->config('_name',$this->_name);
if( ! in_array($this->_name,$noScope) )
{
// Calculate the amount of pages
$maxItems = 10;
defined('URL_SUB2') ? $page = URL_SUB2 : $page = 1;
$from = $maxItems * ($page-1);
$scope = array($from,$maxItems);
// Get all items
$allItems = $this->Model->getItems();
// Previous
if(count($allItems) > $maxItems * $page )
{
$this->View->setData('page_nav',TRUE);
$next = '<a href="'. LANG_ROOT . URL_PAGE . '/page/' . ( $page + 1 ) . '/">' . $this->View->lang['next'] . '</a>';
$this->View->setData('next_page',$next);
}
// Pages
$pageNumbers = '';
$numPages = ceil(count($allItems) / $maxItems);
if($numPages > 1)
{
for($nr=1;$nr<=$numPages;$nr++)
{
$pageNumbers .= '<li ';
if($nr == $page) $pageNumbers .= ' class="active"';
$pageNumbers .= '><a href="' . LANG_ROOT . URL_PAGE . '/page/' . $nr . '/">' . $nr . '</a></li>';
}
$this->View->setData('page_numbers',$pageNumbers);
}
// Next
if($page != 1)
{
$this->View->setData('page_nav',TRUE);
$prev = '<a href="'. LANG_ROOT . URL_PAGE . '/page/' . ( $page - 1 ) . '/">' . $this->View->lang['prev'] . '</a>';
$this->View->setData('prev_page',$prev);
}
}
// Get items on page
$items = $this->Model->getItems(null,null,$scope);
// Add images to items
foreach($items as $key => $item)
{
if($images = $this->Model->getImages($item['id']))
{
$items[$key]['images'] = $images;
}
/* else if($videos = $this->Model->getVideos($item['id']))
{
$items[$key]['videos'] = $videos;
}*/
}
// Create list from items
$ItemList = new Item_List();
$ItemList->setItems($items);
$list = $ItemList->display();
// Set items list for the view
$this->View->setData('item_list', $list);
// Create buttons for the page
$content = array('type'=>'link', 'href'=> LANG_ROOT . $this->View->urls[$this->_name] . '/' . $this->View->urls['newItem'] . '/', 'label'=>$this->View->lang['new_item']);
$BtnNew = new Button($content);
$BtnNew->addClasses(array('blue'));
$btnNew = $BtnNew->display(FALSE);
$this->View->setData('btn_new', $btnNew);
$content = array('type'=>'link', 'href'=> LANG_ROOT . $this->View->urls[$this->_name] . '/'. $this->View->urls['orderItems'] . '/', 'label'=>$this->View->lang['order_items']);
$BtnOrder = new Button($content);
$BtnOrder->addClasses(array('blue'));
$btnOrder = $BtnOrder->display(FALSE);
$this->View->setData('btn_order', $btnOrder);
$content = array('type'=>'submit','name'=>'delete_selected','label'=>$this->View->lang['delete_selected']);
$BtnDelete = new Button($content);
$BtnDelete->addClasses(array('blue'));
$btnDelete = $BtnDelete->display(FALSE);
$this->View->setData('btn_delete', $btnDelete);
// Form action
$formAction = LANG_ROOT . $this->View->urls[$this->_name] . '/' . $this->View->urls['removeItem'] . '/';
$this->View->setData('form_action', $formAction);
// Form Check all - Only show when items found
if( ! empty($items))
{
$formCheckAll = '<div class="check_all_holder"><input type="checkbox" id="check_all" /><label for="check_all">' . $this->View->lang['check_all'] . '</label><div class="clear"></div></div>';
$this->View->setData('form_check_all', $formCheckAll);
}
// Set Global parameters for the view
$this->_label = $this->View->lang[$this->_name];
$this->View->setData('page_label', $this->_label);
$this->View->setData('page_name', $this->_name);
$this->View->setData('page_title', $this->_label . ' | ' . SITE_NAME);
// Unset form data
Session::destroy($this->_name.'_data');
Session::destroy('data_stored');
// Clean Images
$FileHandler = new File_Handler();
$FileHandler->set('_name', $this->_name . '_images');
$FileHandler->clean($this->_name);
// Clean videos
/*
$VideoHandler = new Video_Handler();
$VideoHandler->set('_form_name',$this->_name);
$VideoHandler->cleanSession();
*/
// Render View
$this->View->render('basic_page/v_dash');
}
public function page()
{
$this->index();
}
/*
* New Item Page; Add a new item to the page
*/
public function newItem()
{
// Pin a URL point
Url_Pin::set();
// Create form
$Form = $this->getForm();
$this->View->setData('form_output',$Form->render());
// Global settings
$this->_label = $this->View->lang[$this->_name];
$this->View->setData('page_title', $this->_label . ' | ' . SITE_NAME);
$this->View->setData('page_label', $this->_label . ' / ' . $this->View->lang['new_item']);
// Js
$jsOutput = '<script type="text/javascript" src="'. SITE_ROOT . 'public/js/tinymce.js"></script>';
$this->View->setData('js_output',$jsOutput);
// Render the view
$this->View->render('basic_page/v_form');
}
/*
* New Item Page; Add a new item to the page
*/
public function editItem()
{
// Pin a URL point
Url_Pin::set();
// Create form
$Form = $this->getForm(TRUE);
$this->View->setData('form_output',$Form->render());
// Global settings
$this->_label = $this->View->lang[$this->_name];
$this->View->setData('page_title', $this->_label . ' | ' . SITE_NAME);
$this->View->setData('page_label', $this->_label . ' / Edit');
// Js
$jsOutput = '<script type="text/javascript" src="'. SITE_ROOT . 'public/js/tinymce.js"></script>';
$this->View->setData('js_output',$jsOutput);
// Render the view
$this->View->render('basic_page/v_form');
}
/*
* Show a list for ordering your items
*/
public function orderItems()
{
// Get items from the database
$this->Model->config('_id',$this->_id);
$this->Model->config('_name',$this->_name);
$items = $this->Model->getItems();
// Add images to items
foreach($items as $key => $item)
{
if($images = $this->Model->getImages($item['id']))
{
$items[$key]['images'] = $images;
}
}
// Get Sorter Object
$Sorter = new Sorter();
list($list,$ids) = $Sorter->createList($items);
// Set list for the view
$this->View->setData('sortable_list',$list);
$this->View->setData('list_ids',$ids);
// Create buttons
$content = array('type'=>'submit','name'=>'delete_selected','label'=>$this->View->lang['save']);
$BtnSave = new Button($content);
$BtnSave->addClasses(array('blue'));
$btnSave = $BtnSave->display(FALSE);
$content = array('type'=>'link', 'href'=> LANG_ROOT . $this->View->urls[$this->_name].'/', 'label'=>$this->View->lang['cancel']);
$BtnCancel = new Button($content);
$BtnCancel->addClasses(array('blue'));
$btnCancel = $BtnCancel->display(FALSE);
// Set buttons for the view
$this->View->setData('btn_save', $btnSave);
$this->View->setData('btn_cancel', $btnCancel);
// Global settings
$this->_label = $this->View->lang[$this->_name];
$this->View->setData('page_title', $this->_label . ' | ' . SITE_NAME);
$this->View->setData('page_label', $this->_label . ' / Sort');
// Render the view
$this->View->render('sorter/v_list');
}
/*
* Re-order your items
*/
public function submitOrder()
{
// Get id from sort form
$ids = explode(',',$_POST['sort_order']);
// Get Sorter Object
$Sorter = new Sorter();
$Sorter->setTableName(TABLE_PREFIX . $this->_name . '_items');
$Sorter->update($ids);
// Show success message
$msg = $this->View->lang['sorter_success'];
$this->Alert->set($msg,'success');
// Redirect to dashboard
//$this->Functions->redirect(LANG_ROOT . $this->View->urls[$this->_name] .'/');
Url_Pin::redirect('dash');
}
/*
* Ask if the user is sure he wants to move his item to the trashcan
*/
public function removeItem()
{
// check for id to remove
$ids = null;
if(defined('URL_SUB2'))
{
$ids = URL_SUB2;
}
else if(isset($_POST['items']))
{
$ids = $_POST['items'];
}
else
{
// Show error message
$errors[] = $this->View->lang['general_nothing_selected'];
$this->Alert->set($errors,'error');
// Redirect to dashboard
//$this->Functions->redirect(LANG_ROOT . $this->View->urls[$this->_name]);
Url_Pin::redirect('dash');
}
// Get items from the database
$this->Model->config('_id',$this->_id);
$this->Model->config('_name',$this->_name);
$items = $this->Model->getItems($ids);
$ItemList = new Item_List();
$ItemList->set('_editable',FALSE);
$ItemList->setItems($items);
$list = $ItemList->display();
// Set items list for the view
$this->View->setData('item_list', $list);
// Global settings
$this->_label = $this->View->lang[$this->_name];
$this->View->setData('page_title', $this->_label . ' | ' . SITE_NAME);
$this->View->setData('page_label', $this->_label . ' / ' . $this->View->lang['remove']);
if(is_array($ids)) $ids = implode(',',$ids);
// Create buttons
$content = array('type'=>'link', 'href'=> LANG_ROOT . $this->View->urls[$this->_name].'/submitRemove/' . $ids . '/', 'label'=>$this->View->lang['yes']);
$BtnYes = new Button($content);
$BtnYes->addClasses(array('blue'));
$btnYes = $BtnYes->display(FALSE);
$content = array('type'=>'link', 'href'=> LANG_ROOT . $this->View->urls[$this->_name].'/', 'label'=>$this->View->lang['no']);
$BtnNo = new Button($content);
$BtnNo->addClasses(array('blue'));
$btnNo = $BtnNo->display(FALSE);
// Set buttons for the view
$this->View->setData('btn_yes', $btnYes);
$this->View->setData('btn_no', $btnNo);
// Render
$this->View->setData('remove_question',$this->View->lang['ask_remove_items']);
$this->View->render('trash/v_remove');
}
/*
* Move item to the trashcan
*/
public function submitRemove()
{
// check if a subId is defined
if(defined('URL_SUB2'))
{
$ids = explode(',',URL_SUB2);
// Get Model
if( ! class_exists('Trash_Models')) require DOCUMENT_ROOT . 'app/models/m_trash.class.php';
$Trash = new Trash_Model();
foreach($ids as $id)
{
// Call the insert function
$Trash->insert($this->_id, $id);
}
// Show success message
if(count($ids) > 1)
{
$msg = str_replace('[num]',count($ids),$this->View->lang['success_remove_items']);
}
else
{
$msg = $this->View->lang['success_remove_item'];
}
$this->Alert->set($msg,'success');
// Redirect to dashboard
//$this->Functions->redirect(LANG_ROOT . $this->View->urls[$this->_name]);
Url_Pin::redirect('dash');
}
}
/*
FORM RELATED METHODS
*/
/*
* Returns a form for this page
* @return Form Object
*/
private function getForm($edit = FAlSE)
{
$reader = new Form_Reader();
$reader->load(DOCUMENT_ROOT . 'app/views/forms/' . $this->_name . '.xml.php');
if($edit) $reader->setType('edit');
$Form = $reader->generate();
return $Form;
}
/*
* Validates the add / edit form.
*/
public function validate()
{
$Form = $this->getForm();
$attr = $Form->get();
// Temp solution: remember caption data
if(isset($_POST['caption']))
{
$captions = $_POST['caption'];
foreach($captions as $key => $caption)
{
// Catch label
if($caption == 'bijschrift' || $caption == 'caption') $caption = '';
// Set caption
$image = Session::get(array($attr['name'] . '_images_data',$key));
$image['caption'] = $caption;
Session::set(array($attr['name'] . '_images_data',$key),$image);
}
}
isset($_POST['create_video']) ? $createVideo = TRUE : $createVideo = FALSE;
// save in session
$ignoredFields = array('submit_form','caption');
foreach($ignoredFields as $key)
{
unset($_POST[$key]);
}
$fields = $_POST;
foreach($fields as $key => $value)
{
Session::set(array($attr['name'].'_data',$key), $value);
}
/* CSB extension */
if( ! isset($_POST['services']))
{
Session::set(array($attr['name'].'_data','services'), array());
}
// Temp solution: save data_session when delete image from image_session
if(isset($_POST['delete_image_url']))
{
$this->Functions->redirect($_POST['delete_image_url']);
}
// Video extension
if($createVideo)
{
$VideoHandler = new Video_Handler();
$VideoHandler->set('_form_name',$this->_name);
if($VideoHandler->add())
{
// Show success message
$msg = 'Youtube success';
$this->Alert->set($msg,'success');
}
else
{
// Show error message
$errors = $VideoHandler->get('_errors');
$this->Alert->set($errors,'error');
}
Url_Pin::redirect();
}
// Catch if images where added
else if( isset($_FILES['images']['name'][0]) && $_FILES['images']['name'][0] != '' )
{
$FileHandler = new File_Handler();
$FileHandler->set('_name', $attr['name'] . '_images');
switch($this->_name)
{
case 'photography':
$dim = array('lw'=>'', 'lh'=>800, 'tw'=>'', 'th'=>600, 'kw'=>90, 'kh'=>90);
break;
case 'birthday':
case 'party':
$pos = $_POST['position'];
if($pos == 0) // onder elkaar - liggend
{
$dim = array('lw'=>'', 'lh'=>320, 'tw'=>'', 'th'=>180, 'kw'=>90, 'kh'=>90);
}
else // naast elkaar - vierkant
{
$dim = array('lw'=>'', 'lh'=>480, 'tw'=>'', 'th'=>260, 'kw'=>90, 'kh'=>90);
}
break;
default:
}
$FileHandler->set('_dim',$dim);
if($FileHandler->add())
{
// Show success message
$msg = $this->View->lang['file_upload_success'];
$this->Alert->set($msg,'success');
}
else
{
$errors = $FileHandler->get('_errors');
$this->Alert->set($errors,'error');
}
Url_Pin::redirect();
}
// Else validate the form
else
{
// validate form
if($Form->validate())
{
// Prepare data for the database
$data = $Form->prepare();
// temp solution
unset($data['videos']);
// Store data in the database
$this->Model->config('_id',$this->_id);
$this->Model->config('_name',$this->_name);
$itemId = $this->Model->store($data);
$FileStorage = new File_Storage();
// Store images in the database
$FileStorage->set('_fileSessionName',$this->_name . '_images_data');
$FileStorage->set('_formName',$this->_name);
$FileStorage->set('_tableName',TABLE_PREFIX . $this->_name . '_images');
/* Extension CSB */
if($this->_name == 'projects')
{
$FieldType = new Field_Type_Services();
$FieldType->insert($itemId);
}
if(isset($_SESSION[$this->_name . '_images_data']) && count($_SESSION[$this->_name . '_images_data']) > 0)
{
// Store images in the database
$FileStorage->store($itemId);
}
$FileStorage->cleanDatabase($itemId);
$VideoHandler = new Video_Handler();
$VideoHandler->set('_form_name',$this->_name);
if(isset($_SESSION[$this->_name . '_videos']) && count($_SESSION[$this->_name . '_videos']) > 0)
{
// Store videos in the database
$VideoHandler->store($itemId);
}
$VideoHandler->cleanDatabase($itemId);
// Show success message
$msg = $this->View->lang['item_success'];
$this->Alert->set($msg,'success');
// Redirect to Dashboard
Url_Pin::redirect('dash');
}
else
{
// Show error message
$errors = Session::get($attr['name'] . '_errors');
$this->Alert->set($errors,'error');
// Redirect to pinned URL point
Url_Pin::redirect();
}
}
}
/*
* Remove an image from the image session
*/
public function removeImage()
{
if(defined('URL_SUB2') && defined('URL_SUB3'))
{
// Get image from session
$key = array(URL_SUB2,URL_SUB3);
$images = Session::get($key);
if(is_array($images))
{
// check if remove image
$FileHandler = new File_Handler();
$FileHandler->set('_name', $this->_name . '_images');
$FileHandler->remove($this->_name,URL_SUB3);
}
// Change thumb?
$thumbkey = array(str_replace('_images','',URL_SUB2),'thumb'); // wanneer naam van afbeeldingen-deel gelijk is aan "images"
$thumb = Session::get($thumbkey);
if(URL_SUB3 < $thumb) Session::set($thumbkey,$thumb-1);
if(URL_SUB3 == $thumb) Session::set($thumbkey,0);
// Remove from the session
Session::destroy($key);
// Re-index array
$_SESSION[URL_SUB2] = array_values($_SESSION[URL_SUB2]);
// Redirect back to page
Url_Pin::redirect();
}
}
/*
* Remove an image from the image session
*/
public function removeVideo()
{
if(defined('URL_SUB2') && defined('URL_SUB3'))
{
// Get image from session
$key = array(URL_SUB2,URL_SUB3);
//Remove from the session
Session::destroy($key);
// Redirect back to page
Url_Pin::redirect();
}
}
/* Filter for improve */
public function filter()
{
if(defined('URL_SUB2'))
{
$catId = URL_SUB2;
$this->Model->config('_id',$this->_id);
$this->Model->config('_name',$this->_name);
$tableName = TABLE_PREFIX . 'blog_items';
$Db = new DatabaseHandler();
$Db->setTableName($tableName);
$Db->addRule('catId',$catId);
$Db->setActiveOnly(TRUE,1);
$Db->setOrder('itemOrder','DESC');
$items = $Db->select();
$items = $Db->twoDimensional($items);
// Add images to items
foreach($items as $key => $item)
{
if($images = $this->Model->getImages($item['id']))
{
$items[$key]['images'] = $images;
}
}
// Create list from items
$ItemList = new Item_List();
$ItemList->setItems($items);
$list = $ItemList->display();
// Set items list for the view
$this->View->setData('item_list', $list);
// Create buttons for the page
$content = array('type'=>'link', 'href'=> LANG_ROOT . $this->View->urls[$this->_name] . '/' . $this->View->urls['newItem'] . '/', 'label'=>$this->View->lang['new_item']);
$BtnNew = new Button($content);
$BtnNew->addClasses(array('blue'));
$btnNew = $BtnNew->display(FALSE);
/*
$content = array('type'=>'link', 'href'=> LANG_ROOT . $this->View->urls[$this->_name] . '/'. $this->View->urls['orderItems'] . '/', 'label'=>$this->View->lang['order_items']);
$BtnOrder = new Button($content);
$BtnOrder->addClasses(array('blue'));
$btnOrder = $BtnOrder->display(FALSE);
*/
$content = array('type'=>'submit','name'=>'delete_selected','label'=>$this->View->lang['delete_selected']);
$BtnDelete = new Button($content);
$BtnDelete->addClasses(array('blue'));
$btnDelete = $BtnDelete->display(FALSE);
// Form action
$formAction = LANG_ROOT . $this->View->urls[$this->_name] . '/' . $this->View->urls['removeItem'] . '/';
$this->View->setData('form_action', $formAction);
// Form Check all - Only show when items found
if( ! empty($items))
{
$formCheckAll = '<input type="checkbox" id="check_all" /><label for="check_all">Check all</label>';
// $this->View->setData('form_check_all', $formCheckAll);
}
// Set buttons for the view
$this->View->setData('btn_new', $btnNew);
//$this->View->setData('btn_order', $btnOrder);
$this->View->setData('btn_delete', $btnDelete);
// Set Global parameters for the view
$this->_label = $this->View->lang[$this->_name];
$this->View->setData('page_label', $this->_label);
$this->View->setData('page_name', $this->_name);
$this->View->setData('page_title', $this->_label . ' | ' . SITE_NAME);
$this->View->setData('filter_back', 'Back to all categories');
// Unset form data
Session::destroy($this->_name.'_data');
// Clean Images
$FileHandler = new File_Handler();
$FileHandler->set('_name', $this->_name . '_images');
$FileHandler->clean($this->_name);
// Render View
$this->View->render('basic_page/v_dash');
}
}
/*
* SEARCH
*/
/*
* Dashboard Page; Landing dashboard
*/
public function search()
{
$search = $_POST['keyword'];
// If search is empty header back to page
if( empty($search) || $search == 'trefwoord' || $search == 'keyword' )
{
$this->Functions->redirect(LANG_ROOT . URL_PAGE . '/');
}
// Create a url pin for redirection
Url_Pin::set('dash');
// Get items on pag
$this->Model->config('_id',$this->_id);
$this->Model->config('_name',$this->_name);
$items = $this->Model->getSearchItems();
// Add images to items
foreach($items as $key => $item)
{
if($images = $this->Model->getImages($item['id']))
{
$items[$key]['images'] = $images;
}
/*else if($videos = $this->Model->getVideos($item['id']))
{
$items[$key]['videos'] = $videos;
}*/
}
// Create list from items
$ItemList = new Item_List();
$ItemList->setItems($items);
$list = $ItemList->display();
// Set items list for the view
$this->View->setData('item_list', $list);
// Create buttons for the page
$content = array('type'=>'link', 'href'=> LANG_ROOT . $this->View->urls[$this->_name] . '/' . $this->View->urls['newItem'] . '/', 'label'=>$this->View->lang['new_item']);
$BtnNew = new Button($content);
$BtnNew->addClasses(array('blue'));
$btnNew = $BtnNew->display(FALSE);
$this->View->setData('btn_new', $btnNew);
$content = array('type'=>'submit','name'=>'delete_selected','label'=>$this->View->lang['delete_selected']);
$BtnDelete = new Button($content);
$BtnDelete->addClasses(array('blue'));
$btnDelete = $BtnDelete->display(FALSE);
$this->View->setData('btn_delete', $btnDelete);
// Form action
$formAction = LANG_ROOT . $this->View->urls[$this->_name] . '/' . $this->View->urls['removeItem'] . '/';
$this->View->setData('form_action', $formAction);
// Form Check all - Only show when items found
if( ! empty($items))
{
$formCheckAll = '<div class="check_all_holder"><input type="checkbox" id="check_all" /><label for="check_all">' . $this->View->lang['check_all'] . '</label><div class="clear"></div></div>';
$this->View->setData('form_check_all', $formCheckAll);
}
// Set Global parameters for the view
$this->_label = $this->View->lang[$this->_name];
$this->View->setData('page_label', $this->_label . ' / Search for "'. $search . '"');
$this->View->setData('page_name', $this->_name);
$this->View->setData('page_title', $this->_label . ' | ' . SITE_NAME);
// Unset form data
Session::destroy($this->_name.'_data');
Session::destroy('data_stored');
// Clean Images
$FileHandler = new File_Handler();
$FileHandler->set('_name', $this->_name . '_images');
$FileHandler->clean($this->_name);
// Clean videos
$VideoHandler = new Video_Handler();
$VideoHandler->set('_form_name',$this->_name);
$VideoHandler->cleanSession();
// Render View
$this->View->render('basic_page/v_dash');
}
}