* */ class ems_datasource_db_persongroup extends ems_datasource_db_base implements ems_datasource_interface_persongroup { /** * A comma seperated list of fields that are used in SELECT queries. This way you change the field list in only one place * * @var string * @access protected */ protected $groupFields; /** * The name of the person group table in the database * * @var string * @access protected */ protected $groupTable; /** * Constructor object for the class * * @param ems_framework_registry $param * @access public * @return void */ public function __construct(ems_framework_registry $param) { parent::__construct($param); $this->groupFields = "person_group_id,person_group_name,person_group_description,person_group_list_email"; $this->groupTable = "person_group"; } /** * Retrives a list of values for a given ID. If the values aren't in the cache system, then the it calls the database query method for the data. Returns a list upon success, false on an error. * * @param int $id * @access public * @return array or false */ public function getGroup($id) { if ($this->cache->exists($this->groupTable,$id)) { return $this->cache->get($this->groupTable,$id); } else { $results = $this->getGroupDB($id); if (is_array($results)) $this->cache->set($this->groupTable,$id,$results); return $results; } } /** * Queries the database for data based on the passed ID number. Returns an array on success, false on an error. * * @param int $id * @access public * @return array or false */ protected function getGroupDB($id) { $this->checkParam($id,"n","getGroupDB - id"); $selectSQL = "select {$this->groupFields} from {$this->groupTable} where person_group_is_active = true and person_group_facility_id = %s and person_group_id = %s"; $sql = sprintf($selectSQL,$this->param->getFacility()->getID(),$id); $result = $this->db->GetRow($sql); if (is_array($result) && count($result) > 0) { return $result; } else { return false; } } /** * Queries for all the groups in a particular facility. Places all the records into the cache system so when individual objects are generated, no additional queries are performed. Returns the matrix on success, false on an error. * * @param none * @access public * @return array or false */ public function getGroups() { $selectSQL = "select {$this->groupFields} from {$this->groupTable} where person_group_is_active = true and person_group_facility_id = %s"; $sql = sprintf($selectSQL,$this->param->getFacility()->getID()) $results = $this->db->GetAll($sql); if (is_array($results) && count($results) > 0) { foreach ($results as $index=>$data) { $this->cache->set($this->groupTable,$data['person_group_id'],$data); } return $results; } else { return false; } } /** * Takes a person group object and persists it to the database. Returns true on success, false on an error. It also starts and finishes a transaction if the system isn't already in one. * * @param none * @access public * @return boolean */ public function saveGroup(ems_person_interface_persongroup $group) { $results = array(); if ($this->inTrans() === false) { $this->startTrans(); $processTrans = true; } // add records to the person_group table if ($group->getID() === null) $group->setID($this->db->GenID("person_group_seq")); $fields = array ( "person_group_id"=>$group->getID(), "person_group_facility_id"=>$this->param->getFacility()->getID(), "person_group_name"=>$this->prepareValue($group->getName()), "person_group_description"=>$this->prepareValue($group->getDescription()), "person_group_list_email"=>$this->prepareValue($group->getListEmail()), "person_group_who_last_edited"=>$this->param->getUser()->getID(), "person_group_date_last_edited"=>$this->db->DBTimeStamp(time()), "person_group_is_active"=>$this->prepareBool(true) ); if (!$this->idExists($this->groupTable,array("person_group_id"=>$group->getID()))) { $fields['person_group_who_created'] = $this->param->getUser()->getID(); $fields['person_group_date_created'] = $this->db->DBTimeStamp(time()); } $pkey = "person_group_id"; $results[] = $this->saveData($this->groupTable,$fields,$pkey); if ($processTrans) $this->completeTrans(); if (in_array(false,$results)) { return false; } else { return true; } } /** * Removes the persistence record for a person group object. Returns true on success, false on an error. * * @param none * @access public * @return boolean */ public function deleteGroup(ems_person_interface_persongroup $group) { $prefix = "person_group"; $conditions = array("person_group_id"=>$group->getID()); return $this->deleteData($this->groupTable,$prefix,$conditions); } } ?>