[from now] 2012/05/07 09:36:07

diff --git a/legacy/autoload.php b/legacy/autoload.php
index 296f1da..a0b74bc 100644
--- a/legacy/autoload.php
+++ b/legacy/autoload.php
@@ -58,6 +58,7 @@ define( 'PSU_EXTERNAL_DIR', PSU_BASE_DIR . '/external' );
 includes_psu_register( 'PSU', PSU_LIB_DIR );
 includes_psu_register( 'Rave', PSU_LIB_DIR );
 includes_psu_register( 'Zend', PSU_EXTERNAL_DIR );
+includes_psu_register( 'Calllog', PSU_EXTERNAL_DIR );

 if( 'apache2handler' == php_sapi_name() ) {
 	$logger = new PSU\Error\Logger( '/var/log/php/deprecated.log' );
diff --git a/lib/Calllog.php b/lib/Calllog.php
new file mode 100644
index 0000000..8e98c0a
--- /dev/null
+++ b/lib/Calllog.php
@@ -0,0 +1,57 @@
+<?php
+
+/**
+ * CallLog API.
+ */
+class Calllog {
+	/**
+	 * Filter the ticket list.
+	 *
+	 * Valid $args:
+	 *
+	 * @li $q \b string match text in call history
+	 * @li $group \b int the assigned group
+	 */
+	public static function search_tickets( $args )
+	{
+		$args = PSU::params( $args );
+		extract( $args );
+
+		$where = array();
+		$sql_args = array();
+
+		if( $q ) {
+			$where[] = 'MATCH (comments) AGAINST (?)';
+			$sql_args[] = $q;
+		}
+
+		if( $group ) {
+			$where[] = 'its_assigned_group = ?';
+			$sql_args[] = $group;
+		}
+
+		if( $call_status ) {
+			$where[] = 'call_status = ?';
+			$sql_args[] = $call_status;
+		}
+
+		if( $current ) {
+			$where[] = 'current = ?';
+			$sql_args[] = $current;
+		}
+
+		$where_sql = implode(' AND ', $where);
+
+		$sql = "
+			SELECT *
+			FROM call_history
+			WHERE
+				$where_sql
+			ORDER BY
+				date_assigned DESC,
+				time_assigned DESC
+		";
+
+		return PSU::db('calllog')->GetAll($sql, $sql_args);
+	}//end search_tickets
+}//end Calllog
diff --git a/lib/Calllog/ActiveRecord.php b/lib/Calllog/ActiveRecord.php
new file mode 100644
index 0000000..7a533dc
--- /dev/null
+++ b/lib/Calllog/ActiveRecord.php
@@ -0,0 +1,171 @@
+<?php
+
+namespace Calllog;
+
+/*
+ * CallLog-specific Active Record pattern.
+ */
+abstract class ActiveRecord extends \PSU_DataObject implements \PSU\ActiveRecord {
+	static $rowcache = null;
+	static $table = null;
+
+	/*
+	 * Default constructor
+	 */
+	public function __construct( $row = null ) {
+		// Is row actually a row identifier
+		if( $row && ! is_array( $row ) ) {
+			$row = static::row( $row );
+		}
+		parent::__construct( $row );
+	}
+
+	/*
+	 * Implement ActiveRecord::delete(). Currently disabled.
+	 */
+	public function delete() {
+		$tableKey = static::$table_key;
+
+		$key = $this->$tableKey;
+
+		throw new \Exception( 'deletion is currently disabled' );
+
+		if( !static::$table )
+			throw new \Exception( 'static::$table must be defined' );
+		if( !static::$table_key )
+			throw new \Exception( 'static::$table_key must be defined' );
+
+		$table = static::$table;
+		$table_key = static::$table_key;
+
+		$sql = "DELETE FROM {$table} WHERE {$table_key} = ?";
+		$result = \PSU::db('calllog')->Execute( $sql, $this->$key );
+
+	}//end delete
+
+	/*
+	 * Get a single object of the static type.
+	 */
+	public static function get( $key ) {
+		static $cache = array();
+
+		// If the key is not a usable value throw an error
+		if( !is_scalar( $key ) ) {
+			throw new \InvalidArgumentException( 'key must be scalar' );
+		}//end if
+		$class_name = get_called_class();
+
+		$idx = "{$class_name}-{$key}-{$field->value}";
+
+		if( !isset( $cache[$idx] ) ) {
+			$args = null;		// Initiate the args
+
+			// Get the values
+			if( null !== self::$rowcache ) {
+				$args = self::$rowcache->get( get_called_class(),	$key, $key );
+			}
+
+			if( null === $args ) {
+				$args = $key;
+			}
+
+			$obj = new static( $args );
+
+			$cache[$idx] = $obj;
+		}
+		return $cache[$idx];
+	}//end get
+
+	/*
+	 * Return a record array from the data store, identified
+	 * by the given key
+	 *
+	 * @return array
+	 */
+	public static function row( $key ) {
+		$table = static::$table;
+
+		// Define the paremeter we are searching by
+		$tableKey = static::$table_key;
+
+		$where = "{$tableKey} = {$key}";
+
+		$sql = "SELECT * FROM {$table} WHERE {$tableKey} = ?";
+
+		$row = \PSU::db('calllog')->GetRow( $sql, $key );
+
+		if ( !$row ) {
+			return false;
+		}
+		return $row;
+	}//end row
+
+
+	/*
+	 * Take in an array of arguments that includes the key
+	 * Use that array to update the table row for that key
+	 */
+	public static function update( $args ) {
+		$table = static::$table;
+		$possible_keys = static::$possible_keys;
+		$tableKey = static::$table_key;
+
+		// Get all fields that were updated
+		$changed_values = "UPDATE {$table} SET ";
+		$values_array = array();
+		$arg_count = 0;
+
+		// Get all fields that were updated
+		foreach( $args as $key=>$value ) {
+			// Check to make sure it is a valid field, if not throw it away
+			// Cannot update the key for the record
+			if( in_array( $key, $possible_keys, true ) && $key != $tableKey ) {
+				if( $arg_count++ != 0 ) {
+					$changed_values .= ", ";
+				}
+				$changed_values .= "{$key} = ?";
+				$values_array[]  = $value;
+			}
+		}
+
+		// If there were no changes quit before the db call
+		if ( 0 == $arg_count ) {
+			return false;
+		}
+
+		// Verify the key exists before trying to update it
+		$probe = \PSU::db('calllog')->GetRow( "SELECT {$tableKey} FROM {$table} WHERE {$tableKey} = ?", $args[$tableKey] );
+		if ( !$probe ) {
+			return false;
+		}
+
+		// Add the table key at the end
+		$values_array[] = $args[$tableKey];
+
+		// Create the update statement
+		$changed_values .= " WHERE {$tableKey} = ?";
+		$result = \PSU::db('calllog')->Execute( $changed_values, $values_array );
+
+		if ( $result ) {
+			return true;
+		}
+		else {
+			return false;
+		}
+	}//end update
+
+
+	/*
+	 * Take in the Active record
+	 * Format the data in the record into an array
+	 * Send the data to the update function
+	 */
+	public function save( $method = 'merge' ) {
+		//throw new \Exception( 'saving is currently disabled' );
+
+		$update_array = $this->save_fields();
+
+		$this->update( $update_array );
+
+	}//end save
+}//end class ActiveRecord
diff --git a/lib/Calllog/Collection.php b/lib/Calllog/Collection.php
new file mode 100644
index 0000000..e2b9831
--- /dev/null
+++ b/lib/Calllog/Collection.php
@@ -0,0 +1,62 @@
+<?php
+
+namespace Calllog;
+
+/**
+ * CallLog-specific Collection.
+ */
+class Collection extends \PSU\Collection {
+	/*
+	 * Basic constructor
+	 */
+	public function __construct( $id = null ) {
+		$this->_collection_key = $id;
+	}
+
+	/*
+	 * Returns array of keys which can be used to add children to a collection
+	 */
+	public function get( ) {
+
+		// Make all the static values cleaner
+		$table = static::$table;
+		$possible_keys = static::$possible_keys;
+		$child_key = static::$child_key;
+
+		// If not array, default to the child key
+		if ( ! is_array( $this->_collection_key ) ) {
+			$collection = array( $child_key => $this->_collection_key );
+		}
+		else {
+			$collection = $this->_collection_key;
+		}
+
+		$arg_count = 0;
+		$where = '';
+		$value_array = array();
+
+		// Loop through to build the SQL WHERE
+		foreach( $collection as $key=>$value ) {
+			if ( in_array( $key, $possible_keys, true ) ) {
+				// Add an and to append each statement
+				if ( $arg_count++ != 0 ) {
+				    $where .= " AND ";
+				}
+
+				$where .= " {$key} = ?";
+				$value_array[] = $value;
+			}
+		}
+
+		// Query for the desired collection
+		$sql = "SELECT {$child_key} FROM {$table} WHERE {$where} ORDER BY {$child_key} DESC";
+		$call_entry = \PSU::db('calllog')->GetAll ( $sql, $value_array );
+
+		$ticketArr = array();
+		// Clean up the array
+		foreach ( $call_entry as $id ) {
+				$ticketArr[] = $id[$child_key];
+		}
+		return $ticketArr;
+	}
+}//end class Collection
diff --git a/lib/Calllog/Queue.php b/lib/Calllog/Queue.php
new file mode 100644
index 0000000..ab29354
--- /dev/null
+++ b/lib/Calllog/Queue.php
@@ -0,0 +1,79 @@
+<?php
+
+namespace Calllog;
+
+/**
+ * A queue of tickets.
+ */
+class Queue extends ActiveRecord {
+	static $table = 'itsgroups';
+	static $_name = 'Queue';
+	static $table_key = 'itsgroupid';
+
+	// Define all possible keys for ticket
+	static $ticket_possible_keys = array(
+		'wp_id', 'pidm', 'call_status',
+		'caller_username', 'caller_first_name',
+		'caller_last_name', 'caller_phone_number',
+		'calllog_username', 'call_type',
+		'keywords', 'call_time', 'call_date',
+		'other', 'location_building_id',
+		'location_building_room_number',
+		'location_call_logged_from', 'title',
+		'feelings_face', 'feelings'
+	);
+
+	// Define all possible keys for history
+	static $history_possible_keys = array(
+		'call_id', 'current', 'updated_by',
+		'tlc_assigned_to', 'its_assigned_group',
+		'comments', 'datetime_assigned',
+		'date_assigned', 'time_assigned',
+		'call_status', 'call_priority'
+	);
+
+
+	/*
+	 * Get all open calls for the ITS Group
+	 */
+	public function get_open_calls ( ) {
+		// Set up the parameters for the search
+		$params = array(
+			'call_status' => 'open',
+			'its_assigned_group' => $this->itsgroupid,
+		);
+
+		$open_tickets = Queue::search( $params );
+
+		return $open_tickets;
+	}
+
+	/*
+	 * Returns an array of tickets based on the search parameters
+	 */
+	public static function search( $param ) {
+		$ticket_possible_keys = static::$ticket_possible_keys;
+		$history_possible_keys = static::$history_possible_keys;
+
+		// Build a search that will only look at the most recent history
+		$sql = "SELECT * FROM call_log, call_history WHERE call_log.call_id = call_history.call_id AND call_history.current='1' ";
+
+		$values_array = array();
+
+		// Loop through to build the SQL WHERE
+		foreach( $param as $key=>$value ) {
+			// See if the key is valid, only take call status from history
+			if ( in_array( $key, $ticket_possible_keys, true ) && 'call_status' != $key ) {
+				$sql .= " AND call_log.{$key} = ?";
+				$values_array[] = $value;
+			}
+			elseif ( in_array( $key, $history_possible_keys, true ) ) {
+				$sql .= " AND call_history.{$key} = ?";
+				$values_array[] = $value;
+			}
+		}
+
+		$result = \PSU::db('calllog')->GetAll($sql, $values_array );
+		return $result;
+	}
+}//end class Queue
diff --git a/lib/Calllog/Ticket.php b/lib/Calllog/Ticket.php
new file mode 100644
index 0000000..28543f2
--- /dev/null
+++ b/lib/Calllog/Ticket.php
@@ -0,0 +1,111 @@
+<?php
+
+namespace Calllog;
+
+/*
+ * A single ticket object.
+ */
+class Ticket extends ActiveRecord {
+	static $table = 'call_log';
+	static $_name = 'Ticket';
+	static $table_key = 'call_id';
+
+	// Create an array of possible fields
+	static $possible_keys = array(
+		'call_id', 'wp_id', 'pidm', 'call_status',
+		'caller_username', 'caller_first_name',
+		'caller_last_name', 'caller_phone_number',
+		'calllog_username', 'call_type',
+		'keywords', 'call_time', 'call_date',
+		'other', 'location_building_id',
+		'location_building_room_number',
+		'location_call_logged_from', 'title',
+		'feelings_face', 'feelings'
+	);
+
+
+	/*
+	 * Create a new ticket
+	 *  Take in the user values and create a new database entry
+	 *  Once the entry is in the database create a new ticket item
+	 */
+	public static function create_new_ticket ( $args ) {
+		$table = static::$table;
+		$possible_keys = static::$possible_keys;
+
+		// Get the date and the time
+		if ( null == $args['call_date'] && null == $args['call_time'] ) {
+			$args['call_time'] = date("H:i:00");
+			$args['call_date'] = date("Y-m-d");
+		}
+
+		// When a ticket is created defaults to open
+		if ( null == $args['call_status'] ) {
+			$args['call_status'] = 'open';
+		}
+
+		// Initialize all the query construction variables
+		$insert_string = "INSERT INTO {$table} (";
+		$values_string = 'VALUES (';
+		$values_array = array();
+		$cnt = 0;
+
+		// Get all fields that were updated
+		foreach( $args as $key=>$value ) {
+			// Check to make sure it is a valid field, if not throw it away
+			if( in_array( $key, $possible_keys, true )  ) {
+				// Only add a comma if there is a value before it
+				if( $cnt++ != 0 ) {
+					$insert_string .= ", ";
+					$values_string .= ", ";
+				}
+				$insert_string .= "{$key}";
+				$values_string .= "?";
+				$values_array[] = $value;
+			}
+		}
+
+		$updateSQL = "{$insert_string}) {$values_string})";
+
+		$result = \PSU::db('calllog')->Execute( $updateSQL, $values_array );
+		$call_id = \PSU::db('calllog')->Insert_ID();
+
+		// If success create a new update as well
+		if ( $result ) {
+			$args['date_assigned'] = $args['call_date'];
+			$args['time_assigned'] = $args['call_time'];
+			$args['datetime_assigned'] = $args['call_date'] . ' ' . $args['call_time'];
+			$args['call_id'] = $call_id;
+
+			$origin_update = Update::create_new_update($args);
+		}
+		// Create a new Ticket object
+		$ticket = new Ticket($call_id);
+		return $ticket;
+	}
+
+	public function save_fields() {
+		// Prepare the record to be sent as an update()
+		$update_array = array (
+			'call_id'						=> $this->call_id,
+			'wp_id'						=> $this->wp_id,
+			'pidm'						=> $this->pidm,
+			'call_status'					=> $this->call_status,
+			'caller_username' 				=> $this->caller_username,
+			'caller_first_name' 			=> $this->caller_first_name,
+			'caller_last_name' 				=> $this->caller_last_name,
+			'caller_phone_number' 			=> $this->one_number,
+			'call_type' 					=> $this->call_type,
+			'call_date'					=> $this->call_date,
+			'keywords'					=> $this->keywords,
+			'other'						=> $this->other,
+			'location_building_id' 			=> $this->location_building_id,
+			'location_building_room_number'	=> $this->location_building_room_number,
+			'location_call_logged_from'		=> $this->location_call_logged_from,
+			'title'						=> $this->title,
+			'feelings_face'				=> $this->feelings_face,
+			'feelings'					=> $this->feelings,
+		);
+		return $update_array;
+	}
+}//end class Ticket
diff --git a/lib/Calllog/Tickets.php b/lib/Calllog/Tickets.php
new file mode 100644
index 0000000..314090c
--- /dev/null
+++ b/lib/Calllog/Tickets.php
@@ -0,0 +1,28 @@
+<?php
+
+namespace Calllog;
+
+/**
+ * A group of tickets.
+ */
+class Tickets extends Collection {
+	static $_name = 'Tickets';
+	static $child = '\Calllog\Ticket';
+	static $child_key = 'call_id';
+	static $parent_key = 'call_id';
+	static $table = 'call_log';
+
+	// Create an array of possible fields
+	static $possible_keys = array(
+		'call_id', 'wp_id', 'pidm', 'call_status',
+		'caller_username', 'caller_first_name',
+		'caller_last_name', 'caller_phone_number',
+		'calllog_username', 'call_type',
+		'keywords', 'call_time', 'call_date',
+		'other', 'location_building_id',
+		'location_building_room_number',
+		'location_call_logged_from', 'title',
+		'feelings_face', 'feelings'
+	);
+
+}//end class Ticket
diff --git a/lib/Calllog/Update.php b/lib/Calllog/Update.php
new file mode 100644
index 0000000..1367617
--- /dev/null
+++ b/lib/Calllog/Update.php
@@ -0,0 +1,102 @@
+<?php
+
+namespace Calllog;
+
+/**
+ * An update to a ticket.
+ */
+class Update extends ActiveRecord {
+	static $table = 'call_history';
+	static $_name = 'Ticket';
+	static $table_key = 'id';
+
+	// Create an array of possible fields
+	static $possible_keys = array(
+		'call_id', 'current', 'updated_by',
+		'tlc_assigned_to', 'its_assigned_group',
+		'comments', 'datetime_assigned',
+		'date_assigned', 'time_assigned',
+		'call_status', 'call_priority'
+	);
+
+
+	/*
+	 * Take in changed fields through args
+	 * Check for validity then put into DB
+	 */
+	public function create_new_update( $args ) {
+		$table = static::$table;
+		$possible_keys = static::$possible_keys;
+
+		// Fill in previous data gaps if possible
+		if ( null == $args['datetime_assigned'] ) {
+			// If possible build date-time from other values
+			if ( null != $args['date_assigned'] && null != $args['time_assigned'] ) {
+				$args['datetime_assigned'] = "{$args['date_assigned']} {$args['time_assigned']}";
+			}
+			// If no time default to 00:00:00
+			elseif ( null != $args['date_assigned'] ) {
+				$args['datetime_assigned'] = "{$args['date_assigned']} 00:00:00";
+			}
+		}
+
+		// Prep args to create a new
+		$insert_string = "INSERT INTO {$table} (";
+		$values_string = "VALUES (";
+		$values_array  = array();
+		$cnt = 0;
+
+		// Get all fields that were updated
+		foreach( $args as $key=>$value ) {
+			// Check to make sure it is a valid field, if not throw it away
+			if( in_array( $key, $possible_keys, true ) && $value != null  ) {
+				// Only add a comma if there is a value before it
+				if( $cnt++ != 0 ) {
+					$insert_string .= ", ";
+					$values_string .= ", ";
+				}
+				$insert_string .= $key;
+				$values_string .= "?";
+				$values_array[] = $value;
+			}
+		}
+
+		$updateSQL = "{$insert_string} ) {$values_string} )";
+		$result = \PSU::db('calllog')->Execute( $updateSQL, $values_array );
+		$ticket_id = \PSU::db('calllog')->Insert_ID();
+
+		// Create a new update object
+		if ( $result ) {
+			$update = new Update($ticket_id);
+			return $update;
+		}
+		return false;
+
+		// Set the values up into a string to merge
+	}//end update
+
+	/*
+	 * Return an array of fields of
+	 * the object to update
+	 */
+	public function save_fields() {
+
+		$update_array = array(
+			'call_id'				=> $this->call_id,
+			'current'				=> $this->current,
+			'updated_by'			=> $this->updated_by,
+			'tlc_assigned_to'		=> $this->tlc_assigned_to,
+			'its_assigned_group'	=> $this->its_assigned_group,
+			'comments'			=> $this->comments,
+			'datetime_assigned'		=> $this->datetime_assigned,
+			'date_assigned'		=> $this->date_assigned,
+			'time_assigned'		=> $this->time_assigned,
+			'call_status'			=> $this->call_status,
+			'call_priority'		=> $this->call_priority,
+		);
+
+		return $update_array;
+	}
+
+
+}//end class Update
diff --git a/lib/Calllog/Updates.php b/lib/Calllog/Updates.php
new file mode 100644
index 0000000..5e5c57e
--- /dev/null
+++ b/lib/Calllog/Updates.php
@@ -0,0 +1,23 @@
+<?php
+
+namespace Calllog;
+
+/**
+ *
+ */
+class Updates extends Collection {
+	static $child = '\Calllog\Update';
+	static $child_key = 'id';
+	static $parent_key = 'call_history_system_id';
+	static $table = 'call_history';
+
+	// Create an array of possible fields
+	static $possible_keys = array(
+		'call_id', 'current', 'updated_by',
+		'tlc_assigned_to', 'its_assigned_group',
+		'comments', 'datetime_assigned',
+		'date_assigned', 'time_assigned',
+		'call_status', 'call_priority'
+	);
+
+}//end class Updates
diff --git a/webapp/calllog/includes/CallLog.class.php b/webapp/calllog/includes/CallLog.class.php
deleted file mode 100644
index 6deb91e..0000000
--- a/webapp/calllog/includes/CallLog.class.php
+++ /dev/null
@@ -1,60 +0,0 @@
-<?php
-
-require_once('PSUTools.class.php');
-
-/**
- * CallLog API.
- */
-class CallLog
-{
-	/**
-	 * Filter the ticket list.
-	 *
-	 * Valid $args:
-	 *
-	 * @li $q \b string match text in call history
-	 * @li $group \b int the assigned group
-	 */
-	public static function search_tickets( $args )
-	{
-		$args = PSU::params( $args );
-		extract( $args );
-
-		$where = array();
-		$sql_args = array();
-
-		if( $q ) {
-			$where[] = 'MATCH (comments) AGAINST (?)';
-			$sql_args[] = $q;
-		}
-
-		if( $group ) {
-			$where[] = 'its_assigned_group = ?';
-			$sql_args[] = $group;
-		}
-
-		if( $call_status ) {
-			$where[] = 'call_status = ?';
-			$sql_args[] = $call_status;
-		}
-
-		if( $current ) {
-			$where[] = 'current = ?';
-			$sql_args[] = $current;
-		}
-
-		$where_sql = implode(' AND ', $where);
-
-		$sql = "
-			SELECT *
-			FROM call_history
-			WHERE
-				$where_sql
-			ORDER BY
-				date_assigned DESC,
-				time_assigned DESC
-		";
-
-		return PSU::db('calllog')->GetAll($sql, $sql_args);
-	}//end search_tickets
-}//end CallLog
diff --git a/webapp/calllog/search.html b/webapp/calllog/search.html
index beb0f2a..0830871 100644
--- a/webapp/calllog/search.html
+++ b/webapp/calllog/search.html
@@ -9,7 +9,7 @@ undo_magic_quotes( $_GET );
 $title = 'Search';

 if( count($_GET) ) {
-	$tickets = CallLog::search_tickets( $_GET );
+	$tickets = Calllog::search_tickets( $_GET );

 	$title .= sprintf(' "%s"', htmlentities($_GET['q']));