<?php
/**
* Class File for the EventDriver
*
* PHP Version 4
*
* @author <hide@address.com>
* @copyright Copyright (c) 2006, Benedikt Hallinger
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301 USA
*/
/**
* This is a generic EventDriver, that must be subclassed.
*
* It must be subclassed, so it gets the methods to store/retrieve the events data.
*
* This class provides some standardized method used by the core but you can
* and should use those methods too, especially the config methods.
*
* It is neccessary, that a custom driver is loaded, which extends this class so
* eventdata is really read and stored.
*
* To write your own driver, follow the instructions in the documentation,
* but you just need to subclass this class and make sure it is used
*/
class LCC_EventDriver extends LCC_Driver
{
/**
* Allowed note public fields
*
* @see LCC_Driver::_allowedFields
* @access private
* @var array
* @see setFieldSecurity(), checkField(), checkMandatory()
*/
var $_allowedFields = array(
'title' => array(
'regex' => '/^.*$/',
'mandatory' => true,
'writable' => true,
'html' => false
),
'description' => array(
'regex' => '/^.*$/s', // modificator 's' makes "." match newline
'mandatory' => true,
'writable' => true,
'html' => false
),
'start_date' => array(
'regex' => '/^(?:\d{4}-\d\d-\d\d)|(?:\d\d?\.\d\d?\.\d{4})$/',
'mandatory' => true,
'writable' => true,
'html' => false
),
'end_date' => array(
'regex' => '/^(?:\d{4}-\d\d-\d\d)|(?:\d\d?\.\d\d?\.\d{4})$/',
'mandatory' => true,
'writable' => true,
'html' => false
),
'start_time' => array(
'regex' => '/^[012]?\d:\d\d?(?:\d\d)?$/',
'mandatory' => false,
'writable' => true,
'html' => false
),
'end_time' => array(
'regex' => '/^[012]?\d:\d\d?(?:\d\d)?$/',
'mandatory' => false,
'writable' => true,
'html' => false
),
'color' => array(
'regex' => '/^#[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]$/',
'mandatory' => false,
'writable' => true,
'html' => false
)
);
/**
* Event fields managed by LCC (metadata)
*
* @see LCC_Driver::_allowedFields
* @see getEventData()
* @access private
* @var array
*/
var $_metaFields = array(
'id', 'author', 'created',
'last_modified', 'last_modified_author',
'subscribed_yes', 'subscribed_no', 'subscribed_unknown',
'event_start', 'event_end'
);
/**
* Methods the class (and extends) HAS TO provide to be compatible with the core.
*
* @see LCC_Driver::_needed_methods
* @access private
* @var array
*/
var $_needed_methods = array('geteventsfordate', 'getdata', 'storedata', 'deleteevent');
/**
* Postprocessor for reading data
*
* This postprocessor is called from the core after getting the secured
* data. It enables to perform some driver type unique actions.
* Here, we assign some metadata.
*
* @access private
* @param array $data Data as fetched by {@link LCC_Driver::getSecuredData()}
* @param mixed $id ID of the event in question
* @return array Processed data array
*/
function _getDataPostprocessor($data, $id)
{
$data['id'] = htmlentities($id, ENT_QUOTES); // HTML should never be allowed in IDs!
return $data;
}
/**
* This returns an array of the subscribed users of the event
*
* Interests are stored as event metadata. This method parses this metadata.
*
* @param int $id ID of the event
* @return array Array with usernames of the users, sorted by interest
*/
function getSubscribedUsers($id)
{
$return['yes'] = array();
$return['no'] = array();
$return['unknown'] = array();
// read raw usernames
$event_data = $this->getData($id);
if (false === $event_data) {
return $return;
} else {
// convert raw userlist to array
// "/(?<!\\\\)\|/" matches any pipe that is not preceded by a backslash, eg not escaped
// since this is our choosen separator. "(?<!)" is a so called 'negative lookbehind assertion;
// see: http://www.php.net/manual/de/reference.pcre.pattern.syntax.php
$escaped['yes'] = preg_split('/(?<!\\\\)\|/', $event_data['subscribed_yes']);
$escaped['no'] = preg_split('/(?<!\\\\)\|/', $event_data['subscribed_no']);
$escaped['unknown'] = preg_split('/(?<!\\\\)\|/', $event_data['subscribed_unknown']);
// unescape the usernames
foreach ($escaped as $cat => $users) {
foreach ($users as $user) {
$return[$cat][] = stripcslashes($user);
}
}
return $return;
}
}
/**
* Subscribe a user to this event with a specific interest
*
* Interest values can be "yes", "no", "unknown"
* Interests are stored as event metadata.
*
* @param int $id ID of the event
* @param string $username The name of the user to be subscribed
* @param string $interes How the user want to participate
* @return boolean
*/
function subscribeUser($id, $username, $interest)
{
if ($interest != 'yes' && $interest != 'no' && $interest != 'unknown') {
die('<b>LCC_EventDriver ERROR:</b> The interest "'.$interest.'" is not supported by LCC!');
}
if (strlen($username) <= 0) {
die('<b>LCC_EventDriver ERROR:</b> cant subscribe empty username!');
}
// check if event is here, if so we use the data
$event_data = $this->getData($id);
if (false === $event_data) {
return $event_data;
} else {
// Fetch already subscribed users
$subscribed = $this->getSubscribedUsers($id);
// Remove user from all groups and add to new interest group
foreach ($subscribed as $cat => $users) {
$index = array_search($username, $subscribed[$cat]);
if (false !== $index) {
unset($subscribed[$cat][$index]);
}
if ($cat === $interest) {
array_push($subscribed[$interest], $username);
}
// convert the interest group in storable format
$users_string = '';
foreach ($subscribed[$cat] as $user) {
$users_string .= addcslashes($user, '\|').'|';
}
$users_string = substr($users_string, 0, -1);
$event_data['subscribed_'.$cat] = $users_string;
}
// Store the modified event in backend
$return = $this->storeData($id, $event_data);
if (false === $return) {
return false;
} else {
return true;
}
}
}
/**
* Overriden version of LCC_Driver::_validateField method
*
* This method does deal with timestamps for the date fields, since the timestamps
* are generated by LCC_Core itnernally which will lead to validation failure.
*
* @see LCC_Driver::_validateField()
*/
function _validateField($field, $parameter)
{
$return = true;
if (strlen($parameter) > 0) {
$return = @preg_match($this->_allowedFields[$field]['regex'], $parameter);
if (!$return && is_a($this, 'LCC_EventDriver') && ($field == 'start_date' || $field == 'end_date')) {
// Handle event date fields, since we store timestamps in the backend
if (is_numeric($parameter)) {
$return = true;
}
}
}
return $return;
}
}
?>