File: D:/HostingSpaces/SBogers64/klimroosbudel.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 = ['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 = ['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 = [$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;
} elseif ($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
if (
! ($this->_name == 'news' && $allItems >= 1)
&& $this->_name != 'photo_sliders'
) {
$content = ['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(['blue']);
$btnNew = $BtnNew->display(false);
$this->View->setData('btn_new', $btnNew);
$content = ['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(['blue']);
$btnOrder = $BtnOrder->display(false);
$this->View->setData('btn_order', $btnOrder);
$content = ['type'=>'submit', 'name'=>'delete_selected', 'label'=>$this->View->lang['delete_selected']];
$BtnDelete = new Button($content);
$BtnDelete->addClasses(['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(null, ['id'=>'', 'title'=>'']);
// 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 = ['type'=>'submit', 'name'=>'delete_selected', 'label'=>$this->View->lang['save']];
$BtnSave = new Button($content);
$BtnSave->addClasses(['blue']);
$btnSave = $BtnSave->display(false);
$content = ['type'=>'link', 'href'=> LANG_ROOT.$this->View->urls[$this->_name].'/', 'label'=>$this->View->lang['cancel']];
$BtnCancel = new Button($content);
$BtnCancel->addClasses(['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;
} elseif (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 = ['type'=>'link', 'href'=> LANG_ROOT.$this->View->urls[$this->_name].'/submitRemove/'.$ids.'/', 'label'=>$this->View->lang['yes']];
$BtnYes = new Button($content);
$BtnYes->addClasses(['blue']);
$btnYes = $BtnYes->display(false);
$content = ['type'=>'link', 'href'=> LANG_ROOT.$this->View->urls[$this->_name].'/', 'label'=>$this->View->lang['no']];
$BtnNo = new Button($content);
$BtnNo->addClasses(['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([$attr['name'].'_images_data', $key]);
$image['caption'] = $caption;
Session::set([$attr['name'].'_images_data', $key], $image);
}
}
isset($_POST['create_video']) ? $createVideo = true : $createVideo = false;
// save in session
$ignoredFields = ['submit_form', 'caption'];
foreach ($ignoredFields as $key) {
unset($_POST[$key]);
}
$fields = $_POST;
foreach ($fields as $key => $value) {
Session::set([$attr['name'].'_data', $key], $value);
}
// Temp solution: save data_session when delete image from image_session
if (isset($_POST['delete_image_url'])) {
$this->Functions->redirect($_POST['delete_image_url']);
}
if (isset($_POST['delete_doc_url'])) {
$this->Functions->redirect($_POST['delete_doc_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
elseif (isset($_FILES['images']['name'][0]) && $_FILES['images']['name'][0] != '') {
$FileHandler = new File_Handler();
$FileHandler->set('_name', $attr['name'].'_images');
switch($this->_name) {
case 'photo_albums':
$dim = ['lw'=>800, 'lh'=>null, 'tw'=>300, 'th'=>200, 'kw'=>90, 'kh'=>90];
break;
case 'photo_sliders':
$dim = ['lw'=>460, 'lh'=>null, 'tw'=>null, 'th'=>null, 'kw'=>90, 'kh'=>90];
break;
default:
$dim = ['lw'=>850, 'lh'=>500, 'tw'=>500, 'th'=>280, 'kw'=>90, 'kh'=>90];
}
$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();
}
// Catch if images where added
elseif (isset($_FILES['docs']['name'][0]) && $_FILES['docs']['name'][0] != '') {
$FileHandler = new File_Handler();
$FileHandler->set('_name', $attr['name'].'_docs');
if ($FileHandler->addDoc()) {
// 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 = [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 = [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 = [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 = ['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(['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 = ['type'=>'submit', 'name'=>'delete_selected', 'label'=>$this->View->lang['delete_selected']];
$BtnDelete = new Button($content);
$BtnDelete->addClasses(['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;
} elseif ($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 = ['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(['blue']);
$btnNew = $BtnNew->display(false);
$this->View->setData('btn_new', $btnNew);
$content = ['type'=>'submit', 'name'=>'delete_selected', 'label'=>$this->View->lang['delete_selected']];
$BtnDelete = new Button($content);
$BtnDelete->addClasses(['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');
}
}