Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- App::uses('AppController', 'Controller');
- App::uses('CakeTime', 'Utility');
- class SubmissionsController extends AppController {
- public $name = 'Submissions';
- public $uses = array('Submission', 'SubmissionValue', 'Submit', 'Contact', 'ContactLink', 'ActivityNote', 'Delete');
- public $components = array('Paginator');
- public $helpers = array('Html');
- function index() {
- $this->Submission->recursive = 1;
- $this->set('submissions', $this->Paginator->paginate('Submission'));
- $this->set('nav_bar', 'ADMIN');
- }
- function delete() {
- $this->Paginator->settings = array('limit' => 250);
- $this->set('submits', $this->Paginator->paginate('Submit'));
- }
- function index2() {
- $this->Paginator->settings = array('limit' => 250);
- $this->set('submits', $this->Paginator->paginate('Submit'));
- }
- function import2($id) {
- App::uses('CakeTime', 'Utility');
- if (!$this->Submit->exists($id)) {
- throw new NotFoundException(__('Ugyldig henvendelse'));
- }
- $this->Submit->id = $id;
- $time = $this->Submit->field('submit_time');
- $submits = $this->Submit->findAllBySubmitTime($time);
- $fields = array();
- foreach ($submits as $submit) {
- $fields[$submit['Submit']['field_name']] = $submit['Submit']['field_value'];
- }
- if (!isset($fields['message'])) {
- $fields['message'] = 'Katalogbestilling';
- }
- if ($this->request->is(array('post', 'put'))) {
- $dataSource = $this->Contact->getDataSource();
- $dataSource->begin(); // start transaction
- if (!isset($this->request->data['double_id'])) { // New contact;
- $companyType = (isset($this->request->data['Contact']['contact_type_id']))?
- $this->request->data['Contact']['contact_type_id'] : '3';
- $companyName = (isset($this->request->data['Contact']['company_name']))?
- TRIM($this->request->data['Contact']['company_name']) : '';
- $this->request->data['Contact']['company_name'] = '';
- $this->request->data['Contact']['creator_id'] = $this->request->data['Contact']['user_id'] = 0;
- $this->request->data['Contact']['contact_type_id'] = '3';
- $this->request->data['Contact']['address2'] = '';
- $this->request->data['Contact']['status_id'] = ($companyName == '')?
- ($fields['message'] == 'Katalogbestilling')? 10 : 11
- : 24;
- $this->request->data['Contact']['message'] = $fields['message'];
- $this->Contact->create();
- $success = $this->Contact->save($this->request->data);
- $contactId = $this->Contact->id;
- if ($success && $companyName != '') {
- $this->request->data['Contact']['first_name'] =
- $this->request->data['Contact']['last_name'] = $this->request->data['Contact']['middle_names'] = '';
- $this->request->data['Contact']['company_name'] = $companyName;
- $this->request->data['Contact']['contact_type_id'] = $companyType;
- $this->request->data['Contact']['status_id'] = ($fields['message'] == 'Katalogbestilling')? 10 : 11;
- $this->Contact->create();
- $success = $this->Contact->save($this->request->data);
- if ($success) {
- $companyId = $this->Contact->id;
- $link['ContactLink']['senior_link'] = $companyId;
- $link['ContactLink']['junior_link'] = $contactId;
- $link['ContactLink']['title_id'] = 19;
- $this->ContactLink->create();
- $success = $this->ContactLink->save($link);
- }
- }
- } else { // double
- $contactId = $this->request->data['double_id'];
- $success = true;
- }
- if ($success) {
- $note = array();
- $fieldNames = array('name' => __('Navn'), 'company' => __('Firma'), 'address' => __('Adresse'),
- 'zip' => __('Postnr.'), 'city' => __('By'), 'email' => __('E-mail'), 'phone' => __('Tlf.'),
- 'message' => __('Besked'));
- $note['ActivityNote']['text'] = "Henvendelse fra hjemmesiden. ";
- foreach ($fieldNames as $key => $fieldName) {
- if ($fields[$key] == "") continue;
- $note['ActivityNote']['text'] .= $fieldName.": ".$fields[$key]."; ";
- }
- $note['ActivityNote']['contact_id'] = $contactId;
- $note['ActivityNote']['activity_date'] = CakeTime::format('Y-m-d', round($time));
- $note['ActivityNote']['user_id'] = 0;
- $note['ActivityNote']['activity_type_id'] = '3';
- $this->ActivityNote->create();
- $succes = $this->ActivityNote->save($note);
- if ($success && isset($companyId)) {
- $note['ActivityNote']['contact_id'] = $companyId;
- $this->ActivityNote->create();
- $succes = $this->ActivityNote->save($note);
- }
- }
- if ($success) {
- foreach ($submits as $key => $value) {
- $submits[$key]['Submit']['imported'] = '1';
- }
- $this->Submit->saveAll($submits);
- $dataSource->commit();
- $this->Session->setFlash(__('Henvendelsen er importeret.'), 'default', array('class' => 'success'));
- $contId = (isset($companyId))? $companyId : $contactId;
- $this->redirect(array('controller' => 'Contacts', 'action' => 'view/'.$contId));
- } else {
- $dataSource->rollback();
- $this->Session->setFlash(__('Henvendelsen kunne ikke importeres. Prøv igen.'));
- }
- return $this->redirect(array('action' => 'index'));
- } else {
- $names = TRIM($fields['name']);
- $names = PREG_REPLACE( '/\s+/', ' ', $names);
- $names = explode(' ', $names);
- $nameCount = count($names);
- $contact = array();
- $contact['Contact']['first_name'] = $names[0];
- $contact['Contact']['last_name'] = ($nameCount > 1)? $names[$nameCount-1] : '';
- $contact['Contact']['middle_names'] = '';
- if (count($names) > 2) {
- unset($names[$nameCount-1]);
- unset($names[0]);
- $contact['Contact']['middle_names'] = implode($names);
- }
- $fieldNames = array('address1' => 'address', 'zip' => 'zip', 'city' => 'city', 'email' => 'email');
- foreach ($fieldNames as $contactField => $submissionField) {
- $contact['Contact'][$contactField] = $fields[$submissionField];
- }
- $contact['Contact']['company_name'] = ($fields['company'] != 'Evt.firma')? $fields['company'] : '';
- $contact['Contact']['contact_type_id'] = 1;
- $phone = PREG_REPLACE( '/\s+/', '', $fields['phone']);
- $phone = PREG_REPLACE( '/\+/', '00', $phone);
- $phone = PREG_REPLACE( '/[^0-9]/', '', $phone);
- $contact['Contact']['phone_number'] = $phone;
- $this->request->data = $contact;
- $contactTypes = $this->Contact->ContactType->find('list', array(
- 'fields' => array('ContactType.id', 'ContactType.value'),
- 'conditions' => array('ContactType.id !=' => '3')
- )
- );
- if ($phone == '') $phone = '999';
- if ($fields['email'] == '') $fields['email'] = 'noMail';
- if ($fields['company'] == '') $fields['company'] = 'noCompany';
- $this->Contact->recursive = -1;
- $doubles = $this->Contact->find('all', array(
- 'conditions' => array(
- 'OR' => array(
- 'phone_number' => $phone, 'cell_number' => $phone, 'email' => $fields['email'],
- 'lower(company_name)' => strtolower($fields['company']),
- array(
- 'AND' => array(
- 'lower(first_name)' => strtolower($contact['Contact']['first_name']),
- 'lower(last_name)' => strtolower($contact['Contact']['last_name'])
- )
- )
- )
- )
- ));
- $this->set('doubles', $doubles);
- $this->set('contactTypes', $contactTypes);
- $this->set('message', $fields['message']);
- $this->set('id', $id);
- $this->set('nav_bar', 'ADMIN');
- }
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement