features/images/lib.php
<?php
class Images {
/** constants mapped from Persistence class */
const OK = Persistence::OK;
const ERROR = Persistence::ERROR;
/**
* get all image URLs associated with an event. The single image maps have
* the keys "id" and "image_link"
*
* @param $event_id - the numeric event id
* @param $conn - a PDO connection object
*
* @returns ( "status" => self::OK, "error" => "", "values" => array(images) ) on success
* and ( "status" => self::ERROR, "error" => "message", "values" => array() ) on failure
*/
static function get_images_for_event($event_id, $conn = null) {
$conn = $conn ?: Persistence::get_database_object();
$columns = array('id', 'image_link');
$table_name = 'images';
$where = array(
'postmortem_id' => $event_id,
'deleted' => 0,
);
if (is_null($conn)) {
return array("status" => self::ERROR,
"error" => "Couldn't get connection object.",
"values" => array());
}
return Persistence::get_array($columns, $where, $table_name, $conn);
}
/**
* save images belonging to a certain event to the database
*
* @param $event_id - numeric ID of the event to store for
* @param $images - array of image URLs to store
* @param $conn - a PDO connection object
*
* @returns ( "status" => self::OK ) on success
* or ( "status" => self::ERROR, "error" => "an error message" ) on failure
*/
static function save_images_for_event($event_id, $images, $conn = null) {
$conn = $conn ?: Persistence::get_database_object();
$table_name = 'images';
$assoc_column = 'image_link';
if (is_null($conn)) {
return array("status" => self::ERROR,
"error" => "Couldn't get connection object.");
}
return Persistence::store_array($table_name, $assoc_column, $images,
$event_id, $conn);
}
/**
* delete images belonging to a certain event to the database
*
* @param $event_id - numeric ID of the event to delete for
* @param $conn - a PDO connection object
*
* @returns ( "status" => self::OK ) on success
* or ( "status" => self::ERROR, "error" => "an error message" ) on failure
*/
static function delete_images_for_event($event_id, $conn = null) {
$conn = $conn ?: Persistence::get_database_object();
if (is_null($conn)) {
return array("status" => self::ERROR,
"error" => "Couldn't get connection object.");
}
return Persistence::flag_as_deleted('images', 'postmortem_id', $event_id, $conn);
}
/**
* function to get an image from the association table
*
* @param $id - ID to get
* @param $conn - PDO connection object (default: null)
*
* @returns ( "status" => self::OK, "value" => $row ) on success
* or ( "status" => self::ERROR, "error" => "an error message" ) on failure
*/
static function get_image($theid, $conn = null) {
$conn = $conn ?: Persistence::get_database_object();
$columns = array('id', 'image_link');
$table_name = 'images';
if (is_null($conn)) {
return array("status" => self::ERROR,
"error" => "Couldn't get connection object.");
}
return Persistence::get_association_by_id($columns, $table_name, $theid, $conn);
}
/**
* function to delete an image from the association table
*
* @param $id - ID to delete
* @param $conn - PDO connection object (default: null)
*
* @returns ( "status" => self::OK ) on success
* or ( "status" => self::ERROR, "error" => "an error message" ) on failure
*/
static function delete_image($theid, $conn = null) {
$conn = $conn ?: Persistence::get_database_object();
if (is_null($conn)) {
return array("status" => self::ERROR,
"error" => "Couldn't get connection object.");
}
return Persistence::flag_as_deleted('images', 'id', $theid, $conn);
}
/**
* function to UNdelete an image from the association table
*
* @param $id - ID to undelete
* @param $conn - PDO connection object (default: null)
*
* @returns ( "status" => self::OK ) on success
* or ( "status" => self::ERROR, "error" => "an error message" ) on failure
*/
static function undelete_image($theid, $conn = null) {
$conn = $conn ?: Persistence::get_database_object();
$table_name = 'images';
if (is_null($conn)) {
return array("status" => self::ERROR,
"error" => "Couldn't get connection object.");
}
return Persistence::flag_as_undeleted($table_name, 'postmortem_id', $theid, $conn);
}
}