Alpha Framework alpha--model
[ class tree: alpha--model ] [ index: alpha--model ] [ all elements ]

Source for file PersonObject.inc

Documentation is available at PersonObject.inc

  1. <?php
  2.  
  3. require_once $config->get('sysRoot').'alpha/model/AlphaDAO.inc';
  4. require_once $config->get('sysRoot').'alpha/model/RightsObject.inc';
  5. require_once $config->get('sysRoot').'alpha/model/ArticleCommentObject.inc';
  6.  
  7. /**
  8.  *
  9.  * The main person/user class for the site
  10.  *
  11.  * @package alpha::model
  12.  * @since 1.0
  13.  * @author John Collins <dev@alphaframework.org>
  14.  * @version $Id: PersonObject.inc 1453 2011-12-04 15:12:54Z johnc $
  15.  * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
  16.  * @copyright Copyright (c) 2011, John Collins (founder of Alpha Framework).
  17.  *  All rights reserved.
  18.  * 
  19.  *  <pre>
  20.  *  Redistribution and use in source and binary forms, with or
  21.  *  without modification, are permitted provided that the
  22.  *  following conditions are met:
  23.  * 
  24.  *  * Redistributions of source code must retain the above
  25.  *    copyright notice, this list of conditions and the
  26.  *    following disclaimer.
  27.  *  * Redistributions in binary form must reproduce the above
  28.  *    copyright notice, this list of conditions and the
  29.  *    following disclaimer in the documentation and/or other
  30.  *    materials provided with the distribution.
  31.  *  * Neither the name of the Alpha Framework nor the names
  32.  *    of its contributors may be used to endorse or promote
  33.  *    products derived from this software without specific
  34.  *    prior written permission.
  35.  *   
  36.  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  37.  *  CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  38.  *  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  39.  *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  40.  *  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  41.  *  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  42.  *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  43.  *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  44.  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  45.  *  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  46.  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  47.  *  OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  48.  *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  49.  *  </pre>
  50.  *  
  51.  */
  52. class PersonObject extends AlphaDAO {
  53.     /**
  54.      * The forum display name of the person
  55.      *
  56.      * @var String 
  57.      * @since 1.0
  58.      */
  59.     protected $displayName;
  60.  
  61.     /**
  62.      * The email address for the person
  63.      *
  64.      * @var String 
  65.      * @since 1.0
  66.      */
  67.     protected $email;
  68.  
  69.     /**
  70.      * The password for the person
  71.      *
  72.      * @var String 
  73.      * @since 1.0
  74.      */
  75.     protected $password;
  76.  
  77.     /**
  78.      * A Relation containing all of the rights groups that this person belongs to
  79.      *
  80.      * @var Relation 
  81.      * @since 1.0
  82.      */
  83.     protected $rights;
  84.  
  85.     /**
  86.      * An array of data display labels for the class properties
  87.      *
  88.      * @var array 
  89.      * @since 1.0
  90.      */
  91.     protected $dataLabels = array("OID"=>"Member ID#",
  92.                                     "displayName"=>"Display Name",
  93.                                     "email"=>"E-mail Address",
  94.                                     "password"=>"Password",
  95.                                     "state"=>"Account state",
  96.                                     "URL"=>"Your site address",
  97.                                     "rights"=>"Rights Group Membership");
  98.  
  99.     /**
  100.      * The name of the database table for the class
  101.      *
  102.      * @var string 
  103.      * @since 1.0
  104.      */
  105.     const TABLE_NAME = 'Person';
  106.  
  107.     /**
  108.      * The state of the person (account status)
  109.      *
  110.      * @var Enum 
  111.      * @since 1.0
  112.      */
  113.     protected $state;
  114.  
  115.     /**
  116.      * The website URL of the person
  117.      *
  118.      * @var String 
  119.      * @since 1.0
  120.      */
  121.     protected $URL;
  122.     
  123.     /**
  124.      * Trace logger
  125.      * 
  126.      * @var Logger 
  127.      * @since 1.0
  128.      */
  129.     private static $logger null;
  130.  
  131.     /**
  132.      * Constructor for the class that populates all of the complex types with default values
  133.      * 
  134.      * @since 1.0
  135.      */
  136.     public function __construct({
  137.         self::$logger new Logger('PersonObject');
  138.         self::$logger->debug('>>__construct()');
  139.         
  140.         // ensure to call the parent constructor
  141.         parent::__construct();
  142.         $this->displayName = new String();
  143.         $this->displayName->setRule(AlphaValidator::REQUIRED_USERNAME);
  144.         $this->displayName->setSize(70);
  145.         $this->displayName->setHelper('Please provide a name for display on the website (only letters, numbers, and .-_ characters are allowed!).');
  146.         $this->email = new String();
  147.         $this->email->setRule(AlphaValidator::REQUIRED_EMAIL);
  148.         $this->email->setSize(70);
  149.         $this->email->setHelper('Please provide a valid e-mail address as your username.');
  150.         $this->password = new String();
  151.         $this->password->setSize(70);
  152.         $this->password->setHelper('Please provide a password for logging in.');
  153.         $this->password->isPassword(true);        
  154.         $this->state = new Enum(array(
  155.                                     'Active',
  156.                                     'Disabled'));
  157.         $this->state->setValue('Active');
  158.         $this->URL = new String();
  159.         $this->URL->setRule(AlphaValidator::OPTIONAL_HTTP_URL);
  160.         $this->URL->setHelper('URLs must be in the format http://some_domain/ or left blank!');
  161.         // add unique keys to displayName and email (which is effectively the username in Alpha)
  162.         $this->markUnique('displayName');
  163.         $this->markUnique('email');
  164.  
  165.         $this->rights = new Relation();
  166.         $this->markTransient('rights');
  167.         $this->setupRels();
  168.         
  169.         self::$logger->debug('<<__construct');
  170.     }
  171.  
  172.     /**
  173.      * Set up the transient attributes for the rights group after it has loaded
  174.      * 
  175.      * @since 1.0
  176.      */
  177.     protected function after_load_callback({
  178.         $this->setupRels();        
  179.     }
  180.     
  181.     /**
  182.      * Set up the transient attributes for the site after it has loaded
  183.      * 
  184.      * @since 1.0
  185.      */
  186.     protected function after_loadByAttribute_callback({
  187.         $this->setupRels();
  188.     }
  189.  
  190.     /**
  191.      * Looks up the OID for the Standard rights group, then relates the new person
  192.      * to that group if they are not in it already.  If that group does not exist it
  193.      * will be recreated!
  194.      * 
  195.      * @since 1.0
  196.      */
  197.     protected function after_save_callback({
  198.         if($this->getVersionNumber()->getValue(== 1{
  199.             $standardGroup new RightsObject();
  200.             
  201.             $this->setupRels();
  202.  
  203.             if(!$this->inGroup('Standard')) {
  204.                 try {
  205.                     $standardGroup->loadByAttribute('name''Standard');
  206.                 }catch (BONotFoundException $e{
  207.                     $standardGroup->set('name''Standard');
  208.                     $standardGroup->save();
  209.                 }
  210.                         
  211.                 $lookup $this->rights->getLookup();
  212.                 $lookup->setValue(array($this->getID()$standardGroup->getID()));
  213.                 $lookup->save();
  214.             }
  215.         }
  216.     }
  217.     
  218.     /**
  219.      * Encrypts any fields called 'password' posted for the PersonObject
  220.      * 
  221.      * @since 1.0
  222.      */
  223.     protected function before_populateFromPost_callback({
  224.         if(isset($_POST['password']&& preg_match(AlphaValidator::REQUIRED_STRING$_POST['password']))
  225.             $_POST['password'crypt($_POST['password']);
  226.     }
  227.     
  228.     /**
  229.      * Sets up the Relation definitions on this BO
  230.      * 
  231.      * @since 1.0
  232.      */
  233.     protected function setupRels({        
  234.         // set up MANY-TO-MANY relation person2rights
  235.         if(isset($this->rights)) {
  236.             $this->rights->setRelatedClass('PersonObject''left');
  237.             $this->rights->setRelatedClassDisplayField('email''left');
  238.             $this->rights->setRelatedClass('RightsObject''right');
  239.             $this->rights->setRelatedClassDisplayField('name''right');
  240.             $this->rights->setRelationType('MANY-TO-MANY');
  241.             $this->rights->setValue($this->getID());
  242.         }
  243.     }
  244.  
  245.     /**
  246.      * Setter for displayName
  247.      *
  248.      * @param string $displayName 
  249.      * @since 1.0
  250.      */
  251.     public function setDisplayName($displayName{
  252.         $this->displayName->setValue($displayName);
  253.     }
  254.  
  255.     /**
  256.      * Getter for displayName
  257.      *
  258.      * @return String 
  259.      * @since 1.0
  260.      */
  261.     public function getDisplayName({
  262.         return $this->displayName;
  263.     }
  264.  
  265.     /**
  266.      * Checks to see if the person is in the rights group specified
  267.      *
  268.      * @param string $groupName 
  269.      * @return bool 
  270.      * @since 1.0
  271.      */
  272.     public function inGroup($groupName{
  273.         if(self::$logger == null)
  274.             self::$logger new Logger('PersonObject');
  275.         self::$logger->debug('>>inGroup(groupName=['.$groupName.'])');
  276.             
  277.         $group new RightsObject();
  278.             
  279.         try {
  280.             $group->loadByAttribute('name'$groupName);
  281.         }catch (BONotFoundException $e{
  282.             self::$logger->error('Unable to load the group named ['.$groupName.']');
  283.             self::$logger->debug('<<inGroup [false]');
  284.             return false;
  285.         }
  286.             
  287.         $rel $group->getMembers();
  288.         
  289.         try {
  290.             // load all person2rights RelationLookup objects for this person
  291.             $lookUps $rel->getLookup()->loadAllByAttribute('leftID'$this->getID());
  292.             foreach($lookUps as $lookUp{
  293.                 // the rightID (i.e. RightsObject OID) will be on the right side of the value array
  294.                 $ids $lookUp->getValue();
  295.                 // if we have found a match, return true right away
  296.                 if($ids[1== $group->getID()) {
  297.                     self::$logger->debug('<<inGroup [true]');
  298.                     return true;
  299.                 }
  300.             }                
  301.         }catch (BONotFoundException $e{
  302.             self::$logger->debug('<<inGroup [false]');
  303.             return false;
  304.         }
  305.         
  306.         self::$logger->debug('<<inGroup [false]');
  307.         return false;
  308.     }
  309.  
  310.     /**
  311.      * A generic method for mailing a person
  312.      *
  313.      * @param string $message 
  314.      * @param string $subject 
  315.      * @since 1.0
  316.      * @throws MailNotSentException
  317.      */
  318.     public function sendMail($message$subject{
  319.         global $config;
  320.  
  321.         $body '<html><head></head><body><p>Dear '.$this->getDisplayName().',</p>';
  322.  
  323.         $body .= $message;
  324.  
  325.         $body .= '</body></html>';
  326.  
  327.         $headers 'MIME-Version: 1.0'."\n";
  328.         $headers .= 'Content-type: text/html; charset=iso-8859-1'."\n";
  329.         $headers .= "From: ".$config->get('sysReplyTo')."\n";
  330.         
  331.         try {
  332.             mail($this->get('email')$subject$body$headers);
  333.         }catch (PHPException $e{
  334.             throw new MailNotSentException('Error sending a mail to ['.$this->get('email').']');
  335.         }
  336.     }
  337.  
  338.     /**
  339.      * Generates a random password for the user
  340.      *
  341.      * @return string 
  342.      * @since 1.0
  343.      */
  344.     public function generatePassword({
  345.         $alphabet array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z');
  346.         // the password will be 7 random characters and 2 numbers
  347.         $newPassword '';
  348.         for ($i 0$i 7$i++{
  349.             $newPassword.= $alphabet[rand(0,25)];
  350.         }
  351.         $newPassword.= rand(0,100);
  352.         $newPassword.= rand(0,100);
  353.  
  354.         return $newPassword;
  355.     }
  356.  
  357.     /**
  358.      * Method for getting a count of the amount of article comments posted by the user
  359.      *
  360.      * @return integer 
  361.      * @since 1.0
  362.      * @throws AlphaException
  363.      */
  364.     public function getCommentCount({
  365.         $temp new ArticleCommentObject();
  366.  
  367.         $sqlQuery "SELECT COUNT(OID) AS post_count FROM ".$temp->getTableName()." WHERE created_by='".$this->OID."';";
  368.  
  369.         $result $this->query($sqlQuery);
  370.  
  371.         if(!isset($result[0])) {
  372.             throw new AlphaException('Failed to get the count of the comments posted for the person ['.$this->getDisplayName().'], query is ['.$sqlQuery.']');
  373.             return 0;
  374.         }
  375.  
  376.         $row $result[0];
  377.  
  378.         if(isset($row['post_count']))
  379.             return $row['post_count'];
  380.         else
  381.             return 0;
  382.     }
  383. }
  384.  
  385. ?>

Documentation generated on Tue, 13 Dec 2011 20:27:16 +0000 by phpDocumentor 1.4.3