Advertisement
Guest User

Untitled

a guest
Apr 17th, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.28 KB | None | 0 0
  1. <?php
  2. App::uses('AppController', 'Controller');
  3.         App::uses('CakeTime', 'Utility');
  4.  
  5.  
  6. class SubmissionsController extends AppController {
  7.     public $name = 'Submissions';
  8.     public $uses = array('Submission', 'SubmissionValue', 'Submit', 'Contact', 'ContactLink', 'ActivityNote', 'Delete');
  9.    
  10.     public $components = array('Paginator');
  11.     public $helpers = array('Html');
  12.    
  13.     function index() {
  14.         $this->Submission->recursive = 1;
  15.         $this->set('submissions', $this->Paginator->paginate('Submission'));
  16.         $this->set('nav_bar', 'ADMIN');
  17.     }
  18.    
  19.     function delete() {
  20.         $this->Paginator->settings = array('limit' => 250);
  21.         $this->set('submits', $this->Paginator->paginate('Submit'));
  22.     }  
  23.     function index2() {
  24.         $this->Paginator->settings = array('limit' => 250);
  25.         $this->set('submits', $this->Paginator->paginate('Submit'));
  26.     }
  27.  
  28.    
  29.     function import2($id) {
  30.         App::uses('CakeTime', 'Utility');
  31.         if (!$this->Submit->exists($id)) {
  32.             throw new NotFoundException(__('Ugyldig henvendelse'));
  33.         }
  34.         $this->Submit->id = $id;
  35.         $time = $this->Submit->field('submit_time');
  36.         $submits = $this->Submit->findAllBySubmitTime($time);
  37.         $fields = array();
  38.         foreach ($submits as $submit) {
  39.             $fields[$submit['Submit']['field_name']] = $submit['Submit']['field_value'];
  40.         }
  41.         if (!isset($fields['message'])) {
  42.             $fields['message'] = 'Katalogbestilling';  
  43.         }
  44.        
  45.         if ($this->request->is(array('post', 'put'))) {
  46.             $dataSource = $this->Contact->getDataSource();
  47.             $dataSource->begin(); // start transaction
  48.             if (!isset($this->request->data['double_id'])) { // New contact;
  49.                 $companyType = (isset($this->request->data['Contact']['contact_type_id']))?
  50.                     $this->request->data['Contact']['contact_type_id'] : '3';
  51.                 $companyName = (isset($this->request->data['Contact']['company_name']))?
  52.                     TRIM($this->request->data['Contact']['company_name']) : '';
  53.                 $this->request->data['Contact']['company_name'] = '';
  54.                 $this->request->data['Contact']['creator_id'] = $this->request->data['Contact']['user_id'] = 0;
  55.                 $this->request->data['Contact']['contact_type_id'] = '3';
  56.                 $this->request->data['Contact']['address2'] = '';
  57.                 $this->request->data['Contact']['status_id'] = ($companyName == '')?
  58.                     ($fields['message'] == 'Katalogbestilling')? 10 : 11
  59.                     : 24;
  60.                 $this->request->data['Contact']['message'] = $fields['message'];
  61.                 $this->Contact->create();
  62.                 $success = $this->Contact->save($this->request->data);
  63.                 $contactId = $this->Contact->id;
  64.                 if ($success && $companyName != '') {
  65.                     $this->request->data['Contact']['first_name'] =
  66.                         $this->request->data['Contact']['last_name'] = $this->request->data['Contact']['middle_names'] = '';
  67.                     $this->request->data['Contact']['company_name'] = $companyName;
  68.                     $this->request->data['Contact']['contact_type_id'] = $companyType;
  69.                     $this->request->data['Contact']['status_id'] = ($fields['message'] == 'Katalogbestilling')? 10 : 11;
  70.                     $this->Contact->create();
  71.                     $success = $this->Contact->save($this->request->data);
  72.                     if ($success) {
  73.                         $companyId = $this->Contact->id;
  74.                         $link['ContactLink']['senior_link'] = $companyId;
  75.                         $link['ContactLink']['junior_link'] = $contactId;
  76.                         $link['ContactLink']['title_id'] = 19;
  77.                         $this->ContactLink->create();
  78.                         $success = $this->ContactLink->save($link);
  79.                     }
  80.                 }
  81.             } else { // double
  82.                 $contactId = $this->request->data['double_id'];
  83.                 $success = true;
  84.             }
  85.             if ($success) {
  86.                 $note = array();
  87.                 $fieldNames = array('name' => __('Navn'), 'company' => __('Firma'), 'address' => __('Adresse'),
  88.                     'zip' => __('Postnr.'), 'city' => __('By'), 'email' => __('E-mail'), 'phone' => __('Tlf.'),
  89.                     'message' => __('Besked'));
  90.                 $note['ActivityNote']['text'] = "Henvendelse fra hjemmesiden. ";
  91.                 foreach ($fieldNames as $key => $fieldName) {
  92.                     if ($fields[$key] == "") continue;
  93.                     $note['ActivityNote']['text'] .= $fieldName.": ".$fields[$key]."; ";
  94.                 }
  95.                 $note['ActivityNote']['contact_id'] = $contactId;
  96.                 $note['ActivityNote']['activity_date'] = CakeTime::format('Y-m-d', round($time));
  97.                 $note['ActivityNote']['user_id'] = 0;
  98.                 $note['ActivityNote']['activity_type_id'] = '3';
  99.                 $this->ActivityNote->create();
  100.                 $succes = $this->ActivityNote->save($note);
  101.                 if ($success && isset($companyId)) {
  102.                     $note['ActivityNote']['contact_id'] = $companyId;
  103.                     $this->ActivityNote->create();
  104.                     $succes = $this->ActivityNote->save($note);
  105.                 }
  106.             }
  107.             if ($success) {
  108.                 foreach ($submits as $key => $value) {
  109.                     $submits[$key]['Submit']['imported'] = '1';
  110.                 }
  111.                 $this->Submit->saveAll($submits);
  112.                 $dataSource->commit();
  113.                 $this->Session->setFlash(__('Henvendelsen er importeret.'), 'default', array('class' => 'success'));
  114.                 $contId = (isset($companyId))? $companyId : $contactId;
  115.                 $this->redirect(array('controller' => 'Contacts', 'action' => 'view/'.$contId));
  116.             } else {
  117.                 $dataSource->rollback();
  118.                 $this->Session->setFlash(__('Henvendelsen kunne ikke importeres. Prøv igen.'));
  119.             }
  120.             return $this->redirect(array('action' => 'index'));
  121.         } else {
  122.             $names = TRIM($fields['name']);
  123.             $names = PREG_REPLACE( '/\s+/', ' ', $names);
  124.             $names = explode(' ', $names);
  125.             $nameCount = count($names);
  126.             $contact = array();
  127.             $contact['Contact']['first_name'] = $names[0];
  128.             $contact['Contact']['last_name'] = ($nameCount > 1)? $names[$nameCount-1] : '';
  129.             $contact['Contact']['middle_names'] = '';
  130.             if (count($names) > 2) {
  131.                 unset($names[$nameCount-1]);
  132.                 unset($names[0]);
  133.                 $contact['Contact']['middle_names'] = implode($names);
  134.             }
  135.             $fieldNames = array('address1' => 'address', 'zip' => 'zip', 'city' => 'city', 'email' => 'email');
  136.             foreach ($fieldNames as $contactField => $submissionField) {
  137.                 $contact['Contact'][$contactField] = $fields[$submissionField];    
  138.             }
  139.             $contact['Contact']['company_name'] = ($fields['company'] != 'Evt.firma')? $fields['company'] : '';
  140.             $contact['Contact']['contact_type_id'] = 1;
  141.             $phone = PREG_REPLACE( '/\s+/', '', $fields['phone']);
  142.             $phone = PREG_REPLACE( '/\+/', '00', $phone);
  143.             $phone = PREG_REPLACE( '/[^0-9]/', '', $phone);
  144.             $contact['Contact']['phone_number'] = $phone;
  145.             $this->request->data = $contact;   
  146.             $contactTypes = $this->Contact->ContactType->find('list', array(
  147.                 'fields' => array('ContactType.id', 'ContactType.value'),
  148.                 'conditions' => array('ContactType.id !=' => '3')
  149.                 )
  150.             );
  151.             if ($phone == '') $phone = '999';
  152.             if ($fields['email'] == '') $fields['email'] = 'noMail';
  153.             if ($fields['company'] == '') $fields['company'] = 'noCompany';
  154.             $this->Contact->recursive = -1;
  155.             $doubles = $this->Contact->find('all', array(
  156.                 'conditions' => array(
  157.                     'OR' => array(
  158.                         'phone_number' => $phone, 'cell_number' => $phone, 'email' => $fields['email'],
  159.                         'lower(company_name)' => strtolower($fields['company']),
  160.                         array(
  161.                             'AND' => array(
  162.                                 'lower(first_name)' => strtolower($contact['Contact']['first_name']),
  163.                                 'lower(last_name)' => strtolower($contact['Contact']['last_name'])
  164.                             )
  165.                         )
  166.                     )
  167.                 )
  168.             ));
  169.             $this->set('doubles', $doubles);
  170.             $this->set('contactTypes', $contactTypes);
  171.             $this->set('message', $fields['message']);
  172.             $this->set('id', $id);
  173.             $this->set('nav_bar', 'ADMIN');
  174.         }
  175.         }
  176.     }
  177. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement