Merge pull request #5076 from colemanw/Attachment
[civicrm-core.git] / CRM / Contact / Form / Contact.php
index 1e5590128835cbe9c7698c839718d3d3f4167cac..eae321ae3906b09f1fcb2cb8d2a463813e34d412 100644 (file)
@@ -23,7 +23,7 @@
  | GNU Affero General Public License or the licensing of CiviCRM,     |
  | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
  +--------------------------------------------------------------------+
-*/
+ */
 
 /**
  *
@@ -82,7 +82,6 @@ class CRM_Contact_Form_Contact extends CRM_Core_Form {
    * Name of de-dupe button
    *
    * @var string
-   * @access protected
    */
   protected $_dedupeButtonName;
 
@@ -90,7 +89,6 @@ class CRM_Contact_Form_Contact extends CRM_Core_Form {
    * Name of optional save duplicate button
    *
    * @var string
-   * @access protected
    */
   protected $_duplicateButtonName;
 
@@ -133,9 +131,8 @@ class CRM_Contact_Form_Contact extends CRM_Core_Form {
    * Build all the data structures needed to build the form
    *
    * @return void
-   * @access public
    */
-  function preProcess() {
+  public function preProcess() {
     $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 'add');
 
     $this->_dedupeButtonName = $this->getButtonName('refresh', 'dedupe');
@@ -152,8 +149,9 @@ class CRM_Contact_Form_Contact extends CRM_Core_Form {
         $this, TRUE, NULL, 'REQUEST'
       );
       if (!in_array($this->_contactType,
-          array('Individual', 'Household', 'Organization')
-        )) {
+        array('Individual', 'Household', 'Organization')
+      )
+      ) {
         CRM_Core_Error::statusBounce(ts('Could not get a contact id and/or contact type'));
       }
 
@@ -164,7 +162,8 @@ class CRM_Contact_Form_Contact extends CRM_Core_Form {
 
       if (
         $this->_contactSubType &&
-        !(CRM_Contact_BAO_ContactType::isExtendsContactType($this->_contactSubType, $this->_contactType, TRUE))) {
+        !(CRM_Contact_BAO_ContactType::isExtendsContactType($this->_contactSubType, $this->_contactType, TRUE))
+      ) {
         CRM_Core_Error::statusBounce(ts("Could not get a valid contact subtype for contact type '%1'", array(1 => $this->_contactType)));
       }
 
@@ -177,7 +176,7 @@ class CRM_Contact_Form_Contact extends CRM_Core_Form {
         FALSE, NULL, 'GET'
       );
       $typeLabel = CRM_Contact_BAO_ContactType::contactTypePairs(TRUE, $this->_contactSubType ?
-        $this->_contactSubType : $this->_contactType
+          $this->_contactSubType : $this->_contactType
       );
       $typeLabel = implode(' / ', $typeLabel);
 
@@ -192,8 +191,8 @@ class CRM_Contact_Form_Contact extends CRM_Core_Form {
       }
 
       if ($this->_contactId) {
-        $defaults          = array();
-        $params            = array('id' => $this->_contactId);
+        $defaults = array();
+        $params = array('id' => $this->_contactId);
         $returnProperities = array('id', 'contact_type', 'contact_sub_type', 'modified_date');
         CRM_Core_DAO::commonRetrieve('CRM_Contact_DAO_Contact', $params, $defaults, $returnProperities);
 
@@ -328,7 +327,7 @@ class CRM_Contact_Form_Contact extends CRM_Core_Form {
     if (!empty($_POST['hidden_custom'])) {
       $customGroupCount = CRM_Utils_Array::value('hidden_custom_group_count', $_POST);
 
-      if ($contactSubType = CRM_Utils_Array::value( 'contact_sub_type', $_POST)) {
+      if ($contactSubType = CRM_Utils_Array::value('contact_sub_type', $_POST)) {
         $paramSubType = implode(',', $contactSubType);
       }
 
@@ -361,7 +360,7 @@ class CRM_Contact_Form_Contact extends CRM_Core_Form {
       //custom field to load
       if ($this->_contactSubType || isset($paramSubType)) {
         $paramSubType = (isset($paramSubType)) ? $paramSubType :
-          str_replace( CRM_Core_DAO::VALUE_SEPARATOR, ',', trim($this->_contactSubType, CRM_Core_DAO::VALUE_SEPARATOR));
+          str_replace(CRM_Core_DAO::VALUE_SEPARATOR, ',', trim($this->_contactSubType, CRM_Core_DAO::VALUE_SEPARATOR));
 
         $this->assign('paramSubType', $paramSubType);
       }
@@ -388,11 +387,10 @@ class CRM_Contact_Form_Contact extends CRM_Core_Form {
    * Set default values for the form. Note that in edit/view mode
    * the default values are retrieved from the database
    *
-   * @access public
    *
    * @return void
    */
-  function setDefaultValues() {
+  public function setDefaultValues() {
     $defaults = $this->_values;
     $params = array();
 
@@ -433,8 +431,7 @@ class CRM_Contact_Form_Contact extends CRM_Core_Form {
     }
 
     //set address block defaults
-    CRM_Contact_Form_Edit_Address::setDefaultValues( $defaults, $this );
-
+    CRM_Contact_Form_Edit_Address::setDefaultValues($defaults, $this);
 
     if (!empty($defaults['image_URL'])) {
       list($imageWidth, $imageHeight) = getimagesize(CRM_Utils_String::unstupifyUrl($defaults['image_URL']));
@@ -456,9 +453,8 @@ class CRM_Contact_Form_Contact extends CRM_Core_Form {
   /**
    * Do the set default related to location type id,
    * primary location,  default country
-   *
    */
-  function blockSetDefaults(&$defaults) {
+  public function blockSetDefaults(&$defaults) {
     $locationTypeKeys = array_filter(array_keys(CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id')), 'is_int');
     sort($locationTypeKeys);
 
@@ -475,8 +471,8 @@ class CRM_Contact_Form_Contact extends CRM_Core_Form {
     // get default phone and im provider id.
     $defPhoneTypeId = key(CRM_Core_OptionGroup::values('phone_type', FALSE, FALSE, FALSE, ' AND is_default = 1'));
     $defIMProviderId = key(CRM_Core_OptionGroup::values('instant_messenger_service',
-        FALSE, FALSE, FALSE, ' AND is_default = 1'
-      ));
+      FALSE, FALSE, FALSE, ' AND is_default = 1'
+    ));
     $defWebsiteTypeId = key(CRM_Core_OptionGroup::values('website_type',
       FALSE, FALSE, FALSE, ' AND is_default = 1'
     ));
@@ -502,11 +498,10 @@ class CRM_Contact_Form_Contact extends CRM_Core_Form {
         // make we require one primary block, CRM-5505
         if ($updateMode) {
           if (!$hasPrimary) {
-            $hasPrimary =
-              CRM_Utils_Array::value(
-                'is_primary',
-                CRM_Utils_Array::value($instance, $defaults[$name])
-              );
+            $hasPrimary = CRM_Utils_Array::value(
+              'is_primary',
+              CRM_Utils_Array::value($instance, $defaults[$name])
+            );
           }
           continue;
         }
@@ -554,14 +549,13 @@ class CRM_Contact_Form_Contact extends CRM_Core_Form {
   }
 
   /**
-   * This function is used to add the rules (mainly global rules) for form.
+   * add the rules (mainly global rules) for form.
    * All local rules are added near the element
    *
    * @return void
-   * @access public
    * @see valid_date
    */
-  function addRules() {
+  public function addRules() {
     // skip adding formRules when custom data is build
     if ($this->_addBlockName || ($this->_action & CRM_Core_Action::DELETE)) {
       return;
@@ -586,14 +580,17 @@ class CRM_Contact_Form_Contact extends CRM_Core_Form {
   /**
    * Global validation rules for the form
    *
-   * @param array $fields posted values of the form
-   * @param array $errors list of errors to be posted back to the form
-   * @param int $contactId contact id if doing update.
+   * @param array $fields
+   *   Posted values of the form.
+   * @param array $errors
+   *   List of errors to be posted back to the form.
+   * @param int $contactId
+   *   Contact id if doing update.
    *
-   * @return bool $primaryID email/openId@static
-   * @access public
+   * @return bool
+   *   email/openId
    */
-  static function formRule($fields, &$errors, $contactId = NULL) {
+  public static function formRule($fields, &$errors, $contactId = NULL) {
     $config = CRM_Core_Config::singleton();
 
     // validations.
@@ -642,7 +639,10 @@ class CRM_Contact_Form_Contact extends CRM_Core_Form {
               $hasPrimary[] = $instance;
               if (!$primaryID &&
                 in_array($name, array(
-                  'email', 'openid')) && !empty($blockValues[$name])) {
+                  'email',
+                  'openid',
+                )) && !empty($blockValues[$name])
+              ) {
                 $primaryID = $blockValues[$name];
               }
             }
@@ -653,9 +653,9 @@ class CRM_Contact_Form_Contact extends CRM_Core_Form {
           }
 
           if ($name == 'openid' && !empty($blockValues[$name])) {
-            $oid         = new CRM_Core_DAO_OpenID();
+            $oid = new CRM_Core_DAO_OpenID();
             $oid->openid = $openIds[$instance] = CRM_Utils_Array::value($name, $blockValues);
-            $cid         = isset($contactId) ? $contactId : 0;
+            $cid = isset($contactId) ? $contactId : 0;
             if ($oid->find(TRUE) && ($oid->contact_id != $cid)) {
               $errors["{$name}[$instance][openid]"] = ts('%1 already exist.', array(1 => $blocks['OpenID']));
             }
@@ -705,7 +705,9 @@ class CRM_Contact_Form_Contact extends CRM_Core_Form {
 
         if (!empty($invalidStreetNumbers)) {
           $first = $invalidStreetNumbers[0];
-          foreach ($invalidStreetNumbers as & $num) $num = CRM_Contact_Form_Contact::ordinalNumber($num);
+          foreach ($invalidStreetNumbers as & $num) {
+            $num = CRM_Contact_Form_Contact::ordinalNumber($num);
+          }
           $errors["address[$first][street_number]"] = ts('The street number you entered for the %1 address block(s) is not in an expected format. Street numbers may include numeric digit(s) followed by other characters. You can still enter the complete street address (unparsed) by clicking "Edit Complete Street Address".', array(1 => implode(', ', $invalidStreetNumbers)));
         }
       }
@@ -718,7 +720,6 @@ class CRM_Contact_Form_Contact extends CRM_Core_Form {
    * Build the form object
    *
    * @return void
-   * @access public
    */
   public function buildQuickForm() {
     //load form for child blocks
@@ -730,8 +731,7 @@ class CRM_Contact_Form_Contact extends CRM_Core_Form {
     if ($this->_action == CRM_Core_Action::UPDATE) {
       $deleteExtra = ts('Are you sure you want to delete contact image.');
       $deleteURL = array(
-        CRM_Core_Action::DELETE =>
-        array(
+        CRM_Core_Action::DELETE => array(
           'name' => ts('Delete Contact Image'),
           'url' => 'civicrm/contact/image',
           'qs' => 'reset=1&cid=%%id%%&action=delete',
@@ -849,7 +849,6 @@ class CRM_Contact_Form_Contact extends CRM_Core_Form {
   /**
    * Form submission of new/edit contact is processed.
    *
-   * @access public
    *
    * @return void
    */
@@ -864,14 +863,14 @@ class CRM_Contact_Form_Contact extends CRM_Core_Form {
     $params = $this->controller->exportValues($this->_name);
 
     $group = CRM_Utils_Array::value('group', $params);
-    if ($group && is_array($group)) {
+    if (!empty($group) && is_array($group)) {
       unset($params['group']);
       foreach ($group as $key => $value) {
         $params['group'][$value] = 1;
       }
     }
 
-    CRM_Contact_BAO_Contact_Optimizer::edit( $params, $this->_preEditValues );
+    CRM_Contact_BAO_Contact_Optimizer::edit($params, $this->_preEditValues);
 
     if (!empty($params['image_URL'])) {
       CRM_Contact_BAO_Contact::processImageParams($params);
@@ -905,7 +904,8 @@ class CRM_Contact_Form_Contact extends CRM_Core_Form {
 
     if (isset($params['contact_id'])) {
       // process membership status for deceased contact
-      $deceasedParams = array('contact_id' => CRM_Utils_Array::value('contact_id', $params),
+      $deceasedParams = array(
+        'contact_id' => CRM_Utils_Array::value('contact_id', $params),
         'is_deceased' => CRM_Utils_Array::value('is_deceased', $params, FALSE),
         'deceased_date' => CRM_Utils_Array::value('deceased_date', $params, NULL),
       );
@@ -948,11 +948,11 @@ class CRM_Contact_Form_Contact extends CRM_Core_Form {
     // process shared contact address.
     CRM_Contact_BAO_Contact_Utils::processSharedAddress($params['address']);
 
-    if (!array_key_exists('TagsAndGroups', $this->_editOptions)) {
+    if (!array_key_exists('TagsAndGroups', $this->_editOptions) && !empty($params['group'])) {
       unset($params['group']);
     }
 
-    if (!empty($params['contact_id']) && ($this->_action & CRM_Core_Action::UPDATE)) {
+    if (!empty($params['contact_id']) && ($this->_action & CRM_Core_Action::UPDATE) && !empty($params['group'])) {
       // figure out which all groups are intended to be removed
       $contactGroupList = CRM_Contact_BAO_GroupContact::getContactGroup($params['contact_id'], 'Added');
       if (is_array($contactGroupList)) {
@@ -1062,18 +1062,26 @@ class CRM_Contact_Form_Contact extends CRM_Core_Form {
   /**
    * Is there any real significant data in the hierarchical location array
    *
-   * @param array $fields the hierarchical value representation of this location
+   * @param array $fields
+   *   The hierarchical value representation of this location.
    *
-   * @return boolean true if data exists, false otherwise
-   * @static
-   * @access public
+   * @return bool
+   *   true if data exists, false otherwise
    */
-  static function blockDataExists(&$fields) {
+  public static function blockDataExists(&$fields) {
     if (!is_array($fields)) {
       return FALSE;
     }
 
-    static $skipFields = array('location_type_id', 'is_primary', 'phone_type_id', 'provider_id', 'country_id', 'website_type_id', 'master_id');
+    static $skipFields = array(
+      'location_type_id',
+      'is_primary',
+      'phone_type_id',
+      'provider_id',
+      'country_id',
+      'website_type_id',
+      'master_id',
+    );
     foreach ($fields as $name => $value) {
       $skipField = FALSE;
       foreach ($skipFields as $skip) {
@@ -1106,13 +1114,15 @@ class CRM_Contact_Form_Contact extends CRM_Core_Form {
   /**
    * That checks for duplicate contacts
    *
-   * @param array $fields fields array which are submitted
+   * @param array $fields
+   *   Fields array which are submitted.
    * @param $errors
-   * @param int $contactID contact id
-   * @param string $contactType contact type
-   *
+   * @param int $contactID
+   *   Contact id.
+   * @param string $contactType
+   *   Contact type.
    */
-  static function checkDuplicateContacts(&$fields, &$errors, $contactID, $contactType) {
+  public static function checkDuplicateContacts(&$fields, &$errors, $contactID, $contactType) {
     // if this is a forced save, ignore find duplicate rule
     if (empty($fields['_qf_Contact_upload_duplicate'])) {
 
@@ -1123,12 +1133,21 @@ class CRM_Contact_Form_Contact extends CRM_Core_Form {
         $contactLinks = CRM_Contact_BAO_Contact_Utils::formatContactIDSToLinks($ids, TRUE, TRUE, $contactID);
 
         $duplicateContactsLinks = '<div class="matching-contacts-found">';
-        $duplicateContactsLinks .= ts('One matching contact was found. ', array('count' => count($contactLinks['rows']), 'plural' => '%count matching contacts were found.<br />'));
+        $duplicateContactsLinks .= ts('One matching contact was found. ', array(
+            'count' => count($contactLinks['rows']),
+            'plural' => '%count matching contacts were found.<br />',
+          ));
         if ($contactLinks['msg'] == 'view') {
-          $duplicateContactsLinks .= ts('You can View the existing contact', array('count' => count($contactLinks['rows']), 'plural' => 'You can View the existing contacts'));
+          $duplicateContactsLinks .= ts('You can View the existing contact', array(
+              'count' => count($contactLinks['rows']),
+              'plural' => 'You can View the existing contacts',
+            ));
         }
         else {
-          $duplicateContactsLinks .= ts('You can View or Edit the existing contact', array('count' => count($contactLinks['rows']), 'plural' => 'You can View or Edit the existing contacts'));
+          $duplicateContactsLinks .= ts('You can View or Edit the existing contact', array(
+              'count' => count($contactLinks['rows']),
+              'plural' => 'You can View or Edit the existing contacts',
+            ));
         }
         if ($contactLinks['msg'] == 'merge') {
           // We should also get a merge link if this is for an existing contact
@@ -1159,8 +1178,6 @@ class CRM_Contact_Form_Contact extends CRM_Core_Form {
 
         $errors['_qf_default'] = $duplicateContactsLinks;
 
-
-
         // let smarty know that there are duplicates
         $template = CRM_Core_Smarty::singleton();
         $template->assign('isDuplicate', 1);
@@ -1176,9 +1193,8 @@ class CRM_Contact_Form_Contact extends CRM_Core_Form {
    * Use the form name to create the tpl file name
    *
    * @return string
-   * @access public
    */
-  function getTemplateFileName() {
+  public function getTemplateFileName() {
     if ($this->_contactSubType) {
       $templateFile = "CRM/Contact/Form/Edit/SubType/{$this->_contactSubType}.tpl";
       $template = CRM_Core_Form::getTemplate();
@@ -1195,11 +1211,13 @@ class CRM_Contact_Form_Contact extends CRM_Core_Form {
    * This function either parse street address in to child
    * elements or build street address from child elements.
    *
-   * @param $params array of key value consist of address  blocks.
+   * @param array $params
+   *   of key value consist of address blocks.
    *
-   * @return array $parseSuccess as array of sucess/fails for each address block@static
+   * @return array
+   *   as array of sucess/fails for each address block
    */
-  function parseAddress(&$params) {
+  public function parseAddress(&$params) {
     $parseSuccess = $parsedFields = array();
     if (!is_array($params['address']) ||
       CRM_Utils_System::isNull($params['address'])
@@ -1211,7 +1229,10 @@ class CRM_Contact_Form_Contact extends CRM_Core_Form {
       $buildStreetAddress = FALSE;
       $parseFieldName = 'street_address';
       foreach (array(
-        'street_number', 'street_name', 'street_unit') as $fld) {
+                 'street_number',
+                 'street_name',
+                 'street_unit',
+               ) as $fld) {
         if (!empty($address[$fld])) {
           $parseFieldName = 'street_number';
           $buildStreetAddress = TRUE;
@@ -1240,9 +1261,15 @@ class CRM_Contact_Form_Contact extends CRM_Core_Form {
 
         $streetAddress = NULL;
         foreach (array(
-          'street_number', 'street_number_suffix', 'street_name', 'street_unit') as $fld) {
+                   'street_number',
+                   'street_number_suffix',
+                   'street_name',
+                   'street_unit',
+                 ) as $fld) {
           if (in_array($fld, array(
-            'street_name', 'street_unit'))) {
+            'street_name',
+            'street_unit',
+          ))) {
             $streetAddress .= ' ';
           }
           $streetAddress .= CRM_Utils_Array::value($fld, $address);
@@ -1280,11 +1307,13 @@ class CRM_Contact_Form_Contact extends CRM_Core_Form {
    * Check parse result and if some address block fails then this
    * function return the status message for all address blocks.
    *
-   * @param  $parseResult an array of address blk instance and its status.
+   * @param array $parseResult
+   *   An array of address blk instance and its status.
    *
-   * @return null|string $statusMsg   string status message for all address blocks.@static
+   * @return null|string
+   *   $statusMsg   string status message for all address blocks.
    */
-  static function parseAddressStatusMsg($parseResult) {
+  public static function parseAddressStatusMsg($parseResult) {
     $statusMsg = NULL;
     if (!is_array($parseResult) || empty($parseResult)) {
       return $statusMsg;
@@ -1310,12 +1339,13 @@ class CRM_Contact_Form_Contact extends CRM_Core_Form {
    * Convert normal number to ordinal number format.
    * like 1 => 1st, 2 => 2nd and so on...
    *
-   * @param  $number int number to convert in to ordinal number.
+   * @param int $number
+   *   number to convert in to ordinal number.
    *
-   * @return ordinal number for given number.
-   * @static
+   * @return string
+   *   ordinal number for given number.
    */
-  static function ordinalNumber($number) {
+  public static function ordinalNumber($number) {
     if (empty($number)) {
       return NULL;
     }
@@ -1346,18 +1376,21 @@ class CRM_Contact_Form_Contact extends CRM_Core_Form {
    * Update membership status to deceased
    * function return the status message for updated membership.
    *
-   * @param  $deceasedParams array  having contact id and deceased value.
+   * @param array $deceasedParams
+   *   having contact id and deceased value.
    *
-   * @return null|string $updateMembershipMsg string  status message for updated membership.
+   * @return null|string
+   *   $updateMembershipMsg string  status message for updated membership.
    */
-  function updateMembershipStatus($deceasedParams) {
+  public function updateMembershipStatus($deceasedParams) {
     $updateMembershipMsg = NULL;
-    $contactId           = CRM_Utils_Array::value('contact_id', $deceasedParams);
-    $deceasedDate        = CRM_Utils_Array::value('deceased_date', $deceasedParams);
+    $contactId = CRM_Utils_Array::value('contact_id', $deceasedParams);
+    $deceasedDate = CRM_Utils_Array::value('deceased_date', $deceasedParams);
 
     // process to set membership status to deceased for both active/inactive membership
     if ($contactId &&
-      $this->_contactType == 'Individual' && !empty($deceasedParams['is_deceased'])) {
+      $this->_contactType == 'Individual' && !empty($deceasedParams['is_deceased'])
+    ) {
 
       $session = CRM_Core_Session::singleton();
       $userId = $session->get('userID');
@@ -1365,7 +1398,6 @@ class CRM_Contact_Form_Contact extends CRM_Core_Form {
         $userId = $contactId;
       }
 
-
       // get deceased status id
       $allStatus = CRM_Member_PseudoConstant::membershipStatus();
       $deceasedStatusId = array_search('Deceased', $allStatus);
@@ -1384,7 +1416,7 @@ class CRM_Contact_Form_Contact extends CRM_Core_Form {
       $dao->whereAdd("status_id != $deceasedStatusId");
       $dao->find();
       $activityTypes = CRM_Core_PseudoConstant::activityType(TRUE, FALSE, FALSE, 'name');
-      $allStatus     = CRM_Member_PseudoConstant::membershipStatus();
+      $allStatus = CRM_Member_PseudoConstant::membershipStatus();
       $memCount = 0;
       while ($dao->fetch()) {
         // update status to deceased (for both active/inactive membership )
@@ -1404,7 +1436,6 @@ class CRM_Contact_Form_Contact extends CRM_Core_Form {
           'max_related' => $dao->max_related,
         );
 
-
         CRM_Member_BAO_MembershipLog::add($membershipLog, CRM_Core_DAO::$_nullArray);
 
         //create activity when membership status is changed
@@ -1437,5 +1468,5 @@ class CRM_Contact_Form_Contact extends CRM_Core_Form {
 
     return $updateMembershipMsg;
   }
-}
 
+}