1: <?php
  2: 
  3:   4:   5: 
  6: class Quform_Entry_Processor extends Quform_Form_Processor
  7: {
  8:       9:  10:  11:  12:  13: 
 14:     public function process(Quform_Form $form)
 15:     {
 16:         
 17:         $_POST = wp_unslash($_POST);
 18: 
 19:         
 20:         $result = apply_filters('quform_entry_pre_process', array(), $form);
 21:         $result = apply_filters('quform_entry_pre_process_' . $form->getId(), $result, $form);
 22: 
 23:         if (is_array($result) && ! empty($result)) {
 24:             return $result;
 25:         }
 26: 
 27:         $this->uploader->mergeSessionFiles($form);
 28: 
 29:         $form->setValues($_POST, true);
 30: 
 31:         $result = apply_filters('quform_entry_post_set_form_values', array(), $form);
 32:         $result = apply_filters('quform_entry_post_set_form_values_' . $form->getId(), $result, $form);
 33: 
 34:         if (is_array($result) && ! empty($result)) {
 35:             return $result;
 36:         }
 37: 
 38:         
 39:         $form->calculateElementVisibility();
 40: 
 41:         $result = apply_filters('quform_entry_pre_validate', array(), $form);
 42:         $result = apply_filters('quform_entry_pre_validate_' . $form->getId(), $result, $form);
 43: 
 44:         if (is_array($result) && ! empty($result)) {
 45:             return $result;
 46:         }
 47: 
 48:         list($valid) = $form->isValid();
 49: 
 50:         if ($valid) {
 51:             
 52:             $result = apply_filters('quform_entry_post_validate', array(), $form);
 53:             $result = apply_filters('quform_entry_post_validate_' . $form->getId(), $result, $form);
 54: 
 55:             if (is_array($result) && ! empty($result)) {
 56:                 return $result;
 57:             }
 58: 
 59:             
 60:             $entryId = $this->saveEntry($form);
 61:             $form->setEntryId($entryId);
 62: 
 63:             $result = apply_filters('quform_entry_post_set_entry_id', array(), $form);
 64:             $result = apply_filters('quform_entry_post_set_entry_id_' . $form->getId(), $result, $form);
 65: 
 66:             if (is_array($result) && ! empty($result)) {
 67:                 return $result;
 68:             }
 69: 
 70:             
 71:             $this->uploader->process($form);
 72: 
 73:             
 74:             $this->saveEntryData($entryId, $form);
 75: 
 76:             
 77:             $result = apply_filters('quform_entry_post_process', array(), $form);
 78:             $result = apply_filters('quform_entry_post_process_' . $form->getId(), $result, $form);
 79: 
 80:             if (is_array($result) && ! empty($result)) {
 81:                 return $result;
 82:             }
 83: 
 84:             return array(
 85:                 'type' => 'success',
 86:                 'data' => array('id' => $entryId),
 87:                 'message' => __('Entry saved', 'quform')
 88:             );
 89:         }
 90: 
 91:         return array(
 92:             'type' => 'error',
 93:             'errors' => $form->getErrors()
 94:         );
 95:     }
 96: 
 97:      98:  99: 100: 101: 102: 
103:     protected function saveEntry(Quform_Form $form)
104:     {
105:         $currentTime = Quform::date('Y-m-d H:i:s', null, new DateTimeZone('UTC'));
106: 
107:         if ($createdAt = Quform::get($_POST, 'entry_created_at')) {
108:             try {
109:                 $createdAt = Quform::date(
110:                     'Y-m-d H:i:s',
111:                     new DateTime($createdAt, new DateTimeZone('UTC')),
112:                     new DateTimeZone('UTC')
113:                 );
114: 
115:                 if ($createdAt === false) {
116:                     $createdAt = $currentTime;
117:                 }
118:             } catch (Exception $e) {
119:                 $createdAt = $currentTime;
120:             }
121:         } else {
122:             $createdAt = $currentTime;
123:         }
124: 
125:         $entry = array(
126:             'form_id'       => $form->getId(),
127:             'ip'            => Quform::substr(Quform::get($_POST, 'entry_ip'), 0, 45),
128:             'form_url'      => Quform::substr(Quform::get($_POST, 'entry_form_url'), 0, 512),
129:             'referring_url' => Quform::substr(Quform::get($_POST, 'entry_referring_url'), 0, 512),
130:             'post_id'       => is_numeric($postId = Quform::get($_POST, 'entry_post_id')) && $postId > 0 ? (int) $postId : null,
131:             'created_by'    => is_numeric($createdBy = Quform::get($_POST, 'entry_created_by')) && $createdBy > 0 ? (int) $createdBy : null,
132:             'created_at'    => $createdAt,
133:             'updated_at'    => $currentTime
134:         );
135: 
136:         $entry = $this->repository->saveEntry($entry, $form->getEntryId());
137: 
138:         return $entry['id'];
139:     }
140: 
141:     142: 143: 144: 145: 146: 
147:     protected function saveEntryData($entryId, Quform_Form $form)
148:     {
149:         if ( ! ($entryId > 0)) {
150:             return;
151:         }
152: 
153:         $data = array();
154: 
155:         foreach ($form->getRecursiveIterator() as $element) {
156:             if ($element instanceof Quform_Element_Editable && $element->config('saveToDatabase')) {
157:                 $data[$element->getId()] = $element->getValueForStorage();
158:             }
159:         }
160: 
161:         if (count($data)) {
162:             $this->repository->saveEntryData($entryId, $data);
163:         }
164:     }
165: }
166: