627af134b9ee088bf5e1d32bd5565c288681e98f
[civicrm-core.git] / CRM / Contact / Form / Edit / Individual.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2014
32 * $Id$
33 *
34 */
35
36 /**
37 * Auxilary class to provide support to the Contact Form class. Does this by implementing
38 * a small set of static methods
39 *
40 */
41 class CRM_Contact_Form_Edit_Individual {
42
43 /**
44 * This function provides the HTML form elements that are specific
45 * to the Individual Contact Type
46 *
47 * @param CRM_Core_Form $form
48 * Form object.
49 * @param int $inlineEditMode
50 * ( 1 for contact summary.
51 * top bar form and 2 for display name edit )
52 *
53 * @return void
54 */
55 public static function buildQuickForm(&$form, $inlineEditMode = NULL) {
56 $form->applyFilter('__ALL__', 'trim');
57
58 if (!$inlineEditMode || $inlineEditMode == 1) {
59 $nameFields = CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
60 'contact_edit_options', TRUE, NULL,
61 FALSE, 'name', TRUE, 'AND v.filter = 2'
62 );
63
64 //prefix
65 $prefix = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'prefix_id');
66 if (isset($nameFields['Prefix']) && !empty($prefix)) {
67 $form->addSelect('prefix_id', array('class' => 'eight', 'placeholder' => ' ', 'label' => ts('Prefix')));
68 }
69
70 $attributes = CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Contact');
71
72 if (isset($nameFields['Formal Title'])) {
73 $form->addElement('text', 'formal_title', ts('Title'), $attributes['formal_title']);
74 }
75
76 // first_name
77 if (isset($nameFields['First Name'])) {
78 $form->addElement('text', 'first_name', ts('First Name'), $attributes['first_name']);
79 }
80
81 //middle_name
82 if (isset($nameFields['Middle Name'])) {
83 $form->addElement('text', 'middle_name', ts('Middle Name'), $attributes['middle_name']);
84 }
85
86 // last_name
87 if (isset($nameFields['Last Name'])) {
88 $form->addElement('text', 'last_name', ts('Last Name'), $attributes['last_name']);
89 }
90
91 // suffix
92 $suffix = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'suffix_id');
93 if (isset($nameFields['Suffix']) && $suffix) {
94 $form->addSelect('suffix_id', array('class' => 'eight', 'placeholder' => ' ', 'label' => ts('Suffix')));
95 }
96 }
97
98 if (!$inlineEditMode || $inlineEditMode == 2) {
99 // nick_name
100 $form->addElement('text', 'nick_name', ts('Nickname'),
101 CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Contact', 'nick_name')
102 );
103
104 // job title
105 // override the size for UI to look better
106 $attributes['job_title']['size'] = 30;
107 $form->addElement('text', 'job_title', ts('Job Title'), $attributes['job_title'], 'size="30"');
108
109 //Current Employer Element
110 $props = array(
111 'api' => array('params' => array('contact_type' => 'Organization')),
112 'create' => TRUE,
113 );
114 $form->addEntityRef('employer_id', ts('Current Employer'), $props);
115 $attributes['source']['class'] = 'big';
116 $form->addElement('text', 'contact_source', ts('Source'), CRM_Utils_Array::value('source', $attributes));
117 }
118
119 if (!$inlineEditMode) {
120 $checkSimilar = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
121 'contact_ajax_check_similar',
122 NULL,
123 TRUE
124 );
125
126 if ($checkSimilar == NULL) {
127 $checkSimilar = 0;
128 }
129 $form->assign('checkSimilar', $checkSimilar);
130
131 //External Identifier Element
132 $form->add('text', 'external_identifier', ts('External ID'),
133 CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Contact', 'external_identifier'), FALSE
134 );
135
136 $form->addRule('external_identifier',
137 ts('External ID already exists in Database.'),
138 'objectExists',
139 array('CRM_Contact_DAO_Contact', $form->_contactId, 'external_identifier')
140 );
141 $config = CRM_Core_Config::singleton();
142 CRM_Core_ShowHideBlocks::links($form, 'demographics', '', '');
143 }
144 }
145
146 /**
147 * Global form rule
148 *
149 * @param array $fields
150 * The input form values.
151 * @param array $files
152 * The uploaded files if any.
153 * @param int $contactID
154 *
155 * @return true if no errors, else array of errors
156 * @static
157 */
158 public static function formRule($fields, $files, $contactID = NULL) {
159 $errors = array();
160 $primaryID = CRM_Contact_Form_Contact::formRule($fields, $errors, $contactID);
161
162 // make sure that firstName and lastName or a primary OpenID is set
163 if (!$primaryID && (empty($fields['first_name']) || empty($fields['last_name']))) {
164 $errors['_qf_default'] = ts('First Name and Last Name OR an email OR an OpenID in the Primary Location should be set.');
165 }
166
167 //check for duplicate - dedupe rules
168 CRM_Contact_Form_Contact::checkDuplicateContacts($fields, $errors, $contactID, 'Individual');
169
170 return empty($errors) ? TRUE : $errors;
171 }
172 }