Merge branch 4.6 into master
[civicrm-core.git] / CRM / Contact / Form / Edit / Individual.php
index cc4ccd19712d20035e090c3f0a8ea7fc52b8fe5d..4655e5fddee1df516869f9984b0ca70034029893 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$
  *
  */
@@ -44,8 +44,10 @@ class CRM_Contact_Form_Edit_Individual {
    * This function provides the HTML form elements that are specific
    * to the Individual Contact Type
    *
-   * @param CRM_Core_Form $form form object
-   * @param int $inlineEditMode ( 1 for contact summary
+   * @param CRM_Core_Form $form
+   *   Form object.
+   * @param int $inlineEditMode
+   *   ( 1 for contact summary.
    * top bar form and 2 for display name edit )
    *
    * @return void
@@ -53,103 +55,86 @@ class CRM_Contact_Form_Edit_Individual {
   public static function buildQuickForm(&$form, $inlineEditMode = NULL) {
     $form->applyFilter('__ALL__', 'trim');
 
-    if ( !$inlineEditMode || $inlineEditMode == 1 ) {
+    if (!$inlineEditMode || $inlineEditMode == 1) {
       $nameFields = CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
         'contact_edit_options', TRUE, NULL,
         FALSE, 'name', TRUE, 'AND v.filter = 2'
       );
-
-      //prefix
-      $prefix = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'prefix_id');
-      if (isset($nameFields['Prefix']) && !empty($prefix)) {
-        $form->addSelect('prefix_id', array('class' => 'eight', 'placeholder' => ' ', 'label' => ts('Prefix')));
-      }
-
-      $attributes = CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Contact');
-
-      if (isset($nameFields['Formal Title'])) {
-        $form->addElement('text', 'formal_title', ts('Title'), $attributes['formal_title']);
-      }
-
-      // first_name
-      if (isset($nameFields['First Name'])) {
-        $form->addElement('text', 'first_name', ts('First Name'), $attributes['first_name']);
+      // Fixme: dear god why? these come out in a format that is NOT the name of the fields.
+      foreach ($nameFields as &$fix) {
+        $fix = str_replace(' ', '_', strtolower($fix));
+        if ($fix == 'prefix' || $fix == 'suffix') {
+          // God, why god?
+          $fix .= '_id';
+        }
       }
 
-      //middle_name
-      if (isset($nameFields['Middle Name'])) {
-        $form->addElement('text', 'middle_name', ts('Middle Name'), $attributes['middle_name']);
-      }
-
-      // last_name
-      if (isset($nameFields['Last Name'])) {
-        $form->addElement('text', 'last_name', ts('Last Name'), $attributes['last_name']);
-      }
-
-      // suffix
-      $suffix = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'suffix_id');
-      if (isset($nameFields['Suffix']) && $suffix) {
-        $form->addSelect('suffix_id', array('class' => 'eight', 'placeholder' => ' ', 'label' => ts('Suffix')));
+      foreach ($nameFields as $name) {
+        $props = array();
+        if ($name == 'prefix_id' || $name == 'suffix_id') {
+          $options = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', $name);
+          // Skip if we have no options available
+          if (!CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', $name)) {
+            //continue;
+          }
+          $props = array('class' => 'eight', 'placeholder' => ' ', 'label' => $name == 'prefix_id' ? ts('Prefix') : ts('Suffix'));
+        }
+        $form->addField($name, $props);
       }
     }
 
-    if ( !$inlineEditMode || $inlineEditMode == 2 ) {
+    if (!$inlineEditMode || $inlineEditMode == 2) {
       // nick_name
-      $form->addElement('text', 'nick_name', ts('Nickname'),
-        CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Contact', 'nick_name')
-      );
+      $form->addField('nick_name');
 
       // job title
       // override the size for UI to look better
-      $attributes['job_title']['size'] = 30;
-      $form->addElement('text', 'job_title', ts('Job Title'), $attributes['job_title'], 'size="30"');
+      $form->addField('job_title', array('size' => '30'));
 
       //Current Employer Element
       $props = array(
         'api' => array('params' => array('contact_type' => 'Organization')),
         'create' => TRUE,
       );
-      $form->addEntityRef('employer_id', ts('Current Employer'), $props);
-      $attributes['source']['class'] = 'big';
-      $form->addElement('text', 'contact_source', ts('Source'), CRM_Utils_Array::value('source', $attributes));
+      $form->addField('employer_id', $props);
+      $form->addField('contact_source', array('class' => 'big'));
     }
 
-    if ( !$inlineEditMode ) {
+    if (!$inlineEditMode) {
       $checkSimilar = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
         'contact_ajax_check_similar',
         NULL,
         TRUE
       );
 
-      if ( $checkSimilar == null ) {
+      if ($checkSimilar == NULL) {
         $checkSimilar = 0;
       }
       $form->assign('checkSimilar', $checkSimilar);
 
       //External Identifier Element
-      $form->add('text', 'external_identifier', ts('External ID'),
-        CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Contact', 'external_identifier'), FALSE
-      );
+      $form->addField('external_identifier', array('label' => 'External ID'));
 
       $form->addRule('external_identifier',
         ts('External ID already exists in Database.'),
         'objectExists',
         array('CRM_Contact_DAO_Contact', $form->_contactId, 'external_identifier')
       );
-      $config = CRM_Core_Config::singleton();
       CRM_Core_ShowHideBlocks::links($form, 'demographics', '', '');
     }
   }
 
   /**
-   * Global form rule
+   * Global form rule.
    *
-   * @param array $fields the input form values
-   * @param array $files the uploaded files if any
+   * @param array $fields
+   *   The input form values.
+   * @param array $files
+   *   The uploaded files if any.
    * @param int $contactID
    *
-   * @return true if no errors, else array of errors
-   * @static
+   * @return bool
+   *   TRUE if no errors, else array of errors.
    */
   public static function formRule($fields, $files, $contactID = NULL) {
     $errors = array();
@@ -165,4 +150,5 @@ class CRM_Contact_Form_Edit_Individual {
 
     return empty($errors) ? TRUE : $errors;
   }
+
 }