1: <?php
  2: 
  3:   4:   5: 
  6: class Quform_Admin_Page_Entries_Edit extends Quform_Admin_Page_Entries
  7: {
  8:       9:  10: 
 11:     protected $formFactory;
 12: 
 13:      14:  15: 
 16:     protected $uploader;
 17: 
 18:      19:  20: 
 21:     protected $session;
 22: 
 23:      24:  25:  26:  27:  28:  29: 
 30:     public function __construct(Quform_ViewFactory $viewFactory, Quform_Repository $repository,
 31:                                 Quform_Form_Factory $formFactory, Quform_Uploader $uploader, Quform_Session $session)
 32:     {
 33:         parent::__construct($viewFactory, $repository);
 34: 
 35:         $this->formFactory = $formFactory;
 36:         $this->uploader = $uploader;
 37:         $this->session = $session;
 38:     }
 39: 
 40:     public function init()
 41:     {
 42:         $this->template = QUFORM_TEMPLATE_PATH . '/admin/entries/edit.php';
 43:     }
 44: 
 45:      46:  47: 
 48:     protected function enqueueStyles() {
 49:         wp_enqueue_style('quform-select2', Quform::url('css/select2.min.css'), array(), '4.0.13');
 50: 
 51:         parent::enqueueStyles();
 52:     }
 53: 
 54:      55:  56: 
 57:     protected function enqueueScripts()
 58:     {
 59:         wp_deregister_script('jquery-form');
 60:         wp_enqueue_script('jquery-form', Quform::url('js/jquery.form.min.js'), array('jquery'), '4.3.0', true);
 61:         wp_enqueue_script('quform-select2', Quform::url('js/select2.min.js'), array('jquery'), '4.0.13', true);
 62: 
 63:         parent::enqueueScripts();
 64: 
 65:         wp_enqueue_script('quform-entries-edit', Quform::adminUrl('js/entries.edit.min.js'), array('jquery'), QUFORM_VERSION, true);
 66: 
 67:         wp_localize_script('quform-entries-edit', 'quformEntriesEditL10n', array(
 68:             'none' => __('None', 'quform'),
 69:             'searchUsersNonce' => wp_create_nonce('quform_entries_search_users')
 70:         ));
 71:     }
 72: 
 73:      74:  75:  76:  77: 
 78:     protected function getAdminTitle()
 79:     {
 80:         return __('Edit Entry', 'quform');
 81:     }
 82: 
 83:      84:  85:  86:  87:  88:  89: 
 90:     public function getNavHtml(array $currentForm = null, array $extra = array())
 91:     {
 92:         $extra[10] = sprintf(
 93:             '<div class="qfb-nav-item"><a class="qfb-nav-item-link" href="%s"><i class="qfb-icon qfb-icon-arrow-left"></i></a></div>',
 94:             esc_url(add_query_arg(array(
 95:                 'id' => $currentForm['id'],
 96:                 'sp' => false,
 97:                 'eid' => false,
 98:                 'read' => false,
 99:                 'unread' => false,
100:                 'error' => false,
101:                 'deleted' => false
102:             )))
103:         );
104: 
105:         $extra[40] = sprintf(
106:             '<div class="qfb-nav-item qfb-nav-page-info"><i class="qfb-nav-page-icon qfb-mdi qfb-mdi-message"></i><span class="qfb-nav-page-title">%s</span></div>',
107:             
108:             Quform::escape(sprintf(__('Editing entry for %s', 'quform'), $currentForm['name']))
109:         );
110: 
111:         return parent::getNavHtml($currentForm, $extra);
112:     }
113: 
114:     115: 116: 
117:     public function process()
118:     {
119:         if ( ! current_user_can('quform_edit_entries')) {
120:             wp_die(__( 'You do not have sufficient permissions to access this page.', 'quform'), 403);
121:         }
122: 
123:         $entryId = isset($_GET['eid']) && is_numeric($_GET['eid']) ? (int) $_GET['eid'] : null;
124: 
125:         $entry = $this->repository->findEntry($entryId);
126: 
127:         if ( ! is_array($entry)) {
128:             wp_die(__("You attempted to edit an item that doesn't exist. Perhaps it was deleted?", 'quform'));
129:         }
130: 
131:         $formId = $this->repository->getFormIdFromEntryId($entryId);
132: 
133:         $config = $this->repository->getConfig($formId);
134: 
135:         if ( ! is_array($config)) {
136:             wp_die(__("You attempted to edit an item that doesn't exist. Perhaps it was deleted?", 'quform'));
137:         }
138: 
139:         $uniqueId = Quform_Form::generateUniqueId();
140: 
141:         while ($this->session->has(sprintf('quform-%s', $uniqueId))) {
142:             $uniqueId = Quform_Form::generateUniqueId();
143:         }
144: 
145:         $config['uniqueId'] = $uniqueId;
146:         $config['entryId'] = $entryId;
147:         $config['environment'] = 'editEntry';
148: 
149:         $form = $this->formFactory->create($config);
150: 
151:         $form->setValues(array(), true);
152: 
153:         foreach ($entry as $key => $value) {
154:             if (preg_match('/element_(\d+)/', $key, $matches)) {
155:                 $form->setValueFromStorage($matches[1], $value);
156:                 unset($entry[$key]);
157:             }
158:         }
159: 
160:         
161:         $this->uploader->saveFileUploadValuesIntoSession($form);
162: 
163:         
164:         if ($entry['unread'] == 1) {
165:             $this->repository->readEntries(array($entry['id']));
166:         }
167: 
168:         
169:         $entry['labels'] = $this->repository->getEntryLabels($entryId);
170: 
171:         $data = array(
172:             'form' => $form,
173:             'entry' => $entry,
174:             'labels' => $this->repository->getFormEntryLabels($form->getId())
175:         );
176: 
177:         $this->view->with($data);
178:     }
179: }
180: