*/ class ems_datasource_db_followup extends ems_datasource_db_base implements ems_datasource_interface_followup { protected $followupFields; protected $followupTable; /** * The constructor object for the datasource object. * * @param ems_framework_registry $param * @access public */ public function __construct(ems_framework_registry $param) { parent::__construct($param); $this->followupFields = "followup_id,followup_table_def_id,followup_ref_id,followup_original_poster_id,followup_owner_id,followup_content_id,followup_category_id,followup_follow_up_date,case when followup_is_public = true then 1 else 0 end as followup_is_public,followup_when_completed,followup_date_created,followup_date_last_edited,content_name as followup_content_name,content_text as followup_content_text"; $this->followupTable = "followup"; } /** * Retrieves a follow record from the database. Makes uses of the datasource caching system. * * @param integer * @access public * @returns array or false */ public function getFollowup($id) { if ($this->cache->check($this->followupTable,$id)) { return $this->cache->get($this->followupTable,$id); } else { $results = $this->getFollowupDB($id); if (is_array($results)) $this->cache->set($this->followupTable,$id,$results); return $results; } } /** * This actually performs the query to the database for a follow-up record if it's not in the cache * * @param integer * @access protected * @returns array or false */ protected function getFollowupDB($id) { $this->checkParam($id,"n","getFollowupDB - id"); $selectSQL = "select {$this->followupFields} from followup inner join content on (followup_content_id = content_id and true = content_is_active) where followup_is_active = true and followup_facility_id = %s and followup_id = %s"; $sql = sprintf($selectSQL,$this->param->getFacility()->getID(),$id); $results = $this->db->GetRow($sql); if (is_array($results) && count($results) > 0) { return $results; } else { return false; } } /** * Retrieves a matrix of follow-up data, filtered down by start and end dates, if they are complete, and the owner ID of the follow-ups. * Performs a cache operation on the data as well. * * @param integer,string,string,boolean * @access public * @returns array or false */ public function getFollowupsByOwner($id,$startDate = null,$endDate = null,$includeCompleted = false) { $this->checkParam($id,"n","getFollowupsByOwner - id"); $conditions = array(); if ($startDate) { $startDate = $this->prepareValue($startDate); $conditions[] = "followup_follow_up_date >= {$startDate}"; } if ($endDate) { $endDate = $this->prepareValue($endDate); $conditions[] = "followup_follow_up_date <= {$endDate}"; } if ($includeCompleted) { $conditions[] = "followup_when_completed is not null"; } else { $conditions[] = "followup_when_completed is null"; } if (count($conditions) > 0) { $conditionText = " and " . implode(" and ",$conditions); } else { $conditionText = ""; } $selectSQL = "select {$this->followupFields} from followup inner join content on (followup_content_id = content_id and true = content_is_active) where followup_is_active = true and followup_facility_id = %s and followup_owner_id = %s{$conditionText}"; $sql = sprintf($selectSQL,$this->param->getFacility()->getID(),$id); $results = $this->db->GetAll($sql); if (is_array($results) && count($results) > 0) { foreach ($results as $index=>$data) { $this->cache->set($this->followupTable,$data['followup_id'],$data); } return $results; } else { return false; } } /** * Retrieves a matrix of follow-up data for public viewing, filtered down by start and end dates, if they are complete, and a person ID who doesn't own the follow-ups * Performs a cache operation on the data as well. * * @param integer,string,string,boolean * @access public * @returns array or false */ public function getPublicFollowups($id,$startDate = null,$endDate = null,$includeCompleted = false) { $this->checkParam($id,"n","getPublicFollowups - id"); $conditions = array(); if ($startDate) { $startDate = $this->prepareValue($startDate); $conditions[] = "followup_follow_up_date >= {$startDate}"; } if ($endDate) { $endDate = $this->prepareValue($endDate); $conditions[] = "followup_follow_up_date <= {$endDate}"; } if ($includeCompleted) { $conditions[] = "followup_when_completed is not null"; } else { $conditions[] = "followup_when_completed is null"; } if (count($conditions) > 0) { $conditionText = " and " . implode(" and ",$conditions); } else { $conditionText = ""; } $selectSQL = "select {$this->followupFields} from followup inner join content on (followup_content_id = content_id and true = content_is_active) where followup_is_active = true and followup_facility_id = %s and followup_owner_id != %s{$conditionText}"; $sql = sprintf($selectSQL,$this->param->getFacility()->getID(),$id); $results = $this->db->GetAll($sql); if (is_array($results) && count($results) > 0) { foreach ($results as $index=>$data) { $this->cache->set($this->followupTable,$data['followup_id'],$data); } return $results; } else { return false; } } /** * Retrieves a matrix of follow-up data for the entity item that the follow-up is about. * Performs a cache operation on the data as well. * * @param integer,integer,string,string,boolean * @access public * @returns array or false */ public function getFollowupsByEntity($tableDefID,$refID,$startDate = null,$endDate = null,$includeCompleted = false) { $this->checkParam($tableDefID,"n","getFollowupsByEntity - table def"); $this->checkParam($refID,"n","getFollowupsByEntity - ref ID"); $conditions = array(); if ($startDate) { $startDate = $this->prepareValue($startDate); $conditions[] = "followup_follow_up_date >= {$startDate}"; } if ($endDate) { $endDate = $this->prepareValue($endDate); $conditions[] = "followup_follow_up_date <= {$endDate}"; } if ($includeCompleted) { $conditions[] = "followup_when_completed is not null"; } else { $conditions[] = "followup_when_completed is null"; } if (count($conditions) > 0) { $conditionText = " and " . implode(" and ",$conditions); } else { $conditionText = ""; } $selectSQL = "select {$this->followupFields} from followup inner join content on (followup_content_id = content_id and true = content_is_active) where followup_is_active = true and followup_facility_id = %s and followup_table_def_id = %s and followup_ref_id = %s{$conditionText}"; $sql = sprintf($selectSQL,$this->param->getFacility()->getID(),$tableDefID,$refID); $results = $this->db->GetAll($sql); if (is_array($results) && count($results) > 0) { foreach ($results as $index=>$data) { $this->cache->set($this->followupTable,$data['followup_id'],$data); } return $results; } else { return false; } } /** * Writes follow-up data to the database. Uses ADODB's replace system, which figures out to do an insert or update. * * @param ems_followup_interface_followup * @access public * @returns boolean */ public function saveFollowup($obj) { $results = array(); // we start with a transaction if we aren't in one if ($this->inTrans() === false) { $this->startTrans(); $processTrans = true; } // set the content ID if there isn't one if ($obj->getContentID === null) $obj->setContentID($this->db->GenID("content_seq")); // set the followup ID if we don't have one if ($obj->getID() === null) $obj->setID($this->db->GenID("followup_seq")); // set the content name if one isn't set if ($obj->getContentName() === null) $obj->setContentName("Followup ID {$obj->getID()}"); // add/update the content area $table = "content"; $fields = array ( "content_id"=>$obj->getContentID(), "content_name"=>$this->prepareValue($obj->getContentName()), "content_text"=>$this->prepareValue($obj->getContentText()), "content_system_owned"=>$this->prepareBool(false), "content_replaceable"=>$this->prepareBool(true), "content_who_last_edited"=>$this->param->getUser()->getID(), "content_date_last_edited"=>$this->db->DBTimeStamp(time()), "content_is_active"=>$this->prepareBool(true) ); if(!$this->idExists($table, array('content_id' => $obj->getContentID()))) { $fields['content_date_created'] = $this->db->DBTimeStamp(time()); $fields['content_who_created'] = $this->param->getUser()->getID(); } $pkey = "content_id"; $results[] = $this->saveData($table,$fields,$pkey); $table = $this->followupTable; $fields = array( "followup_id"=>$obj->getID(), "followup_facility_id"=>$this->param->getFacility()->getID(), "followup_table_def_id"=>$obj->getTableDefID(), "followup_ref_id"=>$obj->getRefID(), "followup_original_poster_id"=>$obj->getOriginalPosterID(), "followup_owner_id"=>$obj->getOwnerID(), "followup_content_id"=>$obj->getContentID(), "followup_category_id"=>$this->prepareValue($obj->getCategoryID()), "followup_follow_up_date"=>$this->prepareValue($obj->getFollowupDate()->getTS()), "followup_is_public"=>$this->prepareValue($obj->getIsPublic()), "followup_when_completed"=>$this->prepareValue($obj->getWhenCompleted()->getTS()), "followup_who_last_edited"=>$this->param->getUser()->getID(), "followup_date_last_edited"=>$this->db->DBTimeStamp(time()), "followup_is_active"=>$this->prepareBool(true) ); if(!$this->idExists($table, array('followup_id' => $obj->getID()))) { $fields['followup_date_created'] = $this->db->DBTimeStamp(time()); $fields['followup_who_created'] = $this->param->getUser()->getID(); } $pkey = "followup_id"; $results[] = $this->saveData($table,$fields,$pkey); if ($processTrans) $this->completeTrans(); if (in_array(false,$results)) { return false; } else { return true; } } /** * Marks a follow-up record as inactive in the database. * * @param ems_followup_interface_followup * @access public * @returns boolean */ public function deleteFollowup($obj) { $results = array(); if ($this->inTrans() === false) { $this->startTrans(); $processTrans = true; } $table = $this->followupTable; $prefix = "followup"; $conditions = array("followup_id"=>$obj->getID()); $results[] = $this->deleteData($table,$prefix,$conditions); $table = "content"; $prefix = "content"; $conditions = array("content_id"=>$obj->getContentID()); $results[] = $this->deleteData($table,$prefix,$conditions); if ($processTrans) $this->completeTrans(); if (in_array(false,$results)) { return false; } else { return true; } } } ?>