| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* E2pdf Controller |
| 5 |
* |
| 6 |
* @copyright Copyright 2017 https://e2pdf.com |
| 7 |
* @license GPL v2 |
| 8 |
* @version 1 |
| 9 |
* @link https://e2pdf.com |
| 10 |
* @since 0.00.01 |
| 11 |
*/ |
| 12 |
if (!defined('ABSPATH')) { |
| 13 |
die('Access denied.'); |
| 14 |
} |
| 15 |
|
| 16 |
class Controller_E2pdf extends Helper_E2pdf_View { |
| 17 |
|
| 18 |
/** |
| 19 |
* @url admin.php?page=e2pdf |
| 20 |
*/ |
| 21 |
public function index_action() { |
| 22 |
|
| 23 |
$users_tmp = get_users(array( |
| 24 |
'fields' => array( |
| 25 |
'ID', 'user_login' |
| 26 |
) |
| 27 |
)); |
| 28 |
|
| 29 |
$users = array( |
| 30 |
'0' => __('--- Select ---', 'e2pdf') |
| 31 |
); |
| 32 |
foreach ($users_tmp as $user) { |
| 33 |
$users[$user->ID] = $user->user_login; |
| 34 |
} |
| 35 |
|
| 36 |
$this->view('users', $users); |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* @url admin.php?page=e2pdf&action=export |
| 41 |
*/ |
| 42 |
public function export_action() { |
| 43 |
|
| 44 |
$template_id = (int) $this->get->get('template_id'); |
| 45 |
$dataset_id = $this->get->get('dataset_id'); |
| 46 |
$user_id = 0; |
| 47 |
|
| 48 |
if ($template_id && $dataset_id) { |
| 49 |
|
| 50 |
$template = new Model_E2pdf_Template(); |
| 51 |
|
| 52 |
if ($template->load($template_id)) { |
| 53 |
|
| 54 |
if ($this->post->get()) { |
| 55 |
foreach ($this->post->get('options') as $key => $value) { |
| 56 |
if ($key == 'user_id') { |
| 57 |
$user_id = $value; |
| 58 |
} else { |
| 59 |
$template->set($key, $value); |
| 60 |
} |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
$uid = false; |
| 65 |
$uid_params = array(); |
| 66 |
$uid_params['template_id'] = $template_id; |
| 67 |
$uid_params['dataset'] = $dataset_id; |
| 68 |
$uid_params['user_id'] = $user_id; |
| 69 |
|
| 70 |
$entry = new Model_E2pdf_Entry(); |
| 71 |
$entry->set('entry', $uid_params); |
| 72 |
if ($entry->load_by_uid($entry->get('uid'))) { |
| 73 |
$uid = $entry->get('uid'); |
| 74 |
} |
| 75 |
|
| 76 |
$template->extension()->set('dataset', $dataset_id); |
| 77 |
$template->extension()->set('user_id', $user_id); |
| 78 |
|
| 79 |
$template->set('name', $template->extension()->render($template->get('name'), 'value')); |
| 80 |
$template->set('password', $template->extension()->render($template->get('password'), 'value')); |
| 81 |
$template->set('meta_title', $template->extension()->render($template->get('meta_title'), 'value')); |
| 82 |
$template->set('meta_subject', $template->extension()->render($template->get('meta_subject'), 'value')); |
| 83 |
$template->set('meta_author', $template->extension()->render($template->get('meta_author'), 'value')); |
| 84 |
$template->set('meta_keywords', $template->extension()->render($template->get('meta_keywords'), 'value')); |
| 85 |
|
| 86 |
$template->fill($dataset_id, $uid); |
| 87 |
$request = $template->render(); |
| 88 |
|
| 89 |
if (isset($request['error'])) { |
| 90 |
$this->add_notification('error', $request['error']); |
| 91 |
$this->render('blocks', 'notifications'); |
| 92 |
} else { |
| 93 |
$filename = $template->get_filename(); |
| 94 |
$file = $request['file']; |
| 95 |
$this->download_response('pdf', $file, $filename); |
| 96 |
} |
| 97 |
} else { |
| 98 |
$this->add_notification('error', __("Template can't be loaded", 'e2pdf')); |
| 99 |
$this->render('blocks', 'notifications'); |
| 100 |
} |
| 101 |
} else { |
| 102 |
$this->error('404'); |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Get activated templates list |
| 108 |
* |
| 109 |
* @return array() - Activated templates list |
| 110 |
*/ |
| 111 |
public function get_active_templates() { |
| 112 |
global $wpdb; |
| 113 |
$model_e2pdf_template = new Model_E2pdf_Template(); |
| 114 |
|
| 115 |
$condition = array( |
| 116 |
'trash' => array( |
| 117 |
'condition' => '<>', |
| 118 |
'value' => '1', |
| 119 |
'type' => '%d' |
| 120 |
), |
| 121 |
'activated' => array( |
| 122 |
'condition' => '=', |
| 123 |
'value' => '1', |
| 124 |
'type' => '%d' |
| 125 |
) |
| 126 |
); |
| 127 |
|
| 128 |
$order_condition = array( |
| 129 |
'orderby' => 'id', |
| 130 |
'order' => 'desc', |
| 131 |
); |
| 132 |
|
| 133 |
$where = $this->helper->load('db')->prepare_where($condition); |
| 134 |
$orderby = $this->helper->load('db')->prepare_orderby($order_condition); |
| 135 |
|
| 136 |
|
| 137 |
$templates = $wpdb->get_results($wpdb->prepare("SELECT * FROM " . $model_e2pdf_template->get_table() . $where['sql'] . $orderby . "", $where['filter'])); |
| 138 |
$export_templates = array(); |
| 139 |
|
| 140 |
$export_templates[] = array( |
| 141 |
'key' => '', |
| 142 |
'value' => __('--- Select ---', 'e2pdf') |
| 143 |
); |
| 144 |
|
| 145 |
if (!empty($templates)) { |
| 146 |
foreach ($templates as $key => $value) { |
| 147 |
$export_templates[] = array( |
| 148 |
'key' => $value->ID, |
| 149 |
'value' => $value->title |
| 150 |
); |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
return $export_templates; |
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* Get entries for template |
| 159 |
* |
| 160 |
* @return array() - Entries for template |
| 161 |
*/ |
| 162 |
public function get_datasets($template_id = false) { |
| 163 |
|
| 164 |
$datasets = array(); |
| 165 |
|
| 166 |
$datasets[] = array( |
| 167 |
'key' => '', |
| 168 |
'value' => __('--- Select ---', 'e2pdf') |
| 169 |
); |
| 170 |
|
| 171 |
if ($template_id) { |
| 172 |
$template = new Model_E2pdf_Template(); |
| 173 |
|
| 174 |
if ($template->load($template_id)) { |
| 175 |
$datasets_tmp = $template->extension()-> |
| 176 |
datasets( |
| 177 |
$template->get('item'), $template->get('dataset_title') |
| 178 |
); |
| 179 |
|
| 180 |
if ($datasets_tmp && is_array($datasets_tmp)) { |
| 181 |
$datasets = array_merge($datasets, $datasets_tmp); |
| 182 |
} |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
return $datasets; |
| 187 |
} |
| 188 |
|
| 189 |
/** |
| 190 |
* Get options to overwrite on template |
| 191 |
* |
| 192 |
* @return array() - List of options to overwrite |
| 193 |
*/ |
| 194 |
public function get_options($template_id = false) { |
| 195 |
|
| 196 |
$options = array(); |
| 197 |
|
| 198 |
if ($template_id) { |
| 199 |
$template = new Model_E2pdf_Template(); |
| 200 |
$template->load($template_id); |
| 201 |
$options['name'] = $template->get('name'); |
| 202 |
$options['password'] = $template->get('password'); |
| 203 |
$options['flatten'] = $template->get('flatten'); |
| 204 |
$options['user_id'] = get_current_user_id(); |
| 205 |
} |
| 206 |
|
| 207 |
return $options; |
| 208 |
} |
| 209 |
|
| 210 |
/** |
| 211 |
* Get templates list via ajax |
| 212 |
* action: wp_ajax_e2pdf_templates |
| 213 |
* function: e2pdf_templates |
| 214 |
* |
| 215 |
* @return json - Templates list |
| 216 |
*/ |
| 217 |
public function ajax_templates() { |
| 218 |
|
| 219 |
$this->check_nonce($this->get->get('_nonce'), 'e2pdf_ajax'); |
| 220 |
|
| 221 |
$template_id = (int) $this->post->get('data'); |
| 222 |
$content = array(); |
| 223 |
$content['datasets'] = $this->get_datasets($template_id); |
| 224 |
$content['options'] = $this->get_options($template_id); |
| 225 |
$content['url'] = $this->helper->get_url(array('page' => 'e2pdf-templates', 'action' => 'edit', 'id' => $template_id)); |
| 226 |
|
| 227 |
$template = new Model_E2pdf_Template(); |
| 228 |
if ($template->load($template_id)) { |
| 229 |
$content['delete_items'] = $template->extension()->method('delete_items'); |
| 230 |
} |
| 231 |
|
| 232 |
$response = array( |
| 233 |
'content' => $content, |
| 234 |
); |
| 235 |
|
| 236 |
$this->json_response($response); |
| 237 |
} |
| 238 |
|
| 239 |
/** |
| 240 |
* Get entries list via ajax |
| 241 |
* action: wp_ajax_e2pdf_entry |
| 242 |
* function: e2pdf_entry |
| 243 |
* |
| 244 |
* @return json - Entries list |
| 245 |
*/ |
| 246 |
public function ajax_dataset() { |
| 247 |
|
| 248 |
$this->check_nonce($this->get->get('_nonce'), 'e2pdf_ajax'); |
| 249 |
|
| 250 |
$data = $this->post->get('data'); |
| 251 |
|
| 252 |
$template_id = (int) $data['template']; |
| 253 |
$dataset_id = (int) $data['dataset']; |
| 254 |
|
| 255 |
$template = new Model_E2pdf_Template(); |
| 256 |
|
| 257 |
$content = array( |
| 258 |
'export' => false, |
| 259 |
'view' => false, |
| 260 |
'delete_item' => false, |
| 261 |
'dataset' => false |
| 262 |
); |
| 263 |
|
| 264 |
if ($template->load($template_id)) { |
| 265 |
$dataset = $template->extension()->dataset($dataset_id); |
| 266 |
|
| 267 |
if ($dataset) { |
| 268 |
$content = array( |
| 269 |
'export' => true, |
| 270 |
'view' => isset($dataset->url) && $dataset->url ? true : false, |
| 271 |
'delete_item' => $template->extension()->method('delete_item'), |
| 272 |
'dataset' => $dataset |
| 273 |
); |
| 274 |
} |
| 275 |
} |
| 276 |
|
| 277 |
$response = array( |
| 278 |
'content' => $content, |
| 279 |
); |
| 280 |
|
| 281 |
$this->json_response($response); |
| 282 |
} |
| 283 |
|
| 284 |
public function ajax_delete_item() { |
| 285 |
|
| 286 |
$this->check_nonce($this->get->get('_nonce'), 'e2pdf_ajax'); |
| 287 |
|
| 288 |
$data = $this->post->get('data'); |
| 289 |
|
| 290 |
$template_id = (int) $data['template']; |
| 291 |
$dataset_id = (int) $data['dataset']; |
| 292 |
|
| 293 |
if (!$template_id || !$dataset_id) { |
| 294 |
return; |
| 295 |
} |
| 296 |
|
| 297 |
$template = new Model_E2pdf_Template(); |
| 298 |
|
| 299 |
$action = false; |
| 300 |
if ($template->load($template_id)) { |
| 301 |
$action = $template->extension()->delete_item($template_id, $dataset_id); |
| 302 |
} |
| 303 |
|
| 304 |
if ($action) { |
| 305 |
$response = array( |
| 306 |
'redirect' => $this->helper->get_url(array( |
| 307 |
'page' => 'e2pdf', |
| 308 |
'template_id' => $template_id |
| 309 |
) |
| 310 |
) |
| 311 |
); |
| 312 |
|
| 313 |
$this->add_notification('update', __('Dataset removed successfully', 'e2pdf')); |
| 314 |
} else { |
| 315 |
$response = array( |
| 316 |
'error' => __("Dataset can't be removed!", 'e2pdf') |
| 317 |
); |
| 318 |
} |
| 319 |
|
| 320 |
$this->json_response($response); |
| 321 |
} |
| 322 |
|
| 323 |
public function ajax_delete_items() { |
| 324 |
$this->check_nonce($this->get->get('_nonce'), 'e2pdf_ajax'); |
| 325 |
|
| 326 |
$data = $this->post->get('data'); |
| 327 |
$template_id = (int) $data['template']; |
| 328 |
|
| 329 |
|
| 330 |
if (!$template_id) { |
| 331 |
return; |
| 332 |
} |
| 333 |
|
| 334 |
$template = new Model_E2pdf_Template(); |
| 335 |
|
| 336 |
$action = false; |
| 337 |
if ($template->load($template_id)) { |
| 338 |
$action = $template->extension()->delete_items($template_id); |
| 339 |
} |
| 340 |
|
| 341 |
if ($action) { |
| 342 |
$response = array( |
| 343 |
'redirect' => $this->helper->get_url(array( |
| 344 |
'page' => 'e2pdf', |
| 345 |
'template_id' => $template_id |
| 346 |
)) |
| 347 |
); |
| 348 |
|
| 349 |
$this->add_notification('update', __('Datasets removed successfully', 'e2pdf')); |
| 350 |
} else { |
| 351 |
$response = array( |
| 352 |
'error' => __("Datasets can't be removed!", 'e2pdf') |
| 353 |
); |
| 354 |
} |
| 355 |
|
| 356 |
$this->json_response($response); |
| 357 |
} |
| 358 |
|
| 359 |
} |
| 360 |
|