File: D:/HostingSpaces/SBogers10/spire.komma-mediadesign.nl/wwwroot/kms/app/models/m_multipage.php
<?php
/*
Multipage
Standard page which applies to many standard pages.
By using this multipage you can easily add pages to the system.
*/
class Multipage
{
private $_pdo,
$_template,
$_pages,
$_tableNames,
$_pageinfo;
public function __construct($pageId)
{
global $pdo, $template, $pages;
$this->_pdo = $pdo;
$this->_template = $template;
$this->_pages = $pages;
$this->_pageinfo = $this->_pages->get($pageId);
$this->_tableNames['items'] = 'page_'.$this->_template->encodeDbName($this->_pageinfo['label']).'_items';
if( ! isset($_SESSION['data_to_store']))
{
$_SESSION['data_to_store']['title'] = '';
$_SESSION['data_to_store']['date'] = '';
$_SESSION['data_to_store']['msg'] = '';
}
}
/*
SETER / GETTER
*/
/**
* Returns tablename
*
* @access
* @param
* @return
*/
public function getTableName()
{
return $this->_tableNames['items'];
}
/**
* Returns an array containing all items
*
* @access public
* @param
* @return array
*/
public function get($options = NULL)
{
// define options
isset($options['orderby']) ? $orderby = $options['orderby'] : $orderby = 'itemOrder';
isset($options['orderdir']) ? $orderdir = $options['orderdir'] : $orderdir = 'DESC';
isset($options['limitMax']) ? $limitMax = $options['limitMax'] : $limitMax = NULL;
isset($options['limitFrom']) ? $limitFrom = $options['limitFrom'] : $limitFrom = NULL;
$data = array();
$query = 'SELECT i.id AS itemId, i.title, i.msg, i.published, i.timest, i.itemOrder
FROM '.$this->_tableNames['items'].' AS i, kms_active AS a
WHERE a.active = 1
AND a.itemId = i.id
AND a.pageId = ?
ORDER BY i.'.$orderby.' '.$orderdir.' ';
if( ! empty($limitMax))
{
$query .= 'Limit ';
if( ! empty($limitFrom))
{
$query .= $limitFrom.',';
}
$query .= $limitMax;
}
if($st = $this->_pdo->prepare($query))
{
$pageId = $this->_pageinfo['id'];
$st->bindParam(1,$pageId);
$st->execute();
while($result = $st->fetch(PDO::FETCH_OBJ))
{
$data[] = array('id' => $result->itemId, 'title' => $result->title, 'msg' => $result->msg, 'published' => $result->published, 'timest' => $result->timest, 'itemOrder' => $result->itemOrder);
}
}
return $data;
}
/**
* Returns an array containing all items where id is IN $ids
*
* @access public
* @param array
* @return array
*/
public function getIds($ids)
{
if( ! empty($ids))
{
$data = array();
// set params
$params = '';
$type = '';
foreach($ids as $key => $id)
{
$params .= '?,';
$ids[$key] = intval($id);
}
$params = substr($params,0,-1);
// build query
$query = 'SELECT id, title, msg, published, timest, itemOrder
FROM '.$this->_tableNames['items'].'
WHERE id IN ( '.$params.' )
ORDER BY itemOrder DESC';
// prepare statement
if($st = $this->_pdo->prepare($query))
{
$params = array();
foreach($ids as $id)
{
$params[] = $id;
}
$st->execute($params);
while($result = $st->fetch(PDO::FETCH_ASSOC))
{
$data[] = $result;
}
return $data;
}
}
$this->_template->setAlert('U heeft geen items geselecteerd','warning');
return FALSE;
}
/**
* In this function is determined what optional features are added to the get query
*
* @access public
* @param int(optional)
* @return array
*/
public function getOptions($limitMax = 10)
{
// options
$options = array();
$pageId = $this->_pageinfo['id'];
// options=> limitmax && limitfrom
$numTotalItems = count($this->get());
$numTotalPages = $_SESSION['mp_totalpages'][$pageId] = ceil($numTotalItems / $limitMax);
if($limitMax > 0)
{
if(isset($_SESSION['mp_thispage'][$pageId]) && $_SESSION['mp_thispage'][$pageId] > 0)
{
if($_SESSION['mp_thispage'][$pageId] > $_SESSION['mp_totalpages'][$pageId]) $_SESSION['mp_thispage'][$pageId] = $_SESSION['mp_totalpages'][$pageId];
$thispage = $_SESSION['mp_thispage'][$pageId];
}
else{
$thispage = $_SESSION['mp_thispage'][$pageId] = 1;
}
$limitFrom = ($thispage * $limitMax)-$limitMax;
$options['limitFrom'] = $limitFrom;
$options['limitMax'] = $limitMax;
}
// options=> orderby && orderdir
if(isset($_SESSION['mp_sortby'][$pageId]))
{
if($_SESSION['mp_sortby'][$pageId] == 'title') $options['orderby'] = 'title';
if($_SESSION['mp_sortby'][$pageId] == 'added') $options['orderby'] = 'timest';;
if($_SESSION['mp_sortdir'][$pageId] == '↓') $options['orderdir'] = 'DESC';
if($_SESSION['mp_sortdir'][$pageId] == '↑') $options['orderdir'] = 'ASC';
}
return $options;
}
/**
* Puts data into the sessio data_to_store
*
* @access
* @param
* @return
*/
public function setDataToStore($id)
{
if( ! empty($id) && is_numeric($id))
{
$tempdata = $this->getIds(array($id));
$data = $tempdata[0];
foreach(array_keys($_SESSION['data_to_store']) as $key)
{
if($key == 'date')
{
//timest to date conversion
$date = date('d-m-Y',$data['timest']);
$data[$key] = $date;
}
if(isset($data[$key]))
{
$_SESSION['data_to_store'][$key] = $data[$key];
}
}
}
}
/**
* Remember data in a session
*
* @access public
* @param
* @return null
*/
public function saveSessionData()
{
if(isset($_SESSION['data_to_store']))
{
foreach(array_keys($_SESSION['data_to_store']) as $key)
{
$_SESSION['data_to_store'][$key] = $_POST[$key];
}
}
}
/**
* Cleans session data
*
* @access public
* @param
* @return null
*/
public function clean()
{
unset($_SESSION['data_to_store']);
}
/**
* Checks if all post variables are valid after hitting the submit button
*
* @access private
* @param
* @return bool
*/
private function prepareData($method = 'add')
{
$data = array();
$errors = array();
// get variables from post
$title = $required[] = $_POST['title'];
$date = $_POST['date'];
$msg = $_POST['msg'];
// check if ! empty required variables
$empty = 0;
foreach($required as $value)
{
if(empty($value)) $empty++;
}
if($empty > 0) $errors[] = 'U dient de verplichte velden (*) in te vullen.';
// get order
if($method == 'add')
{
$newOrder = 1;
if($st = $this->_pdo->query('SELECT itemOrder FROM '.$this->_tableNames['items'].' ORDER BY itemOrder DESC LIMIT 1'))
{
if($st->rowCount() > 0)
{
$result = $st->fetch(PDO::FETCH_OBJ);
$newOrder = $result->itemOrder;
$newOrder++;
}
}
}
// create timest
$timest=time();
if( ! empty($date))
{
$temp = explode('-',$date);
$day = $temp[0];
$month = $temp[1];
$year = $temp[2];
if( ! empty($day) && ! empty($month) && ! empty($year))
{
$timest = mktime(12,0,0, $month, $day, $year);
}
}
if(count($errors) > 0)
{
foreach($errors as $error)
{
$this->_template->setAlert($error,'error');
}
return FALSE;
}
else
{
$data['title']['val'] = htmlentities($title);
$data['title']['type'] = 's';
$data['msg']['val'] = $msg;
$data['msg']['type'] = 's';
$data['published']['val'] = 1;
$data['published']['type'] = 'i';
$data['timest']['val'] = $timest;
$data['timest']['type'] = 'i';
if($method == 'add')
{
$data['itemOrder']['val'] = $newOrder;
$data['itemOrder']['type'] = 'i';
}
return $data;
}
}
/**
* Stores new values in the database
*
* @access public
* @param
* @return null
*/
public function storeData()
{
if($data = $this->prepareData())
{
// build query
$query = 'INSERT INTO '.$this->_tableNames['items'].'(';
foreach(array_keys($data) as $column)
{
$query.= $column.',';
}
$query = substr($query, 0, -1);
$query .= ') VALUES(';
foreach($data as $char)
{
$query.= '?, ';
}
$query = substr($query, 0, -2);
$query .= ')';
// prepare statement
if($st = $this->_pdo->prepare($query))
{
$i = 1;
foreach($data as $char)
{
$st->bindParam($i, $char['val']);
$i++;
}
// execute statement
if($st->execute())
{
// insert into kms_active
$itemId = $this->_pdo->lastInsertId();
$this->_pdo->query('INSERT INTO kms_active(itemId, pageId, active, lastupdate)VALUES('.$itemId.','.$this->_pageinfo['id'].',1,'.time().')');
// display succes message and close statement
$this->_template->setAlert('Het item is succesvol toegevoegd');
return $itemId;
}
}
}
return FALSE;
}
/**
* Updates existing data in the database
*
* @access public
* @param
* @return null
*/
public function updateStoredData($id)
{
if($data = $this->prepareData('update'))
{
// build query
$query = 'UPDATE '.$this->_tableNames['items'].' SET ';
foreach($data as $column => $char)
{
$query.= $column.'= ?,';
}
$query = substr($query, 0, -1);
$query.= ' WHERE id = ? LIMIT 1';
//prepare statement
if($st = $this->_pdo->prepare($query))
{
$i = 1;
foreach($data as $char)
{
$st->bindParam($i,$char['val']);
$i++;
}
$st->bindParam($i,$id);
// execute statement
if($st->execute())
{
// display succes message and close statement
$this->_template->setAlert('Het item is succesvol bewerkt');
}
}
}
}
/**
* Updates the sortby & sortdir session
*
* @access public
* @param string
* @return null
*/
public function updateSortBy($value)
{
$pageId = $this->_pageinfo['id'];
if($value == 'title' || $value == 'added')
{
// if session == sub2 -> set next direction
if(isset($_SESSION['mp_sortby'][$pageId]) && $_SESSION['mp_sortby'][$pageId] == $value )
{
switch($_SESSION['mp_sortdir'][$pageId])
{
case '↑':
$_SESSION['mp_sortdir'][$pageId] = '↓';
break;
case '↓':
unset($_SESSION['mp_sortby'][$pageId]);
unset($_SESSION['mp_sortdir'][$pageId]);
break;
}
}
// else set session = sub2
else
{
$_SESSION['mp_sortby'][$pageId] = $value;
$_SESSION['mp_sortdir'][$pageId] = '↑';
}
}
else
{
unset($_SESSION['mp_sortby'][$pageId]);
}
}
/**
* Updates the thispage session to navigate between pages
*
* @access public
* @param string
* @return null
*/
public function updateThisPage($value)
{
$pageId = $this->_pageinfo['id'];
if(isset($_SESSION['mp_thispage']))
{
if($value == 'nextpage')
{
if($_SESSION['mp_thispage'][$pageId] != $_SESSION['mp_totalpages'][$pageId]) $_SESSION['mp_thispage'][$pageId]++;
}
else
{
if($_SESSION['mp_thispage'][$pageId] != 1) $_SESSION['mp_thispage'][$pageId]--;
}
}
}
/**
* Checks if an item is published
*
* @access private
* @param int
* @return bool
*/
private function checkPublished($id)
{
if( ! empty($id) && is_numeric($id))
{
// build query
$query = 'SELECT published FROM '.$this->_tableNames['items'].' WHERE id = ?';
// prepare statement
if($st = $this->_pdo->prepare($query))
{
$st->bindParam(1,$id);
$st->execute();
if($result = $st->fetch(PDO::FETCH_OBJ))
{
return $result->published;
}
}
}
}
/**
* Pubishes and unpublishes an item
*
* @access public
* @param int
* @return null
*/
public function togglePublish($id)
{
if( ! empty($id) && is_numeric($id))
{
$oldPublished = $this->checkPublished($id);
$oldPublished == 1 ? $newPublished = 0 : $newPublished = 1;
$query = 'UPDATE '.$this->_tableNames['items'].' SET published = ? WHERE id = ?';
if($st = $this->_pdo->prepare($query))
{
$st->bindParam(1, $newPublished);
$st->bindParam(2, $id);
$st->execute();
}
}
}
/*
CREATE PAGE OUTPUT
*/
/**
* Returns a string containing the top header of the list item table
*
* @access public
* @param
* @return string
*/
public function createHeader()
{
//check order to display
$orderClasses = array('title'=>'black-grad','added'=>'black-grad');
$orderDir = array('title'=>'','added'=>'');
$linkname = $this->_template->encodeUrl($this->_pageinfo['label']);
if(isset($_SESSION['mp_sortby'][$this->_pageinfo['id']]) && isset($_SESSION['mp_sortdir'][$this->_pageinfo['id']]))
{
switch($_SESSION['mp_sortby'][$this->_pageinfo['id']])
{
case 'title':
$orderClasses['title'] = 'black-grad-inverse';
$orderDir['title'] = ' '.$_SESSION['mp_sortdir'][$this->_pageinfo['id']].' ';
break;
case 'added':
$orderClasses['added'] = 'black-grad-inverse';
$orderDir['added'] = ' '.$_SESSION['mp_sortdir'][$this->_pageinfo['id']].' ';
break;
}
}
// display heading
$output = '';
$output .= '<li class="heading">';
$output .= '<div class="col cb"><input type="checkbox" name="cb" id="check_all"/></div>';
$output .= '<a href="'.SITE_ROOT.$linkname.'/sortby/title/" class="col title '.$orderClasses['title'].'">Titel<span class="arrow">'.$orderDir['title'].'</span></a>';
$output .= '<a href="'.SITE_ROOT.$linkname.'/sortby/added/" class="col added '.$orderClasses['added'].'">Toegevoegd op<span class="arrow">'.$orderDir['added'].'</span></a>';
$output .= '<div class="col edit">Acties</div>';
$output .= '</li>';
$this->_template->setData('item_list_heading',$output,TRUE);
}
/**
* Returns a string containing all listitems on this page
*
* @access public
* @param array
* @return string
*/
public function createList($data, $editable = TRUE)
{
$output = 'Er zijn nog geen items gevonden.';
$ids = array();
if($data != NULL)
{
$i = 1;
$output = '';
foreach($data as $info)
{
$ids[] = $info['id'];
$added = date('d / m / Y',$info['timest']);
$output .= '<li';
if($i % 2 != 0) $output .= ' class="alt"';
$output .= '>';
$output .= '<div class="col cb">';
if($editable)
{
$output .= '<input type="checkbox" name="selected[]" value="'.$info['id'].'" />';
}
else
{
$output .= ' ';
}
$output .= '</div>';
$output .= '<div class="col title">'.ucfirst($info['title']).'</div>';
$output .= '<div class="col added">'.$added.'</div>';
$output .= '<div class="col edit">';
if($editable)
{
$linkname = $this->_template->encodeUrl($this->_pageinfo['label']);
$output .= '<div class="subcol">';
$output .= '<a href="'.SITE_ROOT.$linkname.'/publish/'.$info['id'].'/">';
$published = $this->checkPublished($info['id']);
if($published)
{
$output .= 'Depubliceer';
}
else
{
$output .= '<span class="publish">Publiceer</a>';
}
$output .= '</a>';
$output .= '</div>';
$output .= '<div class="subcol">';
$output .= '<a href="'.SITE_ROOT.$linkname.'/verwijder-item/'.$info['id'].'/">Verwijderen</a>';
$output .= '</div>';
$output .= '<div class="subcol">';
$output .= '<a href="'.SITE_ROOT.$linkname.'/wijzig-item/'.$info['id'].'/">Wijzigen</a>';
$output .= '</div>';
$output .= '<div class="clear"></div>';
}
else
{
$output .= ' ';
}
$output .= '</div>';
$output .= '</li>';
$i++;
}
}
$this->_template->setData('list_ids',$ids,TRUE);
$this->_template->setData('item_list',$output,TRUE);
}
}