<?php
/*
* @package ContentCMS
* @author Dan Goldsmith
* @copyright Dan Goldsmith 2012
* @link http://contentcms.d2g.org.uk/
* @version {SUBVERSION_BUILD_NUMBER}
*
* @licence MPL 2.0
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
*
* __Quick Plugin__
* This class is a base class that is extended for creating very quick custom plugins.
* Extending from this Plugin should allow very quick creation of standard plugins.
*/
abstract class quick_plugin extends templated_plugin
{
//Data
private $data = null;
//content_uid data built
private $parameters = null;
public function __construct($content_id)
{
$this->setID($content_id);
$this->setParameters(array());
$content = new content($content_id);
if($content->getStore() !== null)
{
if($this->isMultipule())
{
if(is_array($content->getStore()))
{
$this->setData($content->getStore());
}
else
{
$this->setData(array());
}
}
else
{
if($content->getStore() instanceof quick_plugin_record)
{
$this->setData($content->getStore());
}
else
{
$this->setData(new quick_plugin_record());
}
}
}
else
{
if($this->isMultipule())
{
$this->setData(array());
}
else
{
$this->setData(new quick_plugin_record());
}
}
}
public function isMultipule()
{
$config = $this->getConfig();
if(array_key_exists('GLOBAL', $config))
{
if(array_key_exists('Multiple', $config['GLOBAL']) && ($config['GLOBAL']['Multiple'] === true || strtoupper($config['GLOBAL']['Multiple']) === 'TRUE'))
{
return true;
}
}
return false;
}
//Array of Standard Object(s)
//In the case Where multipule is false then it's one standard object.
public function setData($data)
{
$this->data = $data;
}
//Array of Standard Object(s)
//In the case Where multipule is false then it's one standard object.
public function getData()
{
return $this->data;
}
public function getDataFields()
{
if($this->isMultipule())
{
$fields = array();
foreach($this->getData() as $data)
{
$fields = $fields + array_keys($data->getFields());
}
$fields = array_unique($fields);
}
else
{
$data = $this->getData();
$fields = array();
$fields = array_keys($data->getFields());
}
return $fields;
}
public function getCurrentRecord()
{
//Get The Data From the current selected record
$parameters = $this->getParameters();
if($this->isMultipule())
{
if(array_key_exists("RECORD_ID", $parameters))
{
if(array_key_exists($parameters['RECORD_ID'], $this->data))
{
return $this->data[$parameters['RECORD_ID']];
}
}
else
{
return new quick_plugin_record();
}
}
else
{
return $this->data;
}
return new quick_plugin_record();
}
public function setToDB()
{
$content = new content($this->getID());
$content->setStore($this->getData());
$content->setToDB();
}
public function getParameter($param_name)
{
if(array_key_exists($param_name, $this->parameters))
{
return $this->parameters[$param_name];
}
return null;
}
public function getParameters()
{
return $this->parameters;
}
public function setParameters($params)
{
$this->parameters = $params;
}
public function addParameter($key,$value)
{
$this->parameters[$key] = $value;
}
public function getContentUIDForParamters()
{
return content::getContentUIDForData($this->getParameters());
}
public function getContent($params = null)
{
if(!is_array($params))
{
$params = array();
}
if(!array_key_exists('template', $params))
{
$params['template'] = 'index';
}
if(array_key_exists('RECORD_ID', $params))
{
if($params['RECORD_ID'] < 0)
{
//If the BLOG_ID is less than it's not really the id (-1 is last blog post,-2 second to last etc)
$items = $this->getData();
$tmp_key = null;
for($i = ($params['RECORD_ID'] * -1);$i > 0;$i--)
{
end($items);
$tmp_key = key($items);
array_pop($items);
}
if($tmp_key !== null)
{
$params['RECORD_ID'] = $tmp_key;
$this->addParameter('RECORD_ID', $params['RECORD_ID']);
}
}
else
{
if(array_key_exists($params['RECORD_ID'], $this->getData()))
{
$this->addParameter('RECORD_ID', $params['RECORD_ID']);
}
}
}
$template_path = null;
if(is_file(CONTENT_PLUGIN_DIRECTORY . "/" . $this->getPlugin() . "/" . $this->getTemplateDirectory() . "/" . $params['template'] . ".template.php"))
{
$template_path = CONTENT_PLUGIN_DIRECTORY . "/" . $this->getPlugin() . "/" . $this->getTemplateDirectory() . "/" . $params['template'] . ".template.php";
}
if($template_path == null)
{
throw new exception("Template " . CONTENT_PLUGIN_DIRECTORY . "/" . $this->getPlugin() . "/" . $this->getTemplateDirectory() . "/" . $params['template'] . ".template.php Is Missing");
}
ob_start();
require($template_path);
$parsed_template = ob_get_contents();
ob_end_clean();
return $parsed_template;
}
public function getContentAdmin($params = null)
{
//Set our Default Parameters
$this->addParameter('CONTENT_ID',$this->getID());
$this->addParameter('ADMIN',true);
$template_filename = null;
//As Default We Will go to the index page.
$template_filename = CONTENT_PLUGIN_DIRECTORY . "/" . $this->getPlugin() . "/" . $this->getAdminTemplateDirectory() . "/index.template.php";
if(!is_file($template_filename))
{
$template_filename = dirname(__FILE__) . "/templates/admin/index.template.php";
}
if($this->isMultipule() === false)
{
//We are managing a single item
//Try an get a overwitten template
$template_filename = CONTENT_PLUGIN_DIRECTORY . "/" . $this->getPlugin() . "/" . $this->getAdminTemplateDirectory() . "/edit.template.php";
if(!is_file($template_filename))
{
$template_filename = dirname(__FILE__) . "/templates/admin/edit.template.php";
}
}
else
{
$content_uid_data = content::getDataForContentUID();
if(array_key_exists('RECORD_ID', $content_uid_data))
{
$this->addParameter('RECORD_ID',$content_uid_data['RECORD_ID']);
unset($content_uid_data);
//Try an get a overwitten template
$template_filename = CONTENT_PLUGIN_DIRECTORY . "/" . $this->getPlugin() . "/" . $this->getAdminTemplateDirectory() . "/edit.template.php";
if(!is_file($template_filename))
{
$template_filename = dirname(__FILE__) . "/templates/admin/edit.template.php";
}
}
}
require($template_filename);
//Templat ename might be admin/index whcih breaks the get temaplte call.
$filename_parts = pathinfo($template_filename);
$template = simple_template::getTemplate($filename_parts['basename']);
return $template->endTemplate();
}
public function setContentAdmin($params = null)
{
$static_data = content::getDataForContentUID();
if(array_key_exists('ACTION',$static_data) && strtoupper($static_data['ACTION']) == 'DELETE')
{
//Delete Action
return $this->actionDelete();
}
else
{
//Normal Action
return $this->actionSave();
}
}
private function actionDelete()
{
$data = content::getDataForContentUID();
unset($this->data[$data['RECORD_ID']]);
$this->setToDB();
}
private function actionSave()
{
$reduced_post = array();
$skip_post_data = array (
'CONTENT_UID'
);
foreach($_POST as $key => $value)
{
if(in_array($key, $skip_post_data))
{
continue;
}
$reduced_post[$key] = $value;
}
$data = $this->getData();
$static_data = content::getDataForContentUID();
if($this->isMultipule() === false)
{
if($data->getCreated() == null)
{
$data->setCreated();
}
else
{
$data->setModified();
}
$data->setFields($reduced_post);
}
else
{
if(array_key_exists('RECORD_ID', $static_data) && $static_data['RECORD_ID'] !== null && $static_data['RECORD_ID'] !== '')
{
//Update
$record = $this->data[$static_data['RECORD_ID']];
$record->setModified();
$record->setFields($reduced_post);
$this->data[$static_data['RECORD_ID']] = $record;
}
else
{
//New
$record = new quick_plugin_record();
$record->setCreated();
$record->setFields($reduced_post);
if(count($this->data) > 0)
{
$this->data[max(array_keys($this->data)) + 1] = $record;
}
else
{
$this->data[] = $record;
}
}
}
$this->setToDB();
if(array_key_exists('RECORD_ID',$static_data))
{
unset($static_data['RECORD_ID']);
}
if(array_key_exists('ACTION',$static_data))
{
unset($static_data['ACTION']);
}
content::setRedirectURL(content::getURLForContentUIDData($static_data, content::getRedirectURL()));
}
}
?>