File: D:/HostingSpaces/SBogers64/klimroosbudel.nl/wwwroot/kms/app/models/m_basic_page.class.php
<?php
/**
* m_basic_page_model.class.php
* Created by Komma Mediadesign.
* Author: mike
* Date: 4/10/13
*/
class Basic_Page_Model extends Model
{
/*
* @property id
* Page Id.
*/
private $_id;
/*
* @property name
* Unique name f.e. used in sessions and database.
*/
private $_name;
/*
* Constructor
*/
public function __construct()
{
parent::__construct();
// Get fields from the database
}
public function config($var, $val)
{
$this->{$var} = $val;
}
/*
* Get Items from the database
* @return array
*/
public function getItems($ids = null, $data = null, $scope = null)
{
// Setup database model
$tableName = TABLE_PREFIX.$this->_name.'_items';
$this->Db->setTableName($tableName);
$this->Db->setActiveOnly(true, $this->_id);
if ($data != null) {
$this->Db->setData($data);
}
if ($scope != null) {
$this->Db->setScope($scope[0], $scope[1]);
}
$this->Db->setOrder('itemOrder', 'DESC');
// Return requested ids
if (is_array($ids)) {
$result = [];
foreach ($ids as $id) {
$this->Db->clearRule();
$this->Db->addRule('id', $id);
$result[] = $this->Db->select();
}
return $result;
}
// Return requested id
elseif (is_numeric($ids)) {
$this->Db->addRule('id', $ids);
if ($result = $this->Db->select()) {
return $this->Db->twoDimensional($result);
}
}
// Return all ids
else {
if ($result = $this->Db->select()) {
return $this->Db->twoDimensional($result);
}
}
return false;
}
/*
* Get Items from the database
* @return array
*/
public function getImages($id)
{
$tableName = TABLE_PREFIX.$this->_name.'_images';
$ImageDb = new DatabaseHandler();
$ImageDb->setTableName($tableName);
$ImageDb->setOrder('session_key', 'ASC');
$ImageDb->addRule('itemId', $id);
if ($result = $ImageDb->select()) {
return $ImageDb->twoDimensional($result);
}
return false;
}
/*
* Get Items from the database
* @return array
*/
public function getVideos($id)
{
$tableName = TABLE_PREFIX.$this->_name.'_videos';
$VideoDb = new DatabaseHandler();
$VideoDb->setTableName($tableName);
$VideoDb->setOrder('session_key', 'ASC');
$VideoDb->addRule('itemId', $id);
if ($result = $VideoDb->select()) {
return $VideoDb->twoDimensional($result);
}
return false;
}
/*
* Store values in the database
*/
public function store($data)
{
// Setup database model
$tableName = TABLE_PREFIX.$this->_name.'_items';
$this->Db->setTableName($tableName);
// Catch images, they have there own storage method
unset($data['images']);
// Thumbnail
if (isset($_POST['thumb'])) {
$data['thumb'] = $_POST['thumb'];
}
// Add or edit
if (isset($_POST['form_type'])) {
switch($_POST['form_type']) {
case 'add':
// get Latest item Order
$orderDb = new DatabaseHandler();
$orderDb->setTableName($tableName);
$orderDb->setData(['itemOrder'=>'']);
$orderDb->setOrder('itemOrder', 'DESC');
$orderDb->setScope(0, 1);
$result = $orderDb->select();
$highestOrder = $result['itemOrder'];
// Add itemOrder to $data
$data['itemOrder'] = $highestOrder + 1;
$this->Db->setData($data);
$insertId = $this->Db->insert();
// Add to "active" table
$data = ['itemId'=>$insertId, 'pageId'=>$this->_id, 'timestamp'=>time()];
$activeDb = new DatabaseHandler();
$activeDb->setTableName(TABLE_PREFIX.'kms_active');
$activeDb->setData($data);
$activeDb->insert();
return $insertId;
break;
case 'edit':
if (isset($_POST['edit_id'])) {
$this->Db->setData($data);
$this->Db->addRule('id', $_POST['edit_id']);
$this->Db->update();
return $_POST['edit_id'];
}
break;
}
}
return false;
}
/*
*
*/
public function getSearchItems()
{
// Setup database model
$tableName = TABLE_PREFIX.$this->_name.'_items';
$Dbh = new DatabaseHandler();
$Dbh->setTableName($tableName);
$Dbh->setActiveOnly(true, $this->_id);
if ($this->_name == 'blog') {
$Dbh->setOrder('date', 'DESC');
} else {
$Dbh->setOrder('itemOrder', 'DESC');
}
//Define fields for search fields
$searchFields = ['title', 'streamer', 'description', 'tags'];
// Add searchwords
$Dbh = $this->addSearchWords($Dbh, $searchFields);
// Checked filters
if ($result = $Dbh->select()) {
return $Dbh->twoDimensional($result);
}
return false;
}
private function addSearchWords($Db, $searchFields)
{
if (strtolower($_POST['keyword']) != 'trefwoord' && strtolower($_POST['keyword']) != 'keyword') {
$superGroupIds = [];
$search = '';
$words = explode(' ', $_POST['keyword']);
foreach ($words as $key => $searchWord) {
$cleanWord = trim($searchWord);
if (! empty($cleanWord)) {
$search = '%'.$cleanWord.'%';
}
$superGroupIds[] = $Db->addSearchGroup($search, $searchFields, 'OR', 'AND');
}
$Db->addToSuperGroup($superGroupIds);
}
return $Db;
}
}