Merge pull request #3905 from colemanw/Use
[civicrm-core.git] / CRM / Contact / Form / Contact.php
index 204ea6fe0ab42d0aaf2012c82613268a9f1481b6..2ca53a78969caa36deb6f003306714c64256a195 100644 (file)
@@ -1,9 +1,9 @@
 <?php
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.4                                                |
+ | CiviCRM version 4.5                                                |
  +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2013                                |
+ | Copyright CiviCRM LLC (c) 2004-2014                                |
  +--------------------------------------------------------------------+
  | This file is a part of CiviCRM.                                    |
  |                                                                    |
@@ -28,7 +28,7 @@
 /**
  *
  * @package CRM
- * @copyright CiviCRM LLC (c) 2004-2013
+ * @copyright CiviCRM LLC (c) 2004-2014
  * $Id$
  *
  */
@@ -197,7 +197,7 @@ class CRM_Contact_Form_Contact extends CRM_Core_Form {
         $returnProperities = array('id', 'contact_type', 'contact_sub_type', 'modified_date');
         CRM_Core_DAO::commonRetrieve('CRM_Contact_DAO_Contact', $params, $defaults, $returnProperities);
 
-        if (!CRM_Utils_Array::value('id', $defaults)) {
+        if (empty($defaults['id'])) {
           CRM_Core_Error::statusBounce(ts('A Contact with that ID does not exist: %1', array(1 => $this->_contactId)));
         }
 
@@ -265,9 +265,7 @@ class CRM_Contact_Form_Contact extends CRM_Core_Form {
         'address_options'
       );
       $this->_parseStreetAddress = FALSE;
-      if (CRM_Utils_Array::value('street_address', $addressOptions) &&
-        CRM_Utils_Array::value('street_address_parsing', $addressOptions)
-      ) {
+      if (!empty($addressOptions['street_address']) && !empty($addressOptions['street_address_parsing'])) {
         $this->_parseStreetAddress = TRUE;
       }
       $this->set('parseStreetAddress', $this->_parseStreetAddress);
@@ -327,7 +325,7 @@ class CRM_Contact_Form_Contact extends CRM_Core_Form {
     CRM_Contact_Form_Location::preProcess($this);
 
     // retain the multiple count custom fields value
-    if(CRM_Utils_Array::value('hidden_custom', $_POST)) {
+    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)) {
@@ -374,7 +372,7 @@ class CRM_Contact_Form_Contact extends CRM_Core_Form {
       else {
         $contactSubType = $this->_contactSubType;
         // need contact sub type to build related grouptree array during post process
-        if (CRM_Utils_Array::value('contact_sub_type', $_POST)) {
+        if (!empty($_POST['contact_sub_type'])) {
           $contactSubType = $_POST['contact_sub_type'];
         }
         //only custom data has preprocess hence directly call it
@@ -392,7 +390,7 @@ class CRM_Contact_Form_Contact extends CRM_Core_Form {
    *
    * @access public
    *
-   * @return None
+   * @return void
    */
   function setDefaultValues() {
     $defaults = $this->_values;
@@ -413,9 +411,6 @@ class CRM_Contact_Form_Contact extends CRM_Core_Form {
       }
     }
     else {
-      $currentEmployer = CRM_Contact_BAO_Relationship::getCurrentEmployer(array($this->_contactId));
-      $defaults['current_employer_id'] = CRM_Utils_Array::value('org_id', $currentEmployer[$this->_contactId]);
-
       foreach ($defaults['email'] as $dontCare => & $val) {
         if (isset($val['signature_text'])) {
           $val['signature_text_hidden'] = $val['signature_text'];
@@ -425,12 +420,10 @@ class CRM_Contact_Form_Contact extends CRM_Core_Form {
         }
       }
 
-      if (CRM_Utils_Array::value('contact_sub_type', $defaults)) {
+      if (!empty($defaults['contact_sub_type'])) {
         $defaults['contact_sub_type'] = $this->_oldSubtypes;
       }
     }
-    $this->assign('currentEmployer', CRM_Utils_Array::value('current_employer_id', $defaults));
-
     // set defaults for blocks ( custom data, address, communication preference, notes, tags and groups )
     foreach ($this->_editOptions as $name => $label) {
       if (!in_array($name, array('Address', 'Notes'))) {
@@ -442,7 +435,8 @@ class CRM_Contact_Form_Contact extends CRM_Core_Form {
     //set address block defaults
     CRM_Contact_Form_Edit_Address::setDefaultValues( $defaults, $this );
 
-    if (CRM_Utils_Array::value('image_URL', $defaults)) {
+
+    if (!empty($defaults['image_URL'])) {
       list($imageWidth, $imageHeight) = getimagesize(CRM_Utils_String::unstupifyUrl($defaults['image_URL']));
       list($imageThumbWidth, $imageThumbHeight) = CRM_Contact_BAO_Contact::getThumbSize($imageWidth, $imageHeight);
       $this->assign('imageWidth', $imageWidth);
@@ -483,6 +477,9 @@ class CRM_Contact_Form_Contact extends CRM_Core_Form {
     $defIMProviderId = key(CRM_Core_OptionGroup::values('instant_messenger_service',
         FALSE, FALSE, FALSE, ' AND is_default = 1'
       ));
+    $defWebsiteTypeId = key(CRM_Core_OptionGroup::values('website_type',
+      FALSE, FALSE, FALSE, ' AND is_default = 1'
+    ));
 
     $allBlocks = $this->_blocks;
     if (array_key_exists('Address', $this->_editOptions)) {
@@ -539,6 +536,10 @@ class CRM_Contact_Form_Contact extends CRM_Core_Form {
         if ($name == 'phone' && $defPhoneTypeId) {
           $defaults[$name][$instance]['phone_type_id'] = $defPhoneTypeId;
         }
+        //set default website type.
+        if ($name == 'website' && $defWebsiteTypeId) {
+          $defaults[$name][$instance]['website_type_id'] = $defWebsiteTypeId;
+        }
 
         //set default im provider.
         if ($name == 'im' && $defIMProviderId) {
@@ -552,7 +553,7 @@ class CRM_Contact_Form_Contact extends CRM_Core_Form {
     }
 
     // set defaults for country-state widget
-    if (CRM_Utils_Array::value('address', $defaults) && is_array($defaults['address'])) {
+    if (!empty($defaults['address']) && is_array($defaults['address'])) {
       foreach ($defaults['address'] as $blockId => $values) {
         CRM_Contact_Form_Edit_Address::fixStateSelect($this,
           "address[$blockId][country_id]",
@@ -573,7 +574,7 @@ class CRM_Contact_Form_Contact extends CRM_Core_Form {
    * This function is used to add the rules (mainly global rules) for form.
    * All local rules are added near the element
    *
-   * @return None
+   * @return void
    * @access public
    * @see valid_date
    */
@@ -602,12 +603,11 @@ 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 $primaryID email/openId
-   * @static
+   * @return bool $primaryID email/openId@static
    * @access public
    */
   static function formRule($fields, &$errors, $contactId = NULL) {
@@ -640,7 +640,7 @@ class CRM_Contact_Form_Contact extends CRM_Core_Form {
     foreach ($blocks as $name => $label) {
       $hasData = $hasPrimary = array();
       $name = strtolower($name);
-      if (CRM_Utils_Array::value($name, $fields) && is_array($fields[$name])) {
+      if (!empty($fields[$name]) && is_array($fields[$name])) {
         foreach ($fields[$name] as $instance => $blockValues) {
           $dataExists = self::blockDataExists($blockValues);
 
@@ -655,23 +655,21 @@ class CRM_Contact_Form_Contact extends CRM_Core_Form {
             }
 
             $hasData[] = $instance;
-            if (CRM_Utils_Array::value('is_primary', $blockValues)) {
+            if (!empty($blockValues['is_primary'])) {
               $hasPrimary[] = $instance;
               if (!$primaryID &&
                 in_array($name, array(
-                  'email', 'openid')) &&
-                CRM_Utils_Array::value($name, $blockValues)
-              ) {
+                  'email', 'openid')) && !empty($blockValues[$name])) {
                 $primaryID = $blockValues[$name];
               }
             }
 
-            if (!CRM_Utils_Array::value('location_type_id', $blockValues)) {
+            if (empty($blockValues['location_type_id'])) {
               $errors["{$name}[$instance][location_type_id]"] = ts('The Location Type should be set if there is  %1 information.', array(1 => $label));
             }
           }
 
-          if ($name == 'openid' && CRM_Utils_Array::value($name, $blockValues)) {
+          if ($name == 'openid' && !empty($blockValues[$name])) {
             $oid         = new CRM_Core_DAO_OpenID();
             $oid->openid = $openIds[$instance] = CRM_Utils_Array::value($name, $blockValues);
             $cid         = isset($contactId) ? $contactId : 0;
@@ -716,7 +714,7 @@ class CRM_Contact_Form_Contact extends CRM_Core_Form {
         foreach ($fields['address'] as $cnt => $address) {
           if ($streetNumber = CRM_Utils_Array::value('street_number', $address)) {
             $parsedAddress = CRM_Core_BAO_Address::parseStreetAddress($address['street_number']);
-            if (!CRM_Utils_Array::value('street_number', $parsedAddress)) {
+            if (empty($parsedAddress['street_number'])) {
               $invalidStreetNumbers[] = $cnt;
             }
           }
@@ -736,7 +734,7 @@ class CRM_Contact_Form_Contact extends CRM_Core_Form {
   /**
    * Function to actually build the form
    *
-   * @return None
+   * @return void
    * @access public
    */
   public function buildQuickForm() {
@@ -762,7 +760,12 @@ class CRM_Contact_Form_Contact extends CRM_Core_Form {
         CRM_Core_Action::DELETE,
         array(
           'id' => $this->_contactId,
-        )
+        ),
+        ts('more'),
+        FALSE,
+        'contact.image.delete',
+        'Contact',
+        $this->_contactId
       );
       $this->assign('deleteURL', $deleteURL);
     }
@@ -778,15 +781,14 @@ class CRM_Contact_Form_Contact extends CRM_Core_Form {
     }
 
     // subtype is a common field. lets keep it here
-    $subtypes = CRM_Contact_BAO_ContactType::subTypePairs($this->_contactType);
+    $subtypes = CRM_Contact_BAO_Contact::buildOptions('contact_sub_type', 'create', array('contact_type' => $this->_contactType));
     if (!empty($subtypes)) {
       $sel = $this->add('select', 'contact_sub_type', ts('Contact Type'),
         $subtypes, FALSE,
         array(
           'id' => 'contact_sub_type',
           'multiple' => 'multiple',
-          'title' => '- ' . ts('select') . ' -',
-          'class' => $buildCustomData,
+          'class' => $buildCustomData . ' crm-select2',
         )
       );
     }
@@ -806,7 +808,7 @@ class CRM_Contact_Form_Contact extends CRM_Core_Form {
 
     // build tags and groups
     CRM_Contact_Form_Edit_TagsAndGroups::buildQuickForm($this, 0, CRM_Contact_Form_Edit_TagsAndGroups::ALL,
-      FALSE, NULL, 'Group(s)', 'Tag(s)', NULL, 'crmasmSelect');
+      FALSE, NULL, 'Group(s)', 'Tag(s)', NULL, 'select');
 
     // build location blocks.
     CRM_Contact_Form_Edit_Lock::buildQuickForm($this);
@@ -849,7 +851,7 @@ class CRM_Contact_Form_Contact extends CRM_Core_Form {
       ),
     );
 
-    if (CRM_Utils_Array::value('contact_sub_type', $this->_values)) {
+    if (!empty($this->_values['contact_sub_type'])) {
       $this->_oldSubtypes = explode(CRM_Core_DAO::VALUE_SEPARATOR,
         trim($this->_values['contact_sub_type'], CRM_Core_DAO::VALUE_SEPARATOR)
       );
@@ -864,7 +866,7 @@ class CRM_Contact_Form_Contact extends CRM_Core_Form {
    *
    * @access public
    *
-   * @return None
+   * @return void
    */
   public function postProcess() {
     // check if dedupe button, if so return.
@@ -886,13 +888,11 @@ class CRM_Contact_Form_Contact extends CRM_Core_Form {
 
     CRM_Contact_BAO_Contact_Optimizer::edit( $params, $this->_preEditValues );
 
-    if (CRM_Utils_Array::value('image_URL', $params)) {
+    if (!empty($params['image_URL'])) {
       CRM_Contact_BAO_Contact::processImageParams($params);
     }
 
-    if (is_numeric(CRM_Utils_Array::value('current_employer_id', $params))
-      && CRM_Utils_Array::value('current_employer', $params)
-    ) {
+    if (is_numeric(CRM_Utils_Array::value('current_employer_id', $params)) && !empty($params['current_employer'])) {
       $params['current_employer'] = $params['current_employer_id'];
     }
 
@@ -913,10 +913,7 @@ class CRM_Contact_Form_Contact extends CRM_Core_Form {
     }
 
     //make deceased date null when is_deceased = false
-    if ($this->_contactType == 'Individual' &&
-      CRM_Utils_Array::value('Demographics', $this->_editOptions) &&
-      !CRM_Utils_Array::value('is_deceased', $params)
-    ) {
+    if ($this->_contactType == 'Individual' && !empty($this->_editOptions['Demographics']) && empty($params['is_deceased'])) {
       $params['is_deceased'] = FALSE;
       $params['deceased_date'] = NULL;
     }
@@ -970,14 +967,12 @@ class CRM_Contact_Form_Contact extends CRM_Core_Form {
       unset($params['group']);
     }
 
-    if (CRM_Utils_Array::value('contact_id', $params) && ($this->_action & CRM_Core_Action::UPDATE)) {
+    if (!empty($params['contact_id']) && ($this->_action & CRM_Core_Action::UPDATE)) {
       // figure out which all groups are intended to be removed
       $contactGroupList = CRM_Contact_BAO_GroupContact::getContactGroup($params['contact_id'], 'Added');
       if (is_array($contactGroupList)) {
         foreach ($contactGroupList as $key) {
-          if ((!array_key_exists($key['group_id'], $params['group']) || $params['group'][$key['group_id']] != 1)
-            && !CRM_Utils_Array::value('is_hidden', $key)
-          ) {
+          if ((!array_key_exists($key['group_id'], $params['group']) || $params['group'][$key['group_id']] != 1) && empty($key['is_hidden'])) {
             $params['group'][$key['group_id']] = -1;
           }
         }
@@ -1126,14 +1121,16 @@ class CRM_Contact_Form_Contact extends CRM_Core_Form {
   /**
    * Function to that checks for duplicate contacts
    *
-   *  @param array  $fields      fields array which are submitted
-   *  @param array  $error       error message array
-   *  @param int    $contactID   contact id
-   *  @param string $contactType contact type
+   * @param array $fields fields array which are submitted
+   * @param $errors
+   * @param int $contactID contact id
+   * @param string $contactType contact type
+   *
+   * @internal param array $error error message array
    */
   static function checkDuplicateContacts(&$fields, &$errors, $contactID, $contactType) {
     // if this is a forced save, ignore find duplicate rule
-    if (!CRM_Utils_Array::value('_qf_Contact_upload_duplicate', $fields)) {
+    if (empty($fields['_qf_Contact_upload_duplicate'])) {
 
       $dedupeParams = CRM_Dedupe_Finder::formatParams($fields, $contactType);
       $ids = CRM_Dedupe_Finder::dupesByParams($dedupeParams, $contactType, 'Supervised', array($contactID));
@@ -1184,13 +1181,22 @@ class CRM_Contact_Form_Contact extends CRM_Core_Form {
         $template = CRM_Core_Smarty::singleton();
         $template->assign('isDuplicate', 1);
       }
-      elseif (CRM_Utils_Array::value('_qf_Contact_refresh_dedupe', $fields)) {
+      elseif (!empty($fields['_qf_Contact_refresh_dedupe'])) {
         // add a session message for no matching contacts
         CRM_Core_Session::setStatus(ts('No matching contact found.'), ts('None Found'), 'info');
       }
     }
   }
 
+  /**
+   * Use the form name to create the tpl file name
+   *
+   * @return string
+   * @access public
+   */
+  /**
+   * @return string
+   */
   function getTemplateFileName() {
     if ($this->_contactSubType) {
       $templateFile = "CRM/Contact/Form/Edit/SubType/{$this->_contactSubType}.tpl";
@@ -1204,15 +1210,16 @@ class CRM_Contact_Form_Contact extends CRM_Core_Form {
 
   /**
    * Parse all address blocks present in given params
-     * and return parse result for all address blocks,
-     * This function either parse street address in to child
-     * elements or build street address from child elements.
-     *
-     * @params $params an array of key value consist of address  blocks.
-     *
-   * @return $parseSuccess as array of sucess/fails for each address block
-   * @static
-     */
+   * and return parse result for all address blocks,
+   * This function either parse street address in to child
+   * elements or build street address from child elements.
+   *
+   * @params $params an array of key value consist of address  blocks.
+   *
+   * @param $params
+   *
+   * @return array $parseSuccess as array of sucess/fails for each address block@static
+   */
   function parseAddress(&$params) {
     $parseSuccess = $parsedFields = array();
     if (!is_array($params['address']) ||
@@ -1226,7 +1233,7 @@ class CRM_Contact_Form_Contact extends CRM_Core_Form {
       $parseFieldName = 'street_address';
       foreach (array(
         'street_number', 'street_name', 'street_unit') as $fld) {
-        if (CRM_Utils_Array::value($fld, $address)) {
+        if (!empty($address[$fld])) {
           $parseFieldName = 'street_number';
           $buildStreetAddress = TRUE;
           break;
@@ -1268,9 +1275,7 @@ class CRM_Contact_Form_Contact extends CRM_Core_Form {
         $success = TRUE;
         // consider address is automatically parseable,
         // when we should found street_number and street_name
-        if (!CRM_Utils_Array::value('street_name', $parsedFields) ||
-          !CRM_Utils_Array::value('street_number', $parsedFields)
-        ) {
+        if (empty($parsedFields['street_name']) || empty($parsedFields['street_number'])) {
           $success = FALSE;
         }
 
@@ -1298,8 +1303,7 @@ class CRM_Contact_Form_Contact extends CRM_Core_Form {
    *
    * @param  $parseResult an array of address blk instance and its status.
    *
-   * @return $statusMsg   string status message for all address blocks.
-   * @static
+   * @return null|string $statusMsg   string status message for all address blocks.@static
    */
   static function parseAddressStatusMsg($parseResult) {
     $statusMsg = NULL;
@@ -1365,7 +1369,7 @@ class CRM_Contact_Form_Contact extends CRM_Core_Form {
    *
    * @param  $deceasedParams array  having contact id and deceased value.
    *
-   * @return $updateMembershipMsg string  status message for updated membership.
+   * @return null|string $updateMembershipMsg string  status message for updated membership.
    */
   function updateMembershipStatus($deceasedParams) {
     $updateMembershipMsg = NULL;
@@ -1374,9 +1378,7 @@ class CRM_Contact_Form_Contact extends CRM_Core_Form {
 
     // process to set membership status to deceased for both active/inactive membership
     if ($contactId &&
-      $this->_contactType == 'Individual' &&
-      CRM_Utils_Array::value('is_deceased', $deceasedParams)
-    ) {
+      $this->_contactType == 'Individual' && !empty($deceasedParams['is_deceased'])) {
 
       $session = CRM_Core_Session::singleton();
       $userId = $session->get('userID');