Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
6 | | This work is published under the GNU AGPLv3 license with some | |
7 | | permitted exceptions and without any warranty. For full license | | |
8 | | and copyright information, see https://civicrm.org/licensing | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
16 | */ |
17 | ||
578e4db3 EM |
18 | use Civi\Api4\Group; |
19 | use Civi\Api4\Tag; | |
20 | ||
6a488035 | 21 | /** |
f12c6f7d | 22 | * This class previews the uploaded file and returns summary statistics. |
6a488035 | 23 | */ |
f532671f | 24 | class CRM_Contact_Import_Form_Preview extends CRM_Import_Form_Preview { |
6a488035 | 25 | |
6a488035 | 26 | /** |
fe482240 | 27 | * Build the form object. |
6a488035 TO |
28 | */ |
29 | public function buildQuickForm() { | |
c7162176 | 30 | $this->addElement('text', 'newGroupName', ts('Name for new group'), CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Group', 'title')); |
6a488035 | 31 | $this->addElement('text', 'newGroupDesc', ts('Description of new group')); |
5a552b87 SL |
32 | $groupTypes = CRM_Core_OptionGroup::values('group_type', TRUE); |
33 | if (!empty($groupTypes)) { | |
34 | $this->addCheckBox('newGroupType', | |
35 | ts('Group Type'), | |
36 | $groupTypes, | |
37 | NULL, NULL, NULL, NULL, ' ' | |
38 | ); | |
39 | } | |
6a488035 | 40 | |
578e4db3 | 41 | $groups = CRM_Core_PseudoConstant::nestedGroup();; |
6a488035 TO |
42 | |
43 | if (!empty($groups)) { | |
578e4db3 | 44 | $this->addElement('select', 'groups', ts('Add imported records to existing group(s)'), $groups, [ |
69078420 SL |
45 | 'multiple' => "multiple", |
46 | 'class' => 'crm-select2', | |
578e4db3 | 47 | ]); |
6a488035 TO |
48 | } |
49 | ||
50 | //display new tag | |
c7162176 | 51 | $this->addElement('text', 'newTagName', ts('Tag'), CRM_Core_DAO::getAttribute('CRM_Core_DAO_Tag', 'name')); |
52 | $this->addElement('text', 'newTagDesc', ts('Description'), CRM_Core_DAO::getAttribute('CRM_Core_DAO_Tag', 'description')); | |
6a488035 | 53 | |
578e4db3 | 54 | $tag = CRM_Core_PseudoConstant::get('CRM_Core_DAO_EntityTag', 'tag_id', ['onlyActive' => FALSE]); |
6a488035 | 55 | if (!empty($tag)) { |
578e4db3 EM |
56 | $this->addElement('select', 'tag', ts(' Tag imported records'), $tag, [ |
57 | 'multiple' => 'multiple', | |
58 | 'class' => 'crm-select2', | |
59 | ]); | |
6a488035 TO |
60 | } |
61 | ||
719a6fec | 62 | $this->addFormRule(array('CRM_Contact_Import_Form_Preview', 'formRule'), $this); |
4c7e1926 CW |
63 | |
64 | parent::buildQuickForm(); | |
6a488035 TO |
65 | } |
66 | ||
67 | /** | |
fe482240 | 68 | * Global validation rules for the form. |
6a488035 | 69 | * |
77c5b619 TO |
70 | * @param array $fields |
71 | * Posted values of the form. | |
6a488035 | 72 | * |
da6b46f4 | 73 | * @param $files |
e8cf95b4 | 74 | * @param self $self |
da6b46f4 | 75 | * |
3592a5e4 | 76 | * @return array|bool |
a6c01b45 | 77 | * list of errors to be posted back to the form |
6a488035 | 78 | */ |
00be9182 | 79 | public static function formRule($fields, $files, $self) { |
affcc9d2 | 80 | $errors = []; |
6a488035 TO |
81 | $invalidTagName = $invalidGroupName = FALSE; |
82 | ||
a7488080 | 83 | if (!empty($fields['newTagName'])) { |
6a488035 | 84 | if (!CRM_Utils_Rule::objectExists(trim($fields['newTagName']), |
353ffa53 TO |
85 | array('CRM_Core_DAO_Tag') |
86 | ) | |
87 | ) { | |
6a488035 TO |
88 | $errors['newTagName'] = ts('Tag \'%1\' already exists.', |
89 | array(1 => $fields['newTagName']) | |
90 | ); | |
91 | $invalidTagName = TRUE; | |
92 | } | |
93 | } | |
94 | ||
a7488080 | 95 | if (!empty($fields['newGroupName'])) { |
353ffa53 TO |
96 | $title = trim($fields['newGroupName']); |
97 | $name = CRM_Utils_String::titleToVar($title); | |
98 | $query = 'select count(*) from civicrm_group where name like %1 OR title like %2'; | |
0e6e8724 DL |
99 | $grpCnt = CRM_Core_DAO::singleValueQuery( |
100 | $query, | |
101 | array( | |
102 | 1 => array($name, 'String'), | |
6a488035 | 103 | 2 => array($title, 'String'), |
0e6e8724 DL |
104 | ) |
105 | ); | |
6a488035 TO |
106 | if ($grpCnt) { |
107 | $invalidGroupName = TRUE; | |
108 | $errors['newGroupName'] = ts('Group \'%1\' already exists.', array(1 => $fields['newGroupName'])); | |
109 | } | |
110 | } | |
111 | ||
112 | $self->assign('invalidTagName', $invalidTagName); | |
113 | $self->assign('invalidGroupName', $invalidGroupName); | |
114 | ||
115 | return empty($errors) ? TRUE : $errors; | |
116 | } | |
117 | ||
6a488035 | 118 | /** |
f12c6f7d | 119 | * Process the mapped fields and map it into the uploaded file. |
52bd01f5 | 120 | * |
578e4db3 | 121 | * @throws \API_Exception|\CRM_Core_Exception |
6a488035 | 122 | */ |
578e4db3 EM |
123 | public function postProcess(): void { |
124 | $groupsToAddTo = (array) $this->getSubmittedValue('groups'); | |
125 | $summaryInfo = ['groups' => [], 'tags' => []]; | |
126 | foreach ($groupsToAddTo as $groupID) { | |
127 | // This is a convenience for now - really url & name should be determined at | |
128 | // presentation stage - ie the summary screen. The only info we are really | |
129 | // preserving is which groups were created vs already existed. | |
130 | $summaryInfo['groups'][$groupID] = [ | |
131 | 'url' => CRM_Utils_System::url('civicrm/group/search', 'reset=1&force=1&context=smog&gid=' . $groupID), | |
132 | 'name' => Group::get(FALSE)->addWhere('id', '=', $groupID)->addSelect('name')->execute()->first()['name'], | |
133 | 'new' => FALSE, | |
134 | 'added' => 0, | |
135 | 'notAdded' => 0, | |
136 | ]; | |
137 | } | |
6a488035 | 138 | |
578e4db3 EM |
139 | if ($this->getSubmittedValue('newGroupName')) { |
140 | /* Create a new group */ | |
141 | $groupsToAddTo[] = $groupID = Group::create(FALSE)->setValues([ | |
142 | 'title' => $this->getSubmittedValue('newGroupName'), | |
143 | 'description' => $this->getSubmittedValue('newGroupDesc'), | |
144 | 'group_type' => $this->getSubmittedValue('newGroupType') ?? [], | |
145 | 'is_active' => TRUE, | |
146 | ])->execute()->first()['id']; | |
147 | $summaryInfo['groups'][$groupID] = [ | |
148 | 'url' => CRM_Utils_System::url('civicrm/group/search', 'reset=1&force=1&context=smog&gid=' . $groupID), | |
149 | 'name' => $this->getSubmittedValue('newGroupName'), | |
150 | 'new' => TRUE, | |
151 | 'added' => 0, | |
152 | 'notAdded' => 0, | |
153 | ]; | |
154 | } | |
155 | $tagsToAdd = (array) $this->getSubmittedValue('tag'); | |
156 | foreach ($tagsToAdd as $tagID) { | |
157 | // This is a convenience for now - really url & name should be determined at | |
158 | // presentation stage - ie the summary screen. The only info we are really | |
159 | // preserving is which tags were created vs already existed. | |
160 | $summaryInfo['tags'][$tagID] = [ | |
161 | 'url' => CRM_Utils_System::url('civicrm/contact/search', 'reset=1&force=1&context=smog&id=' . $tagID), | |
162 | 'name' => Tag::get(FALSE)->addWhere('id', '=', $tagID)->addSelect('name')->execute()->first()['name'], | |
163 | 'new' => TRUE, | |
164 | 'added' => 0, | |
165 | 'notAdded' => 0, | |
166 | ]; | |
167 | } | |
168 | if ($this->getSubmittedValue('newTagName')) { | |
169 | $tagsToAdd[] = $tagID = Tag::create(FALSE)->setValues([ | |
170 | 'name' => $this->getSubmittedValue('newTagName'), | |
171 | 'description' => $this->getSubmittedValue('newTagDesc'), | |
172 | 'is_selectable' => TRUE, | |
173 | 'used_for' => 'civicrm_contact', | |
174 | ])->execute()->first()['id']; | |
175 | $summaryInfo['tags'][$tagID] = [ | |
176 | 'url' => CRM_Utils_System::url('civicrm/contact/search', 'reset=1&force=1&context=smog&id=' . $tagID), | |
177 | 'name' => $this->getSubmittedValue('newTagName'), | |
178 | 'new' => FALSE, | |
179 | 'added' => 0, | |
180 | 'notAdded' => 0, | |
181 | ]; | |
182 | } | |
183 | // Store the actions to take on each row & the data to present at the end to the userJob. | |
184 | $this->updateUserJobMetadata('post_actions', ['group' => $groupsToAddTo, 'tag' => $tagsToAdd]); | |
185 | $this->updateUserJobMetadata('summary_info', $summaryInfo); | |
6a488035 TO |
186 | |
187 | // If ACL applies to the current user, update cache before running the import. | |
188 | if (!CRM_Core_Permission::check('view all contacts')) { | |
a92e7856 SL |
189 | $userID = CRM_Core_Session::getLoggedInContactID(); |
190 | CRM_ACL_BAO_Cache::deleteEntry($userID); | |
191 | CRM_ACL_BAO_Cache::deleteContactCacheEntry($userID); | |
6a488035 TO |
192 | } |
193 | ||
3592a5e4 | 194 | CRM_Utils_Address_USPS::disable($this->getSubmittedValue('disableUSPS')); |
4ff86743 | 195 | $this->runTheImport(); |
6a488035 TO |
196 | } |
197 | ||
73edfc10 EM |
198 | /** |
199 | * @return \CRM_Contact_Import_Parser_Contact | |
200 | */ | |
201 | protected function getParser(): CRM_Contact_Import_Parser_Contact { | |
d00da884 EM |
202 | if (!$this->parser) { |
203 | $this->parser = new CRM_Contact_Import_Parser_Contact(); | |
204 | $this->parser->setUserJobID($this->getUserJobID()); | |
205 | } | |
206 | return $this->parser; | |
73edfc10 EM |
207 | } |
208 | ||
6a488035 | 209 | } |