diff --git a/classes/Map.php b/Models/Map.php similarity index 68% rename from classes/Map.php rename to Models/Map.php index 16a2b95..404e8b6 100644 --- a/classes/Map.php +++ b/Models/Map.php @@ -1,16 +1,18 @@ */ -class Map +namespace Application\Models; + +class Map extends ActiveRecord { public static $extensions = array( - 'jpg'=>array('mime_type'=>'image/jpeg','media_type'=>'image'), - 'gif'=>array('mime_type'=>'image/gif','media_type'=>'image'), - 'png'=>array('mime_type'=>'image/png','media_type'=>'image'), - 'tiff'=>array('mime_type'=>'image/tiff','media_type'=>'image') + 'jpg' => ['mime_type'=>'image/jpeg', 'media_type'=>'image'], + 'gif' => ['mime_type'=>'image/gif' , 'media_type'=>'image'], + 'png' => ['mime_type'=>'image/png' , 'media_type'=>'image'], + 'tiff' => ['mime_type'=>'image/tiff', 'media_type'=>'image'] ); /** @@ -18,12 +20,12 @@ class Map * @param array|string $file Either an entry from $_FILES or a path to a file * @param string $newName The filename to use */ - public static function saveFile($directory,$file,$newName) + public static function saveFile($directory, $file, $newName) { // Handle passing in either a $_FILES array or just a path to a file $tempFile = is_array($file) ? $file['tmp_name'] : $file; if (!$tempFile) { - throw new Exception('media/uploadFailed'); + throw new \Exception('media/uploadFailed'); } # Find out the mime type for this file @@ -33,7 +35,7 @@ class Map // Make sure it's a known file type if (!array_key_exists(strtolower($extension),self::$extensions)) { - throw new Exception('unknownFileType'); + throw new \Exception('unknownFileType'); } // Clean out any previous version of the file @@ -50,7 +52,7 @@ class Map chmod($newFile,0666); if (!is_file($newFile)) { - throw new Exception('media/uploadFailed'); + throw new \Exception('media/uploadFailed'); } } } diff --git a/classes/Person.php b/classes/Person.php deleted file mode 100644 index 1aef596..0000000 --- a/classes/Person.php +++ /dev/null @@ -1,226 +0,0 @@ - - */ -class Person -{ - private $id; - private $firstname; - private $lastname; - private $email; - - private $user_id; - private $user; - - /** - * Populates the object with data - * - * Passing in an associative array of data will populate this object without - * hitting the database. - * - * Passing in a scalar will load the data from the database. - * This will load all fields in the table as properties of this class. - * You may want to replace this with, or add your own extra, custom loading - * - * @param int|string|array $id (ID, email, username) - */ - public function __construct($id=null) - { - if ($id) { - if (is_array($id)) { - $result = $id; - } - else { - $zend_db = Database::getConnection(); - if (ctype_digit($id)) { - $sql = 'select * from people where id=?'; - } - elseif (false !== strpos($id,'@')) { - $sql = 'select * from people where email=?'; - } - else { - $sql = 'select p.* from people p left join users on p.id=person_id where username=?'; - } - $result = $zend_db->fetchRow($sql,array($id)); - } - - if ($result) { - foreach ($result as $field=>$value) { - if ($value) { - $this->$field = $value; - } - } - } - else { - throw new Exception('people/unknownPerson'); - } - } - else { - // This is where the code goes to generate a new, empty instance. - // Set any default values for properties that need it here - } - } - - /** - * Throws an exception if anything's wrong - * @throws Exception $e - */ - public function validate() - { - // Check for required fields here. Throw an exception if anything is missing. - if (!$this->firstname || !$this->lastname) { - throw new Exception('missingRequiredFields'); - } - } - - /** - * Saves this record back to the database - */ - public function save() - { - $this->validate(); - - $data = array(); - $data['firstname'] = $this->firstname; - $data['lastname'] = $this->lastname; - $data['email'] = $this->email ? $this->email : null; - - if ($this->id) { - $this->update($data); - } - else { - $this->insert($data); - } - } - - private function update($data) - { - $zend_db = Database::getConnection(); - $zend_db->update('people',$data,"id={$this->id}"); - } - - private function insert($data) - { - $zend_db = Database::getConnection(); - $zend_db->insert('people',$data); - $this->id = $zend_db->lastInsertId('people','id'); - } - - //---------------------------------------------------------------- - // Generic Getters - //---------------------------------------------------------------- - /** - * @return int - */ - public function getId() - { - return $this->id; - } - - /** - * @return string - */ - public function getFirstname() - { - return $this->firstname; - } - - /** - * @return string - */ - public function getLastname() - { - return $this->lastname; - } - - /** - * @return string - */ - public function getEmail() - { - return $this->email; - } - - //---------------------------------------------------------------- - // Generic Setters - //---------------------------------------------------------------- - /** - * @param string $string - */ - public function setFirstname($string) - { - $this->firstname = trim($string); - } - - /** - * @param string $string - */ - public function setLastname($string) - { - $this->lastname = trim($string); - } - - /** - * @param string $string - */ - public function setEmail($string) - { - $this->email = trim($string); - } - - - //---------------------------------------------------------------- - // Custom Functions - // We recommend adding all your custom code down here at the bottom - //---------------------------------------------------------------- - /** - * @return string - */ - public function getFullname() - { - return "{$this->firstname} {$this->lastname}"; - } - - /** - * @return string - */ - public function getURL() - { - return BASE_URL.'/people/viewPerson.php?person_id='.$this->id; - } - - /** - * @return int - */ - public function getUser_id() - { - if (!$this->user_id) { - $zend_db = Database::getConnection(); - $this->user_id = $zend_db->fetchOne('select id from users where person_id=?',$this->id); - } - return $this->user_id; - } - - /** - * @return User - */ - public function getUser() { - if (!$this->user) { - if ($this->getUser_id()) { - $this->user = new User($this->getUser_id()); - } - } - return $this->user; - } - - /** - * @return string - */ - public function getUsername() { - if ($this->getUser()) { - return $this->getUser()->getUsername(); - } - } -} diff --git a/classes/PersonList.php b/classes/PersonList.php deleted file mode 100644 index da71fc1..0000000 --- a/classes/PersonList.php +++ /dev/null @@ -1,59 +0,0 @@ - - */ -class PersonList extends ZendDbResultIterator -{ - /** - * @param array $fields - */ - public function __construct($fields=null) - { - parent::__construct(); - if (is_array($fields)) { - $this->find($fields); - } - } - - /** - * Populates the collection - * - * @param array $fields - * @param string|array $order Multi-column sort should be given as an array - * @param int $limit - * @param string|array $groupBy Multi-column group by should be given as an array - */ - public function find($fields=null,$order='lastname',$limit=null,$groupBy=null) - { - $this->select->from('people'); - - if (count($fields)) { - foreach ($fields as $key=>$value) { - $this->select->where("$key=?",$value); - } - } - - $this->select->order($order); - if ($limit) { - $this->select->limit($limit); - } - if ($groupBy) { - $this->select->group($groupBy); - } - $this->populateList(); - } - - /** - * Loads a single Person object for the row returned from ZendDbResultIterator - * - * @param array $key - */ - protected function loadResult($key) - { - return new Person($this->result[$key]); - } -} diff --git a/classes/Role.php b/classes/Role.php deleted file mode 100644 index f535eb6..0000000 --- a/classes/Role.php +++ /dev/null @@ -1,142 +0,0 @@ - - */ -class Role -{ - private $id; - private $name; - - /** - * Passing in an associative array of data will populate this object without - * hitting the database. - * - * Passing in an int will load the data from the database for the given ID. - * - * This will load all fields in the table as properties of this class. - * You may want to replace this with, or add your own extra, custom loading - * - * @param int|string|array $id - */ - public function __construct($id=null) - { - if ($id) { - if (is_array($id)) { - $result = $id; - } - else { - $zend_db = Database::getConnection(); - - if (is_int($id) || ctype_digit($id)) { - $sql = 'select * from roles where id=?'; - } - else { - $sql = 'select * from roles where name=?'; - } - $result = $zend_db->fetchRow($sql,array($id)); - } - if ($result) { - foreach ($result as $field=>$value) { - if ($value) { - $this->$field = $value; - } - } - } - else { - throw new Exception('roles/unknownRole'); - } - } - else { - // This is where the code goes to generate a new, empty instance. - // Set any default values for properties that need it here - } - } - - /** - * Throws an exception if theres anything wrong - * @throws Exception - */ - public function validate() - { - if (!$this->name) { - throw new Exception('missingName'); - } - } - - /** - * This generates generic SQL that should work right away. - * You can replace this $fields code with your own custom SQL - * for each property of this class, - */ - public function save() - { - $this->validate(); - - $data = array(); - $data['name'] = $this->name; - - - if ($this->id) { - $this->update($data); - } - else { - $this->insert($data); - } - } - - private function update($data) - { - $zend_db = Database::getConnection(); - $zend_db->update('roles',$data,"id={$this->id}"); - } - - private function insert($data) - { - $zend_db = Database::getConnection(); - $zend_db->insert('roles',$data); - $this->id = $zend_db->lastInsertId('roles','id'); - } - - //---------------------------------------------------------------- - // Generic Getters - //---------------------------------------------------------------- - /** - * @return int - */ - public function getId() - { - return $this->id; - } - /** - * @return string - */ - public function getName() - { - return $this->name; - } - - //---------------------------------------------------------------- - // Generic Setters - //---------------------------------------------------------------- - /** - * @param string $string - */ - public function setName($string) - { - $this->name = trim($string); - } - - //---------------------------------------------------------------- - // Custom Functions - // We recommend adding all your custom code down here at the bottom - //---------------------------------------------------------------- - /** - * @return string - */ - public function __toString() - { - return $this->name; - } -} diff --git a/classes/RoleList.php b/classes/RoleList.php deleted file mode 100644 index e70d8b7..0000000 --- a/classes/RoleList.php +++ /dev/null @@ -1,58 +0,0 @@ - -*/ -class RoleList extends ZendDbResultIterator -{ - /** - * @param array $fields - */ - public function __construct($fields=null) - { - parent::__construct(); - - if (is_array($fields)) { - $this->find($fields); - } - } - - /** - * Populates the collection - * - * @param array $fields - * @param string|array $order Multi-column sort should be given as an array - * @param int $limit - * @param string|array $groupBy Multi-column group by should be given as an array - */ - public function find($fields=null,$order='name',$limit=null,$groupBy=null) - { - $this->select->from('roles'); - - if (count($fields)) { - foreach ($fields as $key=>$value) { - $this->select->where("$key=?",$value); - } - } - - $this->select->order($order); - if ($limit) { - $this->select->limit($limit); - } - if ($groupBy) { - $this->select->group($groupBy); - } - $this->populateList(); - } - - /** - * Load each Role object as we iterate through the results - * - * @return array An array of Role objects - */ - protected function loadResult($key) - { - return new Role($this->result[$key]); - } -} diff --git a/classes/User.php b/classes/User.php deleted file mode 100644 index 4da3751..0000000 --- a/classes/User.php +++ /dev/null @@ -1,387 +0,0 @@ - - */ -class User extends SystemUser -{ - private $id; - private $person_id; - private $username; - private $password; - private $authenticationMethod; - - private $person; - private $roles = array(); - private $newPassword; // the User's new password, unencrypted - - /** - * Should provide the list of methods supported - * - * There should always be at least one method, called "local" - * Additional methods must match classes that implement External Identities - * See: ExternalIdentity.php - * - * @return array - */ - public static function getAuthenticationMethods() - { - return array('local','Employee'); - } - - /** - * @param int|string $id - */ - public function __construct($id = null) - { - if ($id) { - if (is_array($id)) { - $result = $id; - } - else { - if (ctype_digit($id)) { - $sql = 'select * from users where id=?'; - } - else { - $sql = 'select * from users where username=?'; - } - $zend_db = Database::getConnection(); - $result = $zend_db->fetchRow($sql,array($id)); - } - if ($result) { - foreach ($result as $field=>$value) { - if ($value) { - $this->$field = $value; - } - } - } - else { - throw new Exception('users/unknownUser'); - } - } - else { - // This is where the code goes to generate a new, empty instance. - // Set any default values for properties that need it here - } - } - - /** - * Throws an exception if anything's wrong - * @throws Exception $e - */ - public function validate() - { - if (!$this->person_id) { - throw new Exception('users/missingPerson_id'); - } - if (!$this->username) { - throw new Exception('users/missingUsername'); - } - - } - - /** - * Saves this record back to the database - * - * This generates generic SQL that should work right away. - * You can replace this $fields code with your own custom SQL - * for each property of this class, - */ - public function save() - { - $this->validate(); - - $data = array(); - $data['person_id'] = $this->person_id; - $data['username'] = $this->username; - // Passwords should not be updated by default. Use the savePassword() function - $data['authenticationMethod'] = $this->authenticationMethod - ? $this->authenticationMethod - : null; - - // Do the database calls - if ($this->id) { - $this->update($data); - } - else { - $this->insert($data); - } - - // Save the password only if it's changed - if ($this->passwordHasChanged()) { - $this->savePassword(); - } - - $this->updateRoles(); - } - - private function update($data) - { - $zend_db = Database::getConnection(); - $zend_db->update('users',$data,"id={$this->id}"); - } - - private function insert($data) - { - $zend_db = Database::getConnection(); - $zend_db->insert('users',$data); - $this->id = $zend_db->lastInsertId('users','id'); - } - - /** - * Removes this object from the database - */ - public function delete() - { - $zend_db = Database::getConnection(); - $zend_db->delete('user_roles',"user_id={$this->id}"); - $zend_db->delete('users',"id={$this->id}"); - } - - //---------------------------------------------------------------- - // Generic Getters - //---------------------------------------------------------------- - /** - * @return int - */ - public function getId() - { - return $this->id; - } - /** - * @return int - */ - public function getPerson_id() - { - return $this->person_id; - } - /** - * @return string - */ - public function getUsername() - { - return $this->username; - } - /** - * @return string - */ - public function getAuthenticationMethod() - { - return $this->authenticationMethod; - } - /** - * @return Person - */ - public function getPerson() - { - if ($this->person_id) { - if (!$this->person) { - $this->person = new Person($this->person_id); - } - return $this->person; - } - return null; - } - - //---------------------------------------------------------------- - // Generic Setters - //---------------------------------------------------------------- - /** - * @param int $int - */ - public function setPerson_id($int) - { - $this->person = new Person($int); - $this->person_id = $int; - } - /** - * @param string $string - */ - public function setUsername($string) - { - $this->username = trim($string); - } - /** - * Takes a user-given password and converts it to an MD5 Hash - * @param String $string - */ - public function setPassword($string) - { - // Save the user given password, so we can update it externally, if needed - $this->newPassword = trim($string); - $this->password = md5(trim($string)); - } - /** - * Takes a pre-existing MD5 hash - * @param MD5 $hash - */ - public function setPasswordHash($hash) - { - $this->password = trim($hash); - } - /** - * @param string $authenticationMethod - */ - public function setAuthenticationMethod($string) - { - $this->authenticationMethod = $string; - if ($this->authenticationMethod != 'local') { - $this->password = null; - $this->saveLocalPassword(); - } - } - /** - * @param Person $person - */ - public function setPerson($person) - { - $this->person_id = $person->getId(); - $this->person = $person; - } - - //---------------------------------------------------------------- - // Custom Functions - // We recommend adding all your custom code down here at the bottom - //---------------------------------------------------------------- - /** - * @return string - */ - public function getFirstname() - { - return $this->getPerson()->getFirstname(); - } - /** - * @return string - */ - public function getLastname() - { - return $this->getPerson()->getLastname(); - } - /** - * @return string - */ - public function getEmail() - { - return $this->getPerson()->getEmail(); - } - /** - * Returns an array of Role names with the role id as the array index - * - * @return array - */ - public function getRoles() - { - if (!count($this->roles)) { - if ($this->id) { - $zend_db = Database::getConnection(); - $select = new Zend_Db_Select($zend_db); - $select->from('user_roles','role_id') - ->joinLeft('roles','role_id=id','name') - ->where('user_id=?'); - $result = $zend_db->fetchAll($select,$this->id); - - foreach ($result as $row) { - $this->roles[$row['role_id']] = $row['name']; - } - } - } - return $this->roles; - } - /** - * Takes an array of role names. Loads the Roles from the database - * - * @param array $roleNames An array of names - */ - public function setRoles($roleNames) - { - $this->roles = array(); - foreach ($roleNames as $name) { - $role = new Role($name); - $this->roles[$role->getId()] = $role->getName(); - } - } - /** - * Takes a string or an array of strings and checks if the user has that role - * - * @param Array|String $roles - * @return boolean - */ - public function hasRole($roles) - { - if (is_array($roles)) { - foreach ($roles as $roleName) { - if (in_array($roleName,$this->getRoles())) { - return true; - } - } - return false; - } - else { - return in_array($roles,$this->getRoles()); - } - } - - /** - * Saves the current roles back to the database - */ - private function updateRoles() - { - $zend_db = Database::getConnection(); - - $roles = $this->getRoles(); - - $zend_db->delete('user_roles',"user_id={$this->id}"); - - foreach ($roles as $id=>$name) { - $data = array('user_id'=>$this->id,'role_id'=>$id); - $zend_db->insert('user_roles',$data); - } - } - - /** - * Since passwords can be stored externally, we only want to bother trying - * to save them when they've actually changed - * @return boolean - */ - public function passwordHasChanged() - { - return $this->newPassword ? true : false; - } - - /** - * Callback function from the SystemUser class - * The SystemUser will determine where the password should be stored. - * If the password is stored locally, it will call this function - */ - protected function saveLocalPassword() - { - if ($this->id) { - $zend_db = Database::getConnection(); - - // Passwords in the class should already be MD5 hashed - $zend_db->update('users',array('password'=>$this->password),"id={$this->id}"); - } - } - - /** - * Callback function from the SystemUser class - * - * The SystemUser class will determine where the authentication - * should occur. If the user should be authenticated locally, - * this function will be called. - * - * @param string $password - * @return boolean - */ - protected function authenticateDatabase($password) - { - $zend_db = Database::getConnection(); - - $md5 = md5($password); - - $id = $zend_db->fetchOne('select id from users where username=? and password=?', - array($this->username,$md5)); - return $id ? true : false; - } -} diff --git a/classes/UserList.php b/classes/UserList.php deleted file mode 100644 index a6ecca6..0000000 --- a/classes/UserList.php +++ /dev/null @@ -1,112 +0,0 @@ - - */ -class UserList extends ZendDbResultIterator -{ - private $columns = array('id','person_id','username','password','authenticationMethod'); - - /** - * @param array $fields - */ - public function __construct($fields=null) - { - parent::__construct(); - - if (is_array($fields)) { - $this->find($fields); - } - } - - /** - * Populates the collection - * - * @param array $fields - * @param string|array $order Multi-column sort should be given as an array - * @param int $limit - * @param string|array $groupBy Multi-column group by should be given as an array - */ - public function find($fields=null,$order='username',$limit=null,$groupBy=null) - { - $this->select->from(array('u'=>'users')); - - // Finding on fields from the Users table is handled here - if (count($fields)) { - foreach ($fields as $key=>$value) { - if (array_key_exists($key,$this->columns)) { - $this->select->where("u.$key=?",$value); - } - } - } - - // Finding on fields from other tables requires joining those tables. - // You can handle fields from other tables by adding the joins here - // If you add more joins you probably want to make sure that the - // above foreach only handles fields from the users table. - $joins = array(); - - // Firstname, lastname, and email come from the People table - if (isset($fields['firstname'])) { - $joins['p'] = array('table'=>'people','condition'=>'u.id=p.user_id'); - $this->select->where('p.firstname=?',$fields['firstname']); - } - if (isset($fields['lastname'])) { - $joins['p'] = array('table'=>'people','condition'=>'u.id=p.user_id'); - $this->select->where('p.lastname=?',$fields['lastname']); - } - if (isset($fields['email'])) { - $joins['p'] = array('table'=>'people','condition'=>'u.id=p.user_id'); - $this->select->where('p.email=?',$fields['email']); - } - - // To get the Role, we have to join the user_roles and roles tables - if (isset($fields['role'])) { - $joins['ur'] = array('table'=>'user_roles','condition'=>'u.id=ur.user_id'); - $joins['r'] = array('table'=>'roles','condition'=>'ur.role_id=r.id'); - $this->select->where('r.name=?',$fields['role']); - } - - // Add all the joins we've created to the select - foreach ($joins as $key=>$join) { - $this->select->joinLeft(array($key=>$join['table']),$join['condition']); - } - - - - $this->select->order($order); - if ($limit) { - $this->select->limit($limit); - } - if ($groupBy) { - $this->select->group($groupBy); - } - $this->populateList(); - } - - /** - * Hydrates all the objects from a database result set - * - * This is a callback function, called from ZendDbResultIterator. It is - * called once per row of the result. - * - * @param int $key The index of the result row to load - * @return User - */ - protected function loadResult($key) - { - return new User($this->result[$key]); - } -}