API Docs for: WebTemplate API v2.0
Show:

File: /data/www/webtemplate/wtv2/codedoc/wtContact.php

<?php
/**
 *  WebTemplate Core
 *
 *  @version 2.0
 *  @module WebTemplate Core
 */


define('WTSYSTEMSALT', 'GFODJEMBJDUET');

/**
 *  The class representing Contact Nodes
 *
 *  @class WTContact
 *  @extends WTNode
 */

class WTContact extends WTNode 
{
/*  public static function create($parentGuid, $typeName, $attributes) 
  {
    $contact = parent::create($parentGuid, $typeName, $attributes);
    if($GLOBALS["WT"]->getWTValue("wt.settings.Membership.Welcome") == "Yes") {
      $type = explode(",", $contact->getAttribute("Type"));

      // if this person is a member, might need to send them a welcome email
      if(in_array("Member", $type)) {
        $contact->sendNotification("Welcome");     
      }
    }
    return $contact;
  }*/



  /**
   *  Create an administrator contact from the attributes
   *
   *  @method createAdministrator
   *  @static
   *  @param {Array} attributes An associative array of the attributes of the administrator. Must include Username/Password
   *  @return {WTContact} The object for the administrator Contact node
   */

  public static function createAdministrator($attributes) {

    $adminType = $GLOBALS["WT"]->getNode("/Config/Options/Contact Types/Admin");
    $adminTypeID = $adminType->m_guid;

    $memberType = $GLOBALS["WT"]->getNode("/Config/Options/Contact Types/Member");
    $memberTypeID = $memberType->m_guid;

    $attributes["Type"] = "Admin, Member";
    $attributes["Type IDs"] = "$adminTypeID, $memberTypeID";
    $contactsNode = $GLOBALS["WT"]->getNode("/Contacts");
    $contact = $contactsNode->createChild("Contact", $attributes);
    return $contact;
  }


  public static function create($parentGuid, $typeName, $attributes) 
  {
    $contact = parent::create($parentGuid, $typeName, $attributes);
    $uid = uniqid("", true);
    $sql = "UPDATE 
              Contact
            SET
              `Unique ID` = '" . mysql_escape_string($uid) . "'
            WHERE
              __guid = {$contact->m_guid}";
    mysql_query($sql);
    print mysql_error();

    $wantsContact = $contact->getAttribute("Wants Contact");
    if($wantsContact == "Yes") {
      WTCommunication::sendWelcomeEmail($contact->m_guid, $uid);
    }


    return $contact;
  }


  /**
   *  Send the appropriate welcome email to the contact
   *
   *  @method sendWelcomeEmail
   */
  function sendWelcomeEmail() {
    if($this->isMemberOf("/Groups/__Administrators")) {
      $subject = WTConfig::get("Contact Manager/Join Email/admin/Subject");
      $message = WTConfig::get("Contact Manager/Join Email/admin/Message");
    } else if($this->isMemberOf("/Groups/__Members")) {
      $subject = WTConfig::get("Contact Manager/Join Email/member/Subject");
      $message = WTConfig::get("Contact Manager/Join Email/member/Message");
    } else {
      return;
    }

    $attributes = $this->getAttributes();
    $attributes["Password"] = WTSession::get("WTSAVEPASSWORD");

    foreach($attributes as $key => $value) {
      $subject = str_replace("[$key]", $value, $subject);
      $message = str_replace("[$key]", $value, $message);
    }

    $emailAddress = $this->getAttribute("Email Address");

    $fromName = WTConfig::get("Membership/Notifications From Name");
    $fromAddress = WTConfig::get("Membership/Notifications From Address");
    $mail = new WTMail();
    $mail->setTo($emailAddress);
    $mail->setFrom($fromAddress, $fromName);
    $mail->setContent($subject, $message, true);
    $mail->send();
  }


  function getFormAttributeHTML($attributeInfo, $values)
  {
    $contact = NULL;
    $guid = $GLOBALS["WT"]->getRequestValue("guid", 0);
    if($guid != 0) {
      $contact = $GLOBALS["WT"]->getNode($guid);
    }
    $attributeName = $attributeInfo["Attribute Name"];
    if($attributeName == "Password") {
      if($values["Password"] != "") {
        $values["Password"] = "********";
      }
    }

    if($attributeName == "Groups") {
      $value = "";
      $label = "Groups";
      if($contact != NULL) {
        $value = $contact->getGroups();
      }

//      $control = WTControl::getControlHTML("attr" . $attributeInfo["__guid"], Array("Attribute Settings" => "options=/Groups&optionType=Group&optionCriteria=Type='Contact Group'"), $value);
      $control = InOutControl::getControlHTML("attr" . $attributeInfo["__guid"], Array("Attribute Settings" => "options=/Groups&optionType=Group&optionCriteria=Type='Contact Group'"), $value);
      return WTNode::getFormRowHTML($label, $control);
    }

    if($attributeName == "Contact Groups" || $attributeName == "Member Groups" || $attributeName == "Admin Groups") {
      $label = $attributeName;
      $singularlabel = substr($label, 0, strlen($singularlabel) -1);
      $value = "";
      if($contact != NULL) {
        $value = $contact->getGroups($singularlabel);
      }

      $label = $singularlabel;
      $control = WTControl::getControlHTML("attr" . $attributeInfo["__guid"], Array("Attribute Type" => "Single Choice Options", "Attribute Settings" => "options=/Groups&optionType=Group&optionCriteria=Type='$singularlabel'"), $value);
      $control = InOutControl::getControlHTML("attr" . $attributeInfo["__guid"], Array("Attribute Settings" => "options=/Groups&optionType=Group&optionCriteria=Type='$singularlabel'"), $value);
      return WTNode::getFormRowHTML('<label for="attr' . $attributeInfo["__guid"] . '">' . $label . '</label>:', $control, "attr" . $attributeInfo["__guid"] . "row");
    }


    return parent::getFormAttributeHTML($attributeInfo, $values);
  }

  function setAttributes($attributes) 
  {
    $username = $this->getAttribute("Username");
    if($username == "__public" || $username == "__admin") {
      // cant set admin or public's details
      return false;
    }
    if(array_key_exists("Username", $attributes)) {
      $attributes["Username"] = trim($attributes["Username"]);
    }

    if(array_key_exists("Password", $attributes)) {
      $attributes["Password"] = trim($attributes["Password"]);
      if($attributes["Password"] == "********" || trim($attributes["Password"]) == '') {
        $attributes["Password"] = $this->getAttribute("Password");
      } else {
        $username = "";
        if(array_key_exists("Username", $attributes)) {
          $username = $attributes["Username"];      
        } else {
          $username = $this->getAttribute("Username");      
        }
/*        if($username == "") {
          print "need a username for a password";       
          return;
        }
*/
        $attributes["Password"] = md5($username . WTSYSTEMSALT . $attributes["Password"]);    
      }
    }

/*
    if(array_key_exists("Groups IDs", $attributes)) {

      $groupIDArray = explode(",", $attributes["Groups IDs"]);
      $attributeNode = $GLOBALS["WT"]->getNode("/Config/Node Types/Contact/Groups");
      $options = $attributeNode->getOptions();

      foreach($options as $id => $option) {
        if(in_array($id, $groupIDArray)) {
          $GLOBALS["WT"]->linkNodes($id, $this->m_guid);
        } else {
          $GLOBALS["WT"]->unlinkNodes($id, $this->m_guid);
        } 
      }
    }
*/

    $groupTypes = Array("Contact Groups", "Member Groups", "Admin Groups");
    foreach($groupTypes as $groupType) {
      if(array_key_exists($groupType, $attributes)) {
        $groupTypeSingular = substr($groupType, 0, strlen($groupType) - 1);
        $groupsin = array_map("trim", explode(",", $attributes[$groupType]));
        $q = Array();
        $q["Node Type"] = "Group";
        $q["Criteria"] = "Type = '$groupTypeSingular'";
        $groupguids = $GLOBALS["WT"]->query($q, "guidListCallback");
        $groups = array_map("trim", explode(",", $groupguids));      
        //print "contact groups = " . $groupguids . "<BR>";
        foreach($groups as $groupGuid) {
          if($groupGuid != "") {
            if(in_array($groupGuid, $groupsin)) {
              $GLOBALS["WT"]->linkNodes($groupGuid, $this->m_guid);
            } else {
              $GLOBALS["WT"]->unlinkNodes($groupGuid, $this->m_guid);
            }
          }
        }
      }
    }

    parent::setAttributes($attributes);
    $attributes = $this->getAttributes();
    $type = explode(",", $this->getAttribute("Type"));
    $type = array_map("trim", $type);
    
    $members = $GLOBALS["WT"]->getNode("/Groups/__Members");


    if(in_array("Member", $type)) {
      $GLOBALS["WT"]->linkNodes($members->m_guid, $this->m_guid);
    } else {
      $GLOBALS["WT"]->unlinkNodes($members->m_guid, $this->m_guid);
    }

    $siteAdministrators = $GLOBALS["WT"]->getNode("/Groups/__Administrators");
    if(in_array("Admin", $type)) {
      $GLOBALS["WT"]->linkNodes($siteAdministrators->m_guid, $this->m_guid);
    } else {
      $GLOBALS["WT"]->unlinkNodes($siteAdministrators->m_guid, $this->m_guid);
    }


    return true;
  }


  /**
   *  If the contact is a member, return the highest discount % the member is entitled to by their groups
   *
   *  @method getDiscount
   *  @return {float} the discount percent
   */

  function getDiscount() {
    $discount = 0;
    $memberGroups = explode(',', $this->getGroups("Member Group"));
    foreach($memberGroups as $memberGroup) {
      if(trim($memberGroup) <> "") {
        $group = $GLOBALS["WT"]->getNode($memberGroup);
        if($group) {
          $groupDiscount = $group->getAttribute("Discount (%)");
          if($groupDiscount > $discount) {
            $discount = $groupDiscount;
          }
        }
      }
    }    
    return $discount;
  }


  /**
   *  Test if a contact is a member of a group
   *
   *  @method isMemberOf
   *  @param group The path, object, or guid of the group to test membership of
   *  @return {bool} True if the contact is a member of the group
   */

  function isMemberOf($group) 
  {

    return $GLOBALS["WT"]->nodesAreLinked($group, $this->m_guid);

  }


  /**
   *  Get the groups a contact is a member of as a comma separated list of guids
   *
   *  @method getGroups
   *  @param {String} [groupType] The type of group to return (Contact Group, Member Group, Admin Group). Leave blank for all
   *  @return {String} A comma separated list of the guids of the groups
   */

  function getGroups($groupType = "")
  {
    $typeSql = "";
    $groups = "";

    if($groupType != "") {
      $typeSql = 'AND `Group`.Type = \'' . mysql_escape_string($groupType) . '\'';
    }
    $sql = "SELECT
             `Group`.__guid
            FROM
             `Group`, wtNode
            WHERE
              `Group`.__active <> 0
              AND `Group`.__guid = wtNode.__parentGuid
              AND wtNode.__nodeIsHolderForGuid = $this->m_guid
              $typeSql";
    $query = mysql_query($sql);
    print mysql_error();
    while($row = mysql_fetch_row($query)) {
      if($groups != "") {
        $groups .= ",";
      }
      $groups .= $row[0];
    }
    return $groups;
  }

  function linkedToParent($parentNode) 
  {
    if($parentNode->m_typeName == "Group") {
      if($this->getAttribute("Wants Contact") == "Yes" && $parentNode->getAttribute("Type") == "Contact Group") {
        $sql = "INSERT INTO tblMailingListHistory (
                  mailingListHistoryDate,
                  groupGuid,
                  contactGuid,
                  mailingListHistoryDirection
                ) VALUES (
                  NOW(),
                  {$parentNode->m_guid},
                  {$this->m_guid},
                  1
                )";
        mysql_query($sql);
        print mysql_error();
      }
    }       
  }

  function unlinkedFromParent($parentNode) 
  {
    if($parentNode->m_typeName == "Group") {
      if($this->getAttribute("Wants Contact") == "Yes" && $parentNode->getAttribute("Type") == "Contact Group") {
        $sql = "INSERT INTO tblMailingListHistory (
                  mailingListHistoryDate,
                  groupGuid,
                  contactGuid,
                  mailingListHistoryDirection
                ) VALUES (
                  NOW(),
                  {$parentNode->m_guid},
                  {$this->m_guid},
                  -1
                )";
        mysql_query($sql);  
      }
    }
    
  }
  
};



?>