File: D:/HostingSpaces/SBogers10/kms.komma.pro/wwwroot/app/controllers/c_basic_page.class.php
<?php
/**
* c_basic_page.class.php
* Created by Komma Mediadesign.
* Author: mike
* Date: 9/27/13
*/
require_once DOCUMENT_ROOT . 'lib/form/form.class.php';
class Basic_Page extends Controller
{
// Name used as database prefix, array key etc.
private $_name;
// Id of selected item
private $_currentItem = null;
/*
* Constructor
*/
public function __construct($name)
{
parent::__construct();
// Set page name
$this->_name = $name;
// Selected ID if selected
if(isset($_SESSION['current_item'])) $this->_currentItem = $_SESSION['current_item'];
// Data session
if( ! isset($_SESSION[$name][$this->_currentItem]['data']) && $this->_currentItem != null)
$_SESSION[$name][$this->_currentItem]['data'] = array();
}
/*
* Initialise Model
*/
public function initModel()
{
$this->Model->set('_name',$this->_name);
$this->Model->set('_currentItem',$this->_currentItem);
}
/*
* Dashboard
*/
public function index()
{
// Set Page Title
$this->View->setData('page_title',$this->_name);
// Render View
$this->prepareItemList();
$this->View->render('basic_page/v_items');
// Javascript output
}
/*
* Show item
*/
public function item()
{
// Prepare item form in "edit" mode
$this->prepareForm('edit');
}
/*
* New item
*/
public function newItem()
{
// Prepare item form in "add" mode
$this->prepareForm('add');
}
/*
* Get information for item list
*/
private function prepareItemList()
{
// Item bar data
$this->View->setData('bar_title', $this->lang[$this->_name]);
$this->Model->get() ? $count = count($this->Model->get()) : $count = 0;
$this->View->setData('bar_count', $count);
// Get items
$list = $this->Model->createItemList();
$this->View->setData('item_list',$list);
}
/*
* Prepare item form
*/
private function prepareForm($mode)
{
// Pin URL
Url_Pin::set();
// Javascript
$_SESSION['js_output'] .= '<script type="text/javascript" src="/lib/editor/trumbowyg.js"></script>';
$_SESSION['js_output'] .= '<script type="text/javascript" src="/public/js/basic_page_item.js"></script>';
// Only when in edit mode
if($mode == 'edit')
{
// Prepare form; get item info from the database and fill session
if( empty($_SESSION[$this->_name][$this->_currentItem]['data']))
$this->Model->setItemData($this->_currentItem);
}
// Set Page Title
$this->View->setData('page_title', $this->lang[$mode . '_item'] . ' | ' . $this->lang[$this->_name] . ' | ' . SITE_NAME);
// Create Form object
$Form = new Form($this->_name);
// First check for tabs and header to the right tab
$tabs = $Form->getTabs();
if(count($tabs) > 1)
{
$tabOutput = $this->Model->createTabNav($tabs);
$this->View->setData('tabs',$tabOutput);
}
// Define form action
$action = LANG_ROOT . URL_PAGE . '/validate';
if(defined('URL_SUB2')) $action .= '/' . URL_SUB2;
$this->View->setData('form_action',$action);
// Generate form
$fields = $Form->generate();
$this->View->setData('form_fields',$fields);
$this->View->setData('form_save_link','/' . URL_LANG . '/' . URL_PAGE . '/xhrSaveValues');
$this->View->setData('form_remove_link','/' . URL_LANG . '/' . URL_PAGE . '/removeItem');
// Prepare save button
$btnClass = '';
isset($_SESSION['just_saved']) ? $btnValue = $this->lang['saved'] : $btnValue = $this->lang['save'];
if(isset($_SESSION['just_saved'])) $btnClass = ' confirmation';
unset($_SESSION['just_saved']);
if(isset($_SESSION['unsaved_changes'])) $btnClass = ' action';
$this->View->setData('btn_value',$btnValue);
$this->View->setData('btn_class',$btnClass);
// Specific for mode
$this->View->setData('form_type',$mode);
if($mode == 'edit') $this->View->setData('edit_id',$this->_currentItem);
// Prepare item list
$this->prepareItemList();
// Render View
$this->View->render(array('basic_page/v_items','basic_page/v_form'));
}
/*
* Validate form
*/
public function validate()
{
// Create Form object
$Form = new Form($this->_name);
// Save values of current tab in session
$Form->saveValues();
// Is the form valid
if($Form->validate())
{
// Prepare values (all tabs, trough form)
$data = $Form->prepare();
// Store values (all tabs, trough model)
$this->Model->set('_name',$this->_name);
$itemId = $this->Model->store($data);
// Set "Complete" to 1
// Unset "unsaved changes"
unset($_SESSION['unsaved_changes']);
// Redirect to item
Fn::redirect(LANG_ROOT . URL_PAGE . '/item-' . $itemId);
}
else
{
// Set "Complete" to 0
}
// Redirect to last pin
Url_Pin::redirect();
}
/*
* Tell the model to remove an item from the database
*/
public function removeItem()
{
// Remove item
$this->Model->removeItem();
// Redirect item list
Fn::redirect(LANG_ROOT . URL_PAGE);
}
/*
* Save values in session
* Called by ajax when for example switching tabs
*/
public function xhrSaveValues()
{
// Create Form object
$Form = new Form($this->_name);
// Save values of current tab in session
$Form->saveValues();
}
}