<?php
/**
* MySQL based EventDriver for LCC
*
* PHP Version 4, 5
*
* @author Greg* <hide@address.com>
* @copyright Copyright (c) 2007, 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
*/
/**
* A Driver that is capaple of storing and retrieving Notedata from a MySQL database.
*
* The configuration is done via a more dimensional associative array (see below).
*
* The database schema is required as dollowing:
* +----+----------+--------+---------+-----+
* | id | event_id | author | created | ... |
* +----+----------+--------+---------+-----+
* Please be sure to provide at least ALL default fields
* LCC requires. These are all default fields mentioned in
* the default configuration (see below).
*
* Possible configuration:
* 'db' (array) : Database connection informations.
* Default configuration:
* 'host' => "localhost",
* 'user' => "user",
* 'pwd' => "secret",
* 'dbname' => "lcc"
*
* 'table' (string) : The table name of the note table
* Default configuration: 'notes'
*
* 'fields' (array) : User can specify his own db col names for fields
* syntax is internal_key => user_key
* Default configuration:
* 'id' => "id",
* 'event_id', => "event_id",
* 'author' => "author",
* 'created' => "created",
* 'last_modified' => "last_modified",
* 'last_modified_author' => "last_modified_author"
*/
class LCC_NoteDriver_MySQL extends LCC_NoteDriver
{
/**
* configuration array
*/
var $config = array(
'db' => array(
'host' => "localhost", // Hostname
'user' => "user", // db username
'pwd' => "secret", // password
'dbname' => "lcc", // db name
),
// Fields names
'fields' => array(
'id' => "id",
'event_id' => "event_id",
'author' => "author",
'created' => "created",
'last_modified' => "last_modified",
'last_modified_author' => "last_modified_author",
'text' => "text"
),
'table' => "notes"
);
/**
* The link-id for the connection.
*
* @access private
*/
var $_link = false;
/**
* Initiates connection to the database
*/
function configure()
{
$this->_link = mysql_connect($this->config['db']['host'], $this->config['db']['user'], $this->config['db']['pwd']);
if ( $this->_link ) {
mysql_select_db($this->config['db']['dbname'], $this->_link);
}
}
/**
* Retrieves noteIDs for a given event.
*
* @param int $event_id The eventID for which we want the notes.
* @return false|array Either an error (false) or the list of noteIDs.
*/
function getNotes($event_id)
{
$notes = false;
if ( $this->_link ) {
$notes = array();
// SELECT id FROM notes WHERE event_id = $event_id
$query = "SELECT " . $this->config['fields']['id'] . " FROM " . $this->config['table'] . " WHERE (`" . $this->config['fields']['event_id'] . "`=" . $event_id . ");";
$rsc = mysql_query($query, $this->_link);
if ( $rsc ) {
while ($note_row = mysql_fetch_row($rsc)) {
$notes[] = $note_row[0]; // Fills the notes array for the given event.
}
}
}
return $notes;
}
/**
* Inserts or updates a note.
* If passed id is empty, then it's an insertion, otherwise, it's an update of an existing note.
*
* @param int $id NoteID to be updated (or empty if new note).
* @param array $data Note Data.
* @return false|int NoteID or false on error.
*/
function storeData($id, $data)
{
$return = false;
if ($this->_link) {
// By default, we suppose it's a new note.
$query_head = "INSERT INTO ";
$query_body = "";
$query_foot = ";";
if ($id) { // Update an existing event
// build the UPDATE query
$query_head = "UPDATE ";
$query_foot = "WHERE `" . $this->config['fields']['id'] . "`=" . $id . " LIMIT 1;";
}
$query_head .= $this->config['table'] . " SET";
// Builds the query body : loops through the fields that are needed.
foreach ($data as $field => $value) {
if ( array_key_exists($field, $this->config['fields'])) {
$query_body .= " `" . $this->config['fields'][$field] . "`=" . (is_string($value) ? "\"":"") . $value . (is_string($value) ? "\"":"") . ",";
}
}
if (strlen($query_body)) {
// Query was successfully built
$query_body[ strlen($query_body) - 1 ] = " ";
$query = $query_head . $query_body . $query_foot;
$success = mysql_query($query, $this->_link);
}
else {
// Or maybe not.
$success = false;
}
}
if ( $success ) {
if ( $id ) {
$return = $id;
} else {
$return = mysql_insert_id($this->_link);
}
}
return $return;
}
/**
* Retrieves data of a note.
*
* @param int $id NoteID for which we want the datas.
* @return array|false Note Data or false on error.
*/
function getData($id)
{
$data = false;
if ( $this->_link ) {
$rsc = mysql_query("SELECT * FROM " . $this->config['table'] . " WHERE `" . $this->config['fields']['id'] . "`=" . $id . " LIMIT 1;", $this->_link);
// At this stage, col names are those defined by user...
$raw_data = mysql_fetch_assoc($rsc);
// builds the correct data array.
$data = array();
if ( $raw_data ) {
// ... So we have to translate them with internal names.
foreach ($this->config['fields'] as $internal_key => $foreign_key) {
if (array_key_exists($internal_key, $this->_allowedFields) || in_array($internal_key, $this->_metaFields)) {
if ( !empty($raw_data[ $foreign_key ])) {
$data[$internal_key] = $raw_data[ $foreign_key ];
}
}
}
}
}
return $data;
}
/**
* Deletes a note.
*
* @param int $id NoteID to be deleted.
* @return bool Did the deletion go right ?
*/
function deleteNote($id)
{
$return = false;
if ( $this->_link ) {
$return = mysql_query("DELETE FROM " . $this->config['table'] . " WHERE `" . $this->config['fields']['id'] . "`=" . $id . " LIMIT 1;");
}
return $return;
}
}
?>