1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35:
36:
37:
38: class WebFormAction extends CAction {
39:
40: public static function sanitizeGetParams () {
41:
42: $whitelist = array(
43: 'fg', 'bgc', 'font', 'bs', 'bc', 'iframeHeight'
44: );
45: $_GET = array_intersect_key($_GET, array_flip($whitelist));
46:
47: $_GET = preg_replace('/[^a-zA-Z0-9#,.]/', '', $_GET);
48: return $_GET;
49: }
50:
51: private static function addTags ($model) {
52:
53: if(!empty($_POST['tags'])){
54: $taglist = explode(',', $_POST['tags']);
55: if($taglist !== false){
56: foreach($taglist as &$tag){
57: if($tag === '')
58: continue;
59: if(substr($tag, 0, 1) != '#')
60: $tag = '#'.$tag;
61: $tagModel = new Tags;
62: $tagModel->taggedBy = 'API';
63: $tagModel->timestamp = time();
64: $tagModel->type = get_class ($model);
65: $tagModel->itemId = $model->id;
66: $tagModel->tag = $tag;
67: $tagModel->itemName = $model->name;
68: $tagModel->save();
69:
70: X2Flow::trigger('RecordTagAddTrigger', array(
71: 'model' => $model,
72: 'tags' => $tag,
73: ));
74: }
75: }
76: }
77: }
78:
79:
80:
81:
82:
83: private function handleWebleadFormSubmission (X2Model $model, $extractedParams) {
84: $newRecord = $model->isNewRecord;
85: if(isset($_POST['Contacts'])) {
86:
87: $model->createEvent = false;
88: $model->setX2Fields($_POST['Contacts'], true);
89:
90: $p = Fields::getPurifier();
91: foreach($model->attributes as $name=>$value) {
92: if($name != $model->primaryKey() && !empty($value)) {
93: $model->$name = $p->purify($value);
94: }
95: }
96: $now = time();
97:
98:
99: $model->visibility = 1;
100:
101: $model->validate (null, false);
102: if(!$model->hasErrors()){
103: $model->lastUpdated = $now;
104: $model->updatedBy = 'admin';
105:
106:
107:
108: if($model->asa('X2DuplicateBehavior') && $model->checkForDuplicates()){
109: $duplicates = $model->getDuplicates();
110: $oldest = $duplicates[0];
111: $fields = $model->getFields(true);
112: foreach ($fields as $field) {
113: if (!in_array($field->fieldName,
114: $model->X2MergeableBehavior->restrictedFields)
115: && !is_null($model->{$field->fieldName})) {
116: if ($field->type === 'text' && !empty($oldest->{$field->fieldName})) {
117: $oldest->{$field->fieldName} .= "\n--\n" . $model->{$field->fieldName};
118: } else {
119: $oldest->{$field->fieldName} = $model->{$field->fieldName};
120: }
121: }
122: }
123: $model = $oldest;
124: $newRecord = $model->isNewRecord;
125: }
126: if($newRecord){
127: $model->createDate = $now;
128: $model->assignedTo = $this->controller->getNextAssignee();
129: }
130:
131: $success = $model->save();
132:
133:
134:
135:
136:
137: if($success){
138: if ($extractedParams['generateLead'])
139: self::generateLead ($model, $extractedParams['leadSource']);
140: if ($extractedParams['generateAccount'])
141: self::generateAccount ($model);
142:
143: self::addTags ($model);
144: $tags = ((!isset($_POST['tags']) || empty($_POST['tags'])) ?
145: array() : explode(',',$_POST['tags']));
146: if($newRecord) {
147: X2Flow::trigger(
148: 'WebleadTrigger', array('model' => $model, 'tags' => $tags));
149: }
150:
151:
152: Actions::associateAction ($model, array (
153: 'actionDescription' =>
154: Yii::t('contacts', 'Web Lead')
155: ."\n\n".Yii::t('contacts', 'Name').': '.
156: CHtml::decode($model->firstName)." ".
157: CHtml::decode($model->lastName)."\n".Yii::t('contacts', 'Email').
158: ": ".CHtml::decode($model->email)."\n".Yii::t('contacts', 'Phone').
159: ": ".CHtml::decode($model->phone)."\n".
160: Yii::t('contacts', 'Background Info').": ".
161: CHtml::decode($model->backgroundInfo),
162: 'type' => 'note',
163: ));
164:
165:
166: $event = new Events;
167: $event->associationType = 'Contacts';
168: $event->associationId = $model->id;
169: $event->user = $model->assignedTo;
170: $event->type = 'weblead_create';
171: $event->save();
172:
173:
174:
175: if($model->assignedTo != 'Anyone' && $model->assignedTo != '') {
176:
177: $notif = new Notification;
178: $notif->user = $model->assignedTo;
179: $notif->createdBy = 'API';
180: $notif->createDate = time();
181: $notif->type = 'weblead';
182: $notif->modelType = 'Contacts';
183: $notif->modelId = $model->id;
184: $notif->save();
185:
186: $profile = Profile::model()->findByAttributes(
187: array('username' => $model->assignedTo));
188:
189: 190:
191: if($profile !== null && !empty($profile->emailAddress)){
192:
193:
194: $subject = Yii::t('marketing', 'New Web Lead');
195: $message =
196: Yii::t('marketing',
197: 'A new web lead has been assigned to you: ').
198: CHtml::link(
199: $model->firstName.' '.$model->lastName,
200: array('/contacts/contacts/view', 'id' => $model->id)).'.';
201: $address = array('to' => array(array('', $profile->emailAddress)));
202: $emailFrom = Credentials::model()->getDefaultUserAccount(
203: Credentials::$sysUseId['systemNotificationEmail'], 'email');
204: if($emailFrom == Credentials::LEGACY_ID)
205: $emailFrom = array(
206: 'name' => $profile->fullName,
207: 'address' => $profile->emailAddress
208: );
209:
210: $status = $this->controller->sendUserEmail(
211: $address, $subject, $message, null, $emailFrom);
212:
213: }
214:
215: }
216:
217:
218: } else {
219: $errMsg = 'Error: WebListenerAction.php: model failed to save';
220: AuxLib::debugLog ($errMsg);
221: Yii::log ($errMsg, '', 'application.debug');
222: }
223:
224: $this->controller->renderPartial('application.components.views.webFormSubmit',
225: array (
226: 'type' => 'weblead',
227: 'redirectUrl' => $extractedParams['redirectUrl']
228: )
229: );
230:
231: Yii::app()->end();
232: }
233: }
234:
235: $sanitizedGetParams = self::sanitizeGetParams ();
236:
237:
238: $this->controller->renderPartial(
239: 'application.components.views.webForm',
240: array_merge (array(
241: 'type' => 'weblead'
242: ), $sanitizedGetParams));
243:
244:
245: }
246:
247:
248: private function handleServiceFormSubmission ($model, $extractedParams) {
249: if(isset($_POST['Services'])){
250: if(isset($_POST['Services']['firstName'])){
251: $firstName = $_POST['Services']['firstName'];
252: $fullName = $firstName;
253: }
254:
255: if(isset($_POST['Services']['lastName'])){
256: $lastName = $_POST['Services']['lastName'];
257: if(isset($fullName)){
258: $fullName .= ' '.$lastName;
259: }else{
260: $fullName = $lastName;
261: }
262: }
263:
264: if(isset($_POST['Services']['email'])){
265: $email = $_POST['Services']['email'];
266: }
267: if(isset($_POST['Services']['phone'])){
268: $phone = $_POST['Services']['phone'];
269: }
270: if(isset($_POST['Services']['desription'])){
271: $description = $_POST['Services']['description'];
272: }
273:
274:
275:
276:
277: $p = Fields::getPurifier();
278: foreach($model->attributes as $name=>$value) {
279: if($name != $model->primaryKey() && !empty($value)) {
280: $model->$name = $p->purify($value);
281: }
282: }
283:
284: if(isset($email) && $email) {
285: $contact = Contacts::model()->findByAttributes(array('email' => $email));
286: } else {
287: $contact = false;
288: }
289:
290: if($contact){
291: $model->contactId = $contact->nameId;
292: }else{
293: $model->contactId = "Unregistered";
294: }
295:
296: if(isset($fullName) || isset($email)){
297: $model->subject = Yii::t('services', 'Web Form Case entered by {name}', array(
298: '{name}' => isset($fullName) ? $fullName : $email,
299: ));
300: }else{
301: $model->subject = Yii::t('services', 'Web Form Case');
302: }
303:
304: $model->origin = 'Web';
305: if(!isset($model->impact) || $model->impact == '')
306: $model->impact = Yii::t('services', '3 - Moderate');
307: if(!isset($model->status) || $model->status == '')
308: $model->status = Yii::t('services', 'New');
309: if(!isset($model->mainIssue) || $model->mainIssue == '')
310: $model->mainIssue = Yii::t('services', 'General Request');
311: if(!isset($model->subIssue) || $model->subIssue == '')
312: $model->subIssue = Yii::t('services', 'Other');
313: $model->assignedTo = $this->controller->getNextAssignee();
314: if (isset($email))
315: $model->email = CHtml::encode($email);
316: $now = time();
317: $model->createDate = $now;
318: $model->lastUpdated = $now;
319: $model->updatedBy = 'admin';
320: if (isset ($description))
321: $model->description = CHtml::encode($description);
322:
323:
324: $model->validate (null, false);
325:
326: if(!$model->hasErrors()){
327:
328: if($model->save()){
329: $model->name = $model->id;
330: $model->update(array('name'));
331:
332: self::addTags ($model);
333:
334:
335: $action = new Actions;
336: $action->actionDescription = Yii::t('contacts', 'Web Form')."\n\n".
337: (isset($fullName) ? (Yii::t('contacts', 'Name').': '.$fullName."\n") : '').
338: (isset($email) ? (Yii::t('contacts', 'Email').": ".$email."\n") : '').
339: (isset($phone) ? (Yii::t('contacts', 'Phone').": ".$phone."\n") : '').
340: (isset($description) ?
341: (Yii::t('services', 'Description').": ".$description) : '');
342:
343:
344: $action->type = 'note';
345: $action->assignedTo = $model->assignedTo;
346: $action->visibility = '1';
347: $action->associationType = 'services';
348: $action->associationId = $model->id;
349: $action->associationName = $model->name;
350: $action->createDate = $now;
351: $action->lastUpdated = $now;
352: $action->completeDate = $now;
353: $action->complete = 'Yes';
354: $action->updatedBy = 'admin';
355: $action->save();
356:
357: if(isset($email)){
358:
359:
360: $emailBody = Yii::t('services', 'Hello').' '.$fullName.",<br><br>";
361: $emailBody .= Yii::t('services',
362: 'Thank you for contacting our Technical Support '.
363: 'team. This is to verify we have received your request for Case# '.
364: '{casenumber}. One of our Technical Analysts will contact you shortly.',
365: array('{casenumber}' => $model->id));
366:
367: $emailBody = Yii::app()->settings->serviceCaseEmailMessage;
368: if(isset($firstName))
369: $emailBody = preg_replace('/{first}/u', $firstName, $emailBody);
370: if(isset($lastName))
371: $emailBody = preg_replace('/{last}/u', $lastName, $emailBody);
372: if(isset($phone))
373: $emailBody = preg_replace('/{phone}/u', $phone, $emailBody);
374: if(isset($email))
375: $emailBody = preg_replace('/{email}/u', $email, $emailBody);
376: if(isset($description))
377: $emailBody = preg_replace('/{description}/u', $description, $emailBody);
378: $emailBody = preg_replace('/{case}/u', $model->id, $emailBody);
379: $emailBody = preg_replace('/\n|\r\n/', "<br>", $emailBody);
380:
381: $uniqueId = md5(uniqid(rand(), true));
382: $emailBody .= '<img src="'.$this->controller->createAbsoluteUrl(
383: '/actions/actions/emailOpened', array('uid' => $uniqueId, 'type' => 'open')).'"/>';
384:
385: $emailSubject = Yii::app()->settings->serviceCaseEmailSubject;
386: if(isset($firstName))
387: $emailSubject = preg_replace('/{first}/u', $firstName, $emailSubject);
388: if(isset($lastName))
389: $emailSubject = preg_replace('/{last}/u', $lastName, $emailSubject);
390: if(isset($phone))
391: $emailSubject = preg_replace('/{phone}/u', $phone, $emailSubject);
392: if(isset($email))
393: $emailSubject = preg_replace('/{email}/u', $email, $emailSubject);
394: if(isset($description))
395: $emailSubject = preg_replace('/{description}/u', $description,
396: $emailSubject);
397: $emailSubject = preg_replace('/{case}/u', $model->id, $emailSubject);
398: if(Yii::app()->settings->serviceCaseEmailAccount !=
399: Credentials::LEGACY_ID) {
400: $from = (int) Yii::app()->settings->serviceCaseEmailAccount;
401: } else {
402: $from = array(
403: 'name' => Yii::app()->settings->serviceCaseFromEmailName,
404: 'address' => Yii::app()->settings->serviceCaseFromEmailAddress
405: );
406: }
407: $useremail = array('to' => array(array(isset($fullName) ?
408: $fullName : '', $email)));
409:
410: $status = $this->controller->sendUserEmail(
411: $useremail, $emailSubject, $emailBody, null, $from);
412:
413: if($status['code'] == 200){
414: if($model->assignedTo != 'Anyone'){
415: $profile = X2Model::model('Profile')->findByAttributes(
416: array('username' => $model->assignedTo));
417: if(isset($profile)){
418: $useremail['to'] = array(
419: array(
420: $profile->fullName,
421: $profile->emailAddress,
422: ),
423: );
424: $emailSubject = 'Service Case Created';
425: $emailBody = "A new service case, #".$model->id.
426: ", has been created in X2Engine. To view the case, click ".
427: "this link: ".$model->getLink();
428: $status = $this->controller->sendUserEmail(
429: $useremail, $emailSubject, $emailBody, null, $from);
430: }
431: }
432:
433: $action = new Actions;
434: $action->associationType = 'services';
435: $action->associationId = $model->id;
436: $action->associationName = $model->name;
437: $action->visibility = 1;
438: $action->complete = 'Yes';
439: $action->type = 'email';
440: $action->completedBy = 'admin';
441: $action->assignedTo = $model->assignedTo;
442: $action->createDate = time();
443: $action->dueDate = time();
444: $action->completeDate = time();
445: $action->actionDescription = '<b>'.$model->subject."</b>\n\n".
446: $emailBody;
447: if($action->save()){
448: $track = new TrackEmail;
449: $track->actionId = $action->id;
450: $track->uniqueId = $uniqueId;
451: $track->save();
452: }
453: } else {
454: $errMsg = 'Error: actionWebForm.php: sendUserEmail failed';
455: AuxLib::debugLog ($errMsg);
456: Yii::log ($errMsg, '', 'application.debug');
457: }
458: }
459: $this->controller->renderPartial('application.components.views.webFormSubmit',
460: array('type' => 'service', 'caseNumber' => $model->id));
461:
462: Yii::app()->end();
463: }
464: }
465: }
466:
467: $sanitizedGetParams = self::sanitizeGetParams ();
468:
469:
470: $this->controller->renderPartial (
471: 'application.components.views.webForm',
472: array_merge (array(
473: 'model' => $model, 'type' => 'service'
474: ), $sanitizedGetParams));
475:
476: }
477:
478:
479:
480: 481: 482: 483: 484: 485: 486: 487: 488: 489: 490: 491: 492: 493: 494: 495:
496: public function run(){
497: $modelClass = $this->controller->modelClass;
498: if ($modelClass === 'Campaign') $modelClass = 'Contacts';
499:
500: if ($modelClass === 'Contacts')
501: $model = new Contacts ('webForm');
502: elseif ($modelClass === 'Services')
503: $model = new Services ('webForm');
504:
505: $extractedParams = array ();
506:
507: if (isset ($_GET['webFormId'])) {
508: $webForm = WebForm::model()->findByPk($_GET['webFormId']);
509: }
510: $extractedParams['leadSource'] = null;
511: $extractedParams['generateLead'] = false;
512: $extractedParams['generateAccount'] = false;
513: $extractedParams['redirectUrl'] = null;
514: if (isset ($webForm)) {
515: if (!empty ($webForm->leadSource))
516: $extractedParams['leadSource'] = $webForm->leadSource;
517: if (!empty ($webForm->generateLead))
518: $extractedParams['generateLead'] = $webForm->generateLead;
519: if (!empty ($webForm->generateAccount))
520: $extractedParams['generateAccount'] = $webForm->generateAccount;
521: if (!empty ($webForm->redirectUrl))
522: $extractedParams['redirectUrl'] = $webForm->redirectUrl;
523: }
524:
525:
526:
527: if ($modelClass === 'Contacts') {
528: $this->handleWebleadFormSubmission ($model, $extractedParams);
529: } else if ($modelClass === 'Services') {
530: $this->handleServiceFormSubmission ($model, $extractedParams);
531: }
532:
533: }
534:
535: 536: 537: 538: 539:
540: private static function generateLead (Contacts $contact, $leadSource=null) {
541: $lead = new X2Leads ('webForm');
542: $lead->firstName = $contact->firstName;
543: $lead->lastName = $contact->lastName;
544: $lead->leadSource = $leadSource;
545:
546: if ($lead->save (false)) {
547: $lead->createRelationship($contact);
548: }
549:
550: }
551:
552: 553: 554:
555: private static function generateAccount (Contacts $contact) {
556: if (isset ($contact->company)) {
557: $account = new Accounts ();
558: $account->name = $contact->company;
559: if ($account->save ()) {
560: $account->refresh ();
561: $contact->company = $account->nameId;
562: $contact->update ();
563: }
564: }
565: }
566:
567: }
568:
569: ?>
570: