Merge pull request #5513 from mallezie/contact-select-file-16178
[civicrm-core.git] / CRM / Contact / Form / Contact.php
index 2b9d53632285b214b47429c8a31482dee3a1ad04..95c265587d0732fe66899f88555180efc198eb51 100644 (file)
@@ -3,7 +3,7 @@
  +--------------------------------------------------------------------+
  | CiviCRM version 4.6                                                |
  +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2014                                |
+ | Copyright CiviCRM LLC (c) 2004-2015                                |
  +--------------------------------------------------------------------+
  | This file is a part of CiviCRM.                                    |
  |                                                                    |
  | GNU Affero General Public License or the licensing of CiviCRM,     |
  | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
  +--------------------------------------------------------------------+
-*/
+ */
 
 /**
  *
  * @package CRM
- * @copyright CiviCRM LLC (c) 2004-2014
+ * @copyright CiviCRM LLC (c) 2004-2015
  * $Id$
  *
  */
 class CRM_Contact_Form_Contact extends CRM_Core_Form {
 
   /**
-   * The contact type of the form
+   * The contact type of the form.
    *
    * @var string
    */
   public $_contactType;
 
   /**
-   * The contact type of the form
+   * The contact type of the form.
    *
    * @var string
    */
@@ -65,14 +65,14 @@ class CRM_Contact_Form_Contact extends CRM_Core_Form {
   public $_contactId;
 
   /**
-   * The default group id passed in via the url
+   * The default group id passed in via the url.
    *
    * @var int
    */
   public $_gid;
 
   /**
-   * The default tag id passed in via the url
+   * The default tag id passed in via the url.
    *
    * @var int
    */
@@ -86,7 +86,7 @@ class CRM_Contact_Form_Contact extends CRM_Core_Form {
   protected $_dedupeButtonName;
 
   /**
-   * Name of optional save duplicate button
+   * Name of optional save duplicate button.
    *
    * @var string
    */
@@ -104,7 +104,7 @@ class CRM_Contact_Form_Contact extends CRM_Core_Form {
 
   public $_customValueCount;
   /**
-   * The array of greetings with option group and filed names
+   * The array of greetings with option group and filed names.
    *
    * @var array
    */
@@ -116,7 +116,7 @@ class CRM_Contact_Form_Contact extends CRM_Core_Form {
   public $_parseStreetAddress;
 
   /**
-   * Check contact has a subtype or not
+   * Check contact has a subtype or not.
    */
   public $_isContactSubType;
 
@@ -128,7 +128,21 @@ class CRM_Contact_Form_Contact extends CRM_Core_Form {
   public $_preEditValues;
 
   /**
-   * Build all the data structures needed to build the form
+   * Explicitly declare the entity api name.
+   */
+  public function getDefaultEntity() {
+    return 'Contact';
+  }
+
+  /**
+   * Explicitly declare the form context.
+   */
+  public function getDefaultContext() {
+    return 'create';
+  }
+
+  /**
+   * Build all the data structures needed to build the form.
    *
    * @return void
    */
@@ -138,6 +152,9 @@ class CRM_Contact_Form_Contact extends CRM_Core_Form {
     $this->_dedupeButtonName = $this->getButtonName('refresh', 'dedupe');
     $this->_duplicateButtonName = $this->getButtonName('upload', 'duplicate');
 
+    CRM_Core_Resources::singleton()
+      ->addStyleFile('civicrm', 'css/contactSummary.css', 2, 'html-header');
+
     $session = CRM_Core_Session::singleton();
     if ($this->_action == CRM_Core_Action::ADD) {
       // check for add contacts permissions
@@ -193,7 +210,7 @@ class CRM_Contact_Form_Contact extends CRM_Core_Form {
       if ($this->_contactId) {
         $defaults = array();
         $params = array('id' => $this->_contactId);
-        $returnProperities = array('id', 'contact_type', 'contact_sub_type', 'modified_date');
+        $returnProperities = array('id', 'contact_type', 'contact_sub_type', 'modified_date', 'is_deceased');
         CRM_Core_DAO::commonRetrieve('CRM_Contact_DAO_Contact', $params, $defaults, $returnProperities);
 
         if (empty($defaults['id'])) {
@@ -210,6 +227,9 @@ class CRM_Contact_Form_Contact extends CRM_Core_Form {
         }
 
         $displayName = CRM_Contact_BAO_Contact::displayName($this->_contactId);
+        if ($defaults['is_deceased']) {
+          $displayName .= '  <span class="crm-contact-deceased">(deceased)</span>';
+        }
         $displayName = ts('Edit %1', array(1 => $displayName));
 
         // Check if this is default domain contact CRM-10482
@@ -433,7 +453,6 @@ class CRM_Contact_Form_Contact extends CRM_Core_Form {
     //set address block defaults
     CRM_Contact_Form_Edit_Address::setDefaultValues($defaults, $this);
 
-
     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);
@@ -499,11 +518,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;
         }
@@ -580,7 +598,7 @@ class CRM_Contact_Form_Contact extends CRM_Core_Form {
   }
 
   /**
-   * Global validation rules for the form
+   * Global validation rules for the form.
    *
    * @param array $fields
    *   Posted values of the form.
@@ -591,7 +609,6 @@ class CRM_Contact_Form_Contact extends CRM_Core_Form {
    *
    * @return bool
    *   email/openId
-   * @static
    */
   public static function formRule($fields, &$errors, $contactId = NULL) {
     $config = CRM_Core_Config::singleton();
@@ -643,7 +660,7 @@ class CRM_Contact_Form_Contact extends CRM_Core_Form {
               if (!$primaryID &&
                 in_array($name, array(
                   'email',
-                  'openid'
+                  'openid',
                 )) && !empty($blockValues[$name])
               ) {
                 $primaryID = $blockValues[$name];
@@ -720,7 +737,7 @@ class CRM_Contact_Form_Contact extends CRM_Core_Form {
   }
 
   /**
-   * Build the form object
+   * Build the form object.
    *
    * @return void
    */
@@ -734,14 +751,13 @@ 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(
-            'name' => ts('Delete Contact Image'),
-            'url' => 'civicrm/contact/image',
-            'qs' => 'reset=1&cid=%%id%%&action=delete',
-            'extra' =>
-              'onclick = "if (confirm( \'' . $deleteExtra . '\' ) ) this.href+=\'&amp;confirmed=1\'; else return false;"',
-          ),
+        CRM_Core_Action::DELETE => array(
+          'name' => ts('Delete Contact Image'),
+          'url' => 'civicrm/contact/image',
+          'qs' => 'reset=1&cid=%%id%%&action=delete',
+          'extra' =>
+          'onclick = "if (confirm( \'' . $deleteExtra . '\' ) ) this.href+=\'&amp;confirmed=1\'; else return false;"',
+        ),
       );
       $deleteURL = CRM_Core_Action::formLink($deleteURL,
         CRM_Core_Action::DELETE,
@@ -770,13 +786,13 @@ class CRM_Contact_Form_Contact extends CRM_Core_Form {
     // subtype is a common field. lets keep it here
     $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',
-          'class' => $buildCustomData . ' crm-select2',
-        )
+      $this->addField('contact_sub_type', array(
+        'label' => ts('Contact Type'),
+        'options' => $subtypes,
+        'class' => $buildCustomData,
+        'multiple' => 'multiple',
+        'options-url' => FALSE,
+          )
       );
     }
 
@@ -802,8 +818,7 @@ class CRM_Contact_Form_Contact extends CRM_Core_Form {
     CRM_Contact_Form_Location::buildQuickForm($this);
 
     // add attachment
-    $this->addElement('file', 'image_URL', ts('Browse/Upload Image'), 'size=30 maxlength=60');
-    $this->addUploadElement('image_URL');
+    $this->addField('image_URL', array('maxlength' => '60', 'label' => ts('Browse/Upload Image')));
 
     // add the dedupe button
     $this->addElement('submit',
@@ -867,7 +882,7 @@ 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;
@@ -952,11 +967,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)) {
@@ -1064,14 +1079,13 @@ class CRM_Contact_Form_Contact extends CRM_Core_Form {
   }
 
   /**
-   * Is there any real significant data in the hierarchical location array
+   * Is there any real significant data in the hierarchical location array.
    *
    * @param array $fields
    *   The hierarchical value representation of this location.
    *
-   * @return boolean
+   * @return bool
    *   true if data exists, false otherwise
-   * @static
    */
   public static function blockDataExists(&$fields) {
     if (!is_array($fields)) {
@@ -1085,7 +1099,7 @@ class CRM_Contact_Form_Contact extends CRM_Core_Form {
       'provider_id',
       'country_id',
       'website_type_id',
-      'master_id'
+      'master_id',
     );
     foreach ($fields as $name => $value) {
       $skipField = FALSE;
@@ -1117,7 +1131,7 @@ class CRM_Contact_Form_Contact extends CRM_Core_Form {
   }
 
   /**
-   * That checks for duplicate contacts
+   * That checks for duplicate contacts.
    *
    * @param array $fields
    *   Fields array which are submitted.
@@ -1140,18 +1154,18 @@ class CRM_Contact_Form_Contact extends CRM_Core_Form {
         $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 />'
+            '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'
+              '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'
+              'plural' => 'You can View or Edit the existing contacts',
             ));
         }
         if ($contactLinks['msg'] == 'merge') {
@@ -1183,7 +1197,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);
@@ -1196,7 +1209,7 @@ class CRM_Contact_Form_Contact extends CRM_Core_Form {
   }
 
   /**
-   * Use the form name to create the tpl file name
+   * Use the form name to create the tpl file name.
    *
    * @return string
    */
@@ -1222,7 +1235,6 @@ class CRM_Contact_Form_Contact extends CRM_Core_Form {
    *
    * @return array
    *   as array of sucess/fails for each address block
-   * @static
    */
   public function parseAddress(&$params) {
     $parseSuccess = $parsedFields = array();
@@ -1238,7 +1250,7 @@ class CRM_Contact_Form_Contact extends CRM_Core_Form {
       foreach (array(
                  'street_number',
                  'street_name',
-                 'street_unit'
+                 'street_unit',
                ) as $fld) {
         if (!empty($address[$fld])) {
           $parseFieldName = 'street_number';
@@ -1271,11 +1283,11 @@ class CRM_Contact_Form_Contact extends CRM_Core_Form {
                    'street_number',
                    'street_number_suffix',
                    'street_name',
-                   'street_unit'
+                   'street_unit',
                  ) as $fld) {
           if (in_array($fld, array(
             'street_name',
-            'street_unit'
+            'street_unit',
           ))) {
             $streetAddress .= ' ';
           }
@@ -1319,7 +1331,6 @@ class CRM_Contact_Form_Contact extends CRM_Core_Form {
    *
    * @return null|string
    *   $statusMsg   string status message for all address blocks.
-   * @static
    */
   public static function parseAddressStatusMsg($parseResult) {
     $statusMsg = NULL;
@@ -1352,7 +1363,6 @@ class CRM_Contact_Form_Contact extends CRM_Core_Form {
    *
    * @return string
    *   ordinal number for given number.
-   * @static
    */
   public static function ordinalNumber($number) {
     if (empty($number)) {
@@ -1382,7 +1392,7 @@ class CRM_Contact_Form_Contact extends CRM_Core_Form {
   }
 
   /**
-   * Update membership status to deceased
+   * Update membership status to deceased.
    * function return the status message for updated membership.
    *
    * @param array $deceasedParams
@@ -1407,7 +1417,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);
@@ -1446,7 +1455,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
@@ -1479,4 +1487,5 @@ class CRM_Contact_Form_Contact extends CRM_Core_Form {
 
     return $updateMembershipMsg;
   }
+
 }