Contact AISC
/**
* Contains the {@link Employee} class, which represents an employee.
*/
namespace ASUW\Shared\Contact;
require_once dirname(__DIR__)."/core.php";
require_once dirname(__DIR__) ."/record.php";
use ASUW\Shared\Record;
use ASUW\Shared\Core;
/**
* Stores information about an employee of the organization.
*
* @package Contact
* @author Kristian Randall, kristian.l.randall@gmail.com
* @version 1.1
*
* @see Entity
*
* @since 1.0
*/
class Employee extends Record {
/**
* Stores the data of an employee (mirrors that in the database).
*
* @since 1.1
*/
protected $data;
/**
* Creates an employee or array of employees using data that matches a specified selector.
* Accepted selectors are "username", "entity_id", and "all". Checks if a single employee
* or an array of employees are returned from the database, and returns them. If no employees
* are selected, returns false.
*
* @param selector Specifies the filter for the data in the database (i.e. "username").
* @param data The data corresponding to the selector (i.e. a username) to check the database for.
*
* @return mixed Returns an array of employees, or a single employee object, or false.
*
* @since 1.1
*/
public static function instance( $selector, $data = "", $core = false ) {
if( !$core )
$core = new Core;
$sql = "SELECT em.*, en.id as entity_id, en.name as entity, en.office as office, en.phone as phone
FROM contact.employees em
JOIN contact.empltoent ee ON em.id = ee.employee
JOIN contact.entities en ON en.id = ee.entity ";
$multiple = true; //whether or not to return an array of employees
switch ( $selector ) {
case 'username':
$sql .= "WHERE em.username = ?";
$multiple = false;
break;
case 'entity_id':
$sql .= "WHERE en.id = ?";
break;
case 'all':
break;
default: //false if not one of the above options
return false;
}
$sql .= " ORDER BY em.display_rank, em.firstname ASC"; //order by the employees' first names
$stmt = $core->dbh->prepare( $sql );
if ( $stmt->execute( array( $data ) ) ) {
$results = $stmt->fetchAll();
if ( !empty( $results ) ) {
if ( $multiple ) {
$seen = array();
$employees = array();
foreach ( $results as $row ) {
if ( !array_key_exists( $row['id'], $seen ) ) {
$seen[ $row['id'] ] = 1;
$employee = new self( $row, $core );
array_push( $employees, $employee );
}
}
return $employees;
}
else {
return new self( $results[0], $core );
}
}
}
return false;
}
/**
* Gets the property out of the data array, or creates it based on certain parameters
*
* @since 1.1
* @param String The property to retrieve
*/
public function __get( $property ) {
if ( !empty( $this->data[ $property ] ) ) {
return trim( $this->data[ $property ] );
}
else {
switch ( (string) $property ) {
case 'email':
return $this->username . "@uw.edu";
break;
case 'mailto':
return "" . $this->email . "";
break;
case 'name':
return $this->firstname . " " . $this->lastname;
default:
return false;
}
}
}
/**
* Checks if employee is in database.
*
* @param username The NetID of the employee
* @return boolean Returns true if the user is in the database
*
* @since 1.0
*/
public static function is_valid_username( $username ) {
$core = new Core;
$sql = "SELECT count(*) as count FROM employees WHERE username = ?";
$stmt = $core->dbh->prepare( $sql );
if ( $stmt->execute( array( $username ) ) ) {
$row = $stmt->fetch();
return $row["count"] > 0;
}
return false;
}
/**
* Gets the options for the dropdown menu built for WordPress shortcodes.
*
* @return array The methods that can be used.
*
* @since 1.0
*/
public static function get_employee_options() {
return array(
"name",
"title",
"mailto",
"username",
"office",
"entity",
);
}
}
?>
/**
* This file includes the {@link Entity} class.
*/
namespace ASUW\Shared\Contact;
require_once dirname(__DIR__) ."/record.php";
require_once __DIR__."/employee.php";
use ASUW\Shared\Record;
use ASUW\Shared\Contact\Employee;
use ASUW\Shared\Core;
/**
* Stores information about an entity.
*
* @package Contact
* @author Kristian Randall, kristian.l.randall@gmail.com
* @version 1.0
*
* @see Employee
*
* @since 1.0
*/
class Entity extends Record {
public $employees;
/**
* Makes a new Entity object. Stores the data received from the
* database as a data array.
*
* @param row An array of a row from the database.
*
* @return Entity|false a new entity object or false if no entity
*
* @since 1.0
*/
protected function __construct( $row, $core ) {
$this->employees = Employee::instance( "entity_id", $row["id"] );
parent::__construct( $row, $core );
return $this;
}
/**
* Gets an array of all entities. This function is meant to replace get_all.
*
* @return array An array of all entities.
* @since 1.1
*/
public static function instance( $selector, $data = "", $core = false ) {
$core = new Core;
$sql = "SELECT * FROM contact.entities ";
$multiple = true;
switch ( (string) $selector ) {
case "abbr":
$multiple = false;
$sql .= "WHERE abbr = ? ";
break;
case "all":
break;
default:
return false;
}
$sql .= "ORDER BY name ASC";
$stmt = $core->dbh->prepare( $sql );
if( $stmt->execute( array( $data ) ) ) {
$results = $stmt->fetchAll();
if( !empty($results) ) {
if( $multiple ) {
$entities = array();
foreach( $results as $row ) {
$entity = new self( $row, $core );
array_push( $entities, $entity );
}
return $entities;
}
else {
return new self( $results[0], $core );
}
}
}
return false;
}
/**
* Gets any data from the entity, including some custom options (mailto, etc.)
*
* @param property The property of the entity to get.
*
* @return mixed The value of whatever is requested.
*
* @since 1.1
*/
public function __get( $property ) {
switch ( (string) $property ) {
case 'phone':
$one = substr( $this->data["phone"], 0, 3 );
$two = substr( $this->data["phone"], 3, 3 );
$three = substr( $this->data["phone"], 6, 4 );
return $one . "-" . $two . "-" . $three;
break;
case 'name':
case 'abbr':
case 'email':
return htmlspecialchars( $this->data[ $property ] );
break;
case 'facebook':
return "//facebook.com/" . $this->data[ $property ];
break;
case 'twitter':
return "//twitter.com/" . $this->data[ $property ];
break;
case 'mailto':
return "" . $this->email . "";
break;
}
if ( !empty( $this->data[ $property ] ) ) {
return $this->data[ $property ];
}
else {
return false;
}
}
/**
* Gets an options list of publically available data about an entity.
*
* @return String[] An array of fields available
*/
public static function get_entity_options() {
return array(
"name",
"office",
"phone",
"email",
"facebook",
"twitter",
);
}
}
?>