+<?php
+/**
+ * Member.php
+ *
+ * PHP Version 5
+ *
+ * @category Toolkit_Members
+ * @package Members
+ * @author Jamie Kahgee <steve@gaslightmedia.com>
+ * @license Gaslight Media
+ * @link <>
+ */
+/**
+ * Toolkit_Members_MemberImport_Member
+ *
+ * Member class for importing member
+ *
+ * @category Toolkit_Members
+ * @package Members
+ * @author Jamie Kahgee <steve@gaslightmedia.com>
+ * @license Gaslight Media
+ * @link <>
+ */
+class Toolkit_Members_MemberImport_Member
+{
+
+ /**
+ * Description for $active
+ * @var boolean
+ * @access protected
+ */
+ protected $active = true;
+ protected $non_member = true;
+ protected $new_member = false;
+ protected $create_date;
+
+ /**
+ * Description for $join_date
+ * @var date
+ * @access protected
+ */
+ protected $join_date;
+
+ /**
+ * Description for $member_id
+ * @var integer
+ * @access protected
+ */
+ protected $member_id;
+
+ /**
+ * Description for $member_name
+ * @var string
+ * @access protected
+ */
+ protected $member_name;
+
+ /**
+ * Description for $street
+ * @var string
+ * @access protected
+ */
+ protected $street;
+
+ /**
+ * Description for $country
+ * @var string
+ * @access protected
+ */
+ protected $country;
+
+ /**
+ * Description for $phone_area
+ * @var string
+ * @access protected
+ */
+ protected $phone_area; // not real field
+
+ /**
+ * Description for $phone
+ * @var string
+ * @access protected
+ */
+ protected $phone;
+
+ /**
+ * Description for $fax_area
+ * @var string
+ * @access protected
+ */
+ protected $fax_area; // not real field
+
+ /**
+ * Description for $fax
+ * @var string
+ * @access protected
+ */
+ protected $fax;
+
+ /**
+ * Description for $process_email
+ * @var string
+ * @access protected
+ */
+ protected $process_email;
+
+ /**
+ * Description for $url
+ * @var string
+ * @access protected
+ */
+ protected $url;
+
+ /**
+ * Description for $city
+ * @var string
+ * @access protected
+ */
+ protected $city;
+
+ /**
+ * Description for $city_id
+ * @var integer
+ * @access protected
+ */
+ protected $city_id;
+
+ /**
+ * Description for $state
+ * @var string
+ * @access protected
+ */
+ protected $state;
+
+ /**
+ * Description for $state_id
+ * @var integer
+ * @access protected
+ */
+ protected $state_id;
+
+ /**
+ * Description for $description
+ * @var integer
+ * @access protected
+ */
+ protected $description;
+
+ /**
+ * Description for $zip
+ * @var string
+ * @access protected
+ */
+ protected $zip;
+
+ /**
+ * Description for $toll_free_area
+ * @var string
+ * @access protected
+ */
+ protected $toll_free_area; // not real field
+
+ /**
+ * Description for $toll_free
+ * @var string
+ * @access protected
+ */
+ protected $toll_free;
+
+ /**
+ * Description for $member_contact_email
+ * @var string
+ * @access protected
+ */
+ protected $member_contact_email;
+
+ /**
+ * Description for $mailing_address
+ * @var string
+ * @access protected
+ */
+ protected $mailing_address;
+
+ /**
+ * Description for $primary_contact
+ * @var string
+ * @access protected
+ */
+ protected $primary_contact;
+
+ /**
+ * Description for $primary_contact_fname
+ * @var string
+ * @access protected
+ */
+ protected $primary_contact_fname;
+
+ /**
+ * Description for $primary_contact_lname
+ * @var string
+ * @access protected
+ */
+ protected $primary_contact_lname;
+
+ /**
+ * Description for $mailing_state
+ * @var string
+ * @access protected
+ */
+ protected $mailing_state;
+
+ /**
+ * Description for $mailing_state_id
+ * @var integer
+ * @access protected
+ */
+ protected $mailing_state_id;
+
+ /**
+ * Description for $mailing_city
+ * @var string
+ * @access protected
+ */
+ protected $mailing_city;
+
+ /**
+ * Description for $mailing_city_id
+ * @var integer
+ * @access protected
+ */
+ protected $mailing_city_id;
+
+ /**
+ * Description for $mailing_zip
+ * @var string
+ * @access protected
+ */
+ protected $mailing_zip;
+
+ /**
+ * Description for $lat
+ * @var double
+ * @access protected
+ */
+ protected $lat;
+
+ /**
+ * Description for $lon
+ * @var double
+ * @access protected
+ */
+ protected $lon;
+
+ /**
+ * Description for $member_categories
+ * @var array
+ * @access protected
+ */
+ protected $member_categories = array(); // not real field
+
+ /**
+ * created setters and getters for the class properties
+ * checks for existance of the property before running getter/setter
+ *
+ * @param String $name Function name
+ * @param Mixed $args if set the arg to set
+ *
+ * @return mixed
+ * @throws Exception
+ * @access public
+ */
+ public function __call($name, $args)
+ {
+ if (preg_match('/^(get|set)(\w+)/', strtolower($name), $match)
+ && $attribute = $this->validateAttribute($match[2])
+ ) {
+ if ('get' == $match[1]) {
+ return $this->$attribute;
+ } else {
+ $this->$attribute = $args[0];
+ }
+ } else {
+ throw new Exception(
+ 'Call to undefined method Member::' . $name
+ );
+ }
+ }
+ /**
+ * _insert
+ *
+ * create the record in database
+ *
+ * @param PDO $dbh Databese connection
+ *
+ * @return void
+ * @access public
+ */
+ private function _insert(PDO $dbh)
+ {
+ $classData = get_object_vars($this);
+ unset(
+ $classData['member_id'],
+ $classData['member_categories'],
+ $classData['phone_area'],
+ $classData['fax_area'],
+ $classData['toll_free_area'],
+ $classData['mailing_state']
+ );
+ $sql = Toolkit_Common::createSQLInsert(
+ 'member',
+ array_keys($classData)
+ );
+ $sql .= " RETURNING member_id";
+ $stmt = Toolkit_Common::prepareQuery(
+ $dbh,
+ 'member',
+ $sql,
+ $classData
+ );
+ $stmt->execute();
+ $this->setMemberId($stmt->fetchColumn());
+ }
+ /**
+ * getCLassVars
+ *
+ * get all class properties
+ *
+ * @return Array Keys of the class properties
+ * @access public
+ */
+ public function getClassVars()
+ {
+ return array_keys(get_class_vars(get_class($this)));
+ }
+ /**
+ * check the date given to see if its a valid date first before setting it
+ *
+ * @param String $date date
+ *
+ * @return void
+ * @access public
+ */
+ public function setJoinDate($date)
+ {
+ if (strtotime($date)) {
+ $this->join_date = $date;
+ }
+ }
+ /**
+ * check the number given to make sure it is numeric
+ *
+ * @param String $lat latitude
+ *
+ * @return void
+ * @access public
+ */
+ public function setLat($lat)
+ {
+ if (is_numeric($lat)) {
+ $this->lat = $lat;
+ }
+ }
+ /**
+ * check the number given to make sure it is numeric
+ *
+ * @param String $lon longitude
+ *
+ * @return void
+ * @access public
+ */
+ public function setLon($lon)
+ {
+ if (is_numeric($lon)) {
+ $this->lon = $lon;
+ }
+ }
+ /**
+ * check the number given to make sure it is numeric
+ *
+ * @param Int $id member id must be numeric
+ *
+ * @throws Exception if not numeric
+ * @return void
+ * @access public
+ */
+ public function setMemberId($id)
+ {
+ if (!is_numeric($id)) {
+ throw new Exception('Id must be numeric');
+ }
+ if (!$this->member_id) {
+ $this->member_id = $id;
+ }
+ }
+ /**
+ * create the city if not found then set member property
+ *
+ * @param PDO $dbh Database connection
+ * @param String $cityName City Name
+ *
+ * @return Int
+ * @access public
+ */
+ public function setCity(PDO $dbh, $cityName)
+ {
+ if (!$cityName) {
+ return false;
+ }
+ // update the city name so each first letter is upper case
+ $cityName = ucwords(strtolower($cityName));
+ $city = new Toolkit_Members_MemberImport_City();
+ $newCity = $city->fetchByName($dbh, $cityName);
+ if ($newCity) {
+ $cityId = $newCity->getCity_id();
+ $this->setCity_id($cityId);
+ } else {
+ // need to create new city
+ $city->setCity_name($cityName);
+ $city->setState_id($this->getState_id());
+ $city->save($dbh);
+ $this->setCity_id($city->getCity_id());
+ }
+ }
+ /**
+ * create the city if not found then set member property
+ *
+ * @param PDO $dbh Database connection
+ * @param String $cityName City Name
+ *
+ * @return boolean
+ * @access public
+ */
+ public function setMailingCity(PDO $dbh, $cityName)
+ {
+ if (!$cityName) {
+ return false;
+ }
+ $city = new Toolkit_Members_MemberImport_City();
+ $newCity = $city->fetchByName($dbh, $cityName);
+ if ($newCity) {
+ $cityId = $newCity->getCity_id();
+ $this->setMailing_city_id($cityId);
+ } else {
+ // need to create new city
+ $city->setCity_name($cityName);
+ $city->setState_id($this->getMailing_state_id());
+ $city->save($dbh);
+ $this->setMailing_city_id($city->getCity_id);
+ }
+ }
+ /**
+ * create the state if not found then set member property
+ *
+ * @param PDO $dbh Database connection
+ * @param String $stateAbb State Abbreviation
+ *
+ * @return boolean
+ * @access public
+ */
+ public function setMailingState(PDO $dbh, $stateAbb)
+ {
+ if (!$stateAbb) {
+ return false;
+ }
+ $state = new Toolkit_Members_MemberImport_State();
+ $newState = $state->fetchByAbb($dbh, $stateAbb);
+ if (is_object($newState)) {
+ if ($stateId = $newState->getState_id()) {
+ $this->setMailing_state_id($stateId);
+ } else {
+ // ?
+ }
+ } else {
+ var_dump($stateId);
+ var_dump($stateAbb);
+ }
+ }
+ /**
+ * also set the member_contact_email at same time
+ *
+ * @param String $email Email address
+ *
+ * @return void
+ * @access public
+ */
+ public function setProcessEmail($email)
+ {
+ if ($email != '') {
+ $this->process_email = $email;
+ $this->member_contact_email = $email;
+ }
+ }
+ /**
+ * phone field may come in two seperate fields from the file
+ * here it's appending the fields area and
+ *
+ * @param <type> $phone_area phone area code
+ *
+ * @return void
+ * @access public
+ */
+ public function setPhoneArea($phone_area)
+ {
+ $this->phone = $phone_area . ' ';
+ }
+ /**
+ * set phone
+ *
+ * @param <type> $phone phone number
+ *
+ * @return void
+ * @access public
+ */
+ public function setPhone($phone)
+ {
+ if ($phone) {
+ $phone = Toolkit_Common::filterPhone($phone);
+ }
+ $this->phone .= $phone;
+ $this->phone = trim($this->phone);
+ }
+ /**
+ * set fax
+ *
+ * @param String $fax_area fax area code part
+ *
+ * @return void
+ * @access public
+ */
+ public function setFaxArea($fax_area)
+ {
+ $this->fax = $fax_area. ' ';
+ }
+ /**
+ * set fax number
+ *
+ * @param String $fax fax number part
+ *
+ * @return void
+ * @access public
+ */
+ public function setFax($fax)
+ {
+ $this->fax .= $fax;
+ $this->fax = trim($this->fax);
+ }
+ /**
+ * set toll free area code
+ *
+ * @param String $toll_free_area toll free area code
+ *
+ * @return void
+ * @access public
+ */
+ public function setTollFreeArea($toll_free_area)
+ {
+ $this->toll_free = $toll_free_area . ' ';
+ }
+ /**
+ * set toll free number
+ *
+ * @param String $toll_free Toll free number
+ *
+ * @return void
+ * @access public
+ */
+ public function setTollFree($toll_free)
+ {
+ $this->toll_free .= $toll_free;
+ $this->toll_free = trim($this->toll_free);
+ }
+ /**
+ * create the state if not found then set member property
+ *
+ * @param PDO $dbh Database connection
+ * @param String $stateAbb State Abbreviation
+ *
+ * @return boolean
+ * @access public
+ */
+ public function setState(PDO $dbh, $stateAbb)
+ {
+ if (!$stateAbb) {
+ return false;
+ }
+ $state = new Toolkit_Members_MemberImport_State();
+ $newState = $state->fetchByAbb($dbh, strtoupper($stateAbb));
+ if ($newState && $stateId = $newState->getState_id()) {
+ $this->setState_id($stateId);
+ } else {
+ // ?
+ }
+ }
+ /**
+ * save
+ *
+ * check to see if the id is set if it is then update else call insert
+ * also set the member categories
+ *
+ * @param PDO $dbh Database connection
+ *
+ * @return void
+ * @access public
+ */
+ public function save(PDO $dbh)
+ {
+ if ($this->member_id) {
+ $this->_update($dbh);
+ } else {
+ $this->_insert($dbh);
+ }
+ // now we can add categories
+ if (!empty($this->member_categories)) {
+ $addMemberCatSql = "
+ INSERT INTO member_category
+ (member_id, category_id)
+ VALUES
+ (:member_id, :category_id)";
+ $addMemberCat = $dbh->prepare($addMemberCatSql);
+ foreach ($this->member_categories as $catName) {
+ $category = new Toolkit_Members_MemberImport_Category();
+ if ($catName
+ && $oldCategory = $category->fetchByName($dbh, $catName)
+ ) {
+ // can now add this category to this member
+ $addMemberCat->bindParam(
+ ":member_id",
+ $this->getMember_id(),
+ PDO::PARAM_INT
+ );
+ $addMemberCat->bindParam(
+ ":category_id",
+ $oldCategory->getCategory_id(),
+ PDO::PARAM_INT
+ );
+ $addMemberCat->execute();
+ } else if ($catName) {
+ // not found must add
+ $category->setName($catName);
+ $category->save($dbh);
+ $addMemberCat->bindParam(
+ ":member_id",
+ $this->getMember_id(),
+ PDO::PARAM_INT
+ );
+ $addMemberCat->bindParam(
+ ":category_id",
+ $category->getCategory_id(),
+ PDO::PARAM_INT
+ );
+ $addMemberCat->execute();
+ }
+ }
+ }
+ }
+ /**
+ * validateAttribute
+ *
+ * checks to see if the properties exists
+ *
+ * @param String $name property name
+ *
+ * @return String
+ * @access public
+ */
+ protected function validateAttribute($name)
+ {
+ if (property_exists(get_class($this), $name)) {
+ return strtolower($name);
+ }
+ }
+}