SchedulingController.php
157 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Wpae\App\UnsecuredController; |
| 4 | |
| 5 | |
| 6 | use Wpae\App\Service\Addons\AddonNotFoundException; |
| 7 | use Wpae\App\Service\Addons\AddonService; |
| 8 | use Wpae\Controller\BaseController; |
| 9 | use Wpae\Http\Request; |
| 10 | use Wpae\Scheduling\Export; |
| 11 | use Wpae\Http\JsonResponse; |
| 12 | |
| 13 | class SchedulingController extends BaseController |
| 14 | { |
| 15 | /** Scheduling API Version */ |
| 16 | const VERSION = 1; |
| 17 | |
| 18 | /** @var Export */ |
| 19 | private $scheduledExportService; |
| 20 | |
| 21 | public function __construct($container) |
| 22 | { |
| 23 | parent::__construct($container); |
| 24 | $this->scheduledExportService = new Export(); |
| 25 | } |
| 26 | |
| 27 | public function triggerAction(Request $request) |
| 28 | { |
| 29 | if (!$this->isRequestValid()) { |
| 30 | return new JsonResponse(array('message' => 'Export hash is invalid'), 401); |
| 31 | } |
| 32 | |
| 33 | $exportId = intval($request->get('export_id')); |
| 34 | |
| 35 | $export = new \PMXE_Export_Record(); |
| 36 | $export->getById($exportId); |
| 37 | |
| 38 | if ($export->isEmpty()) { |
| 39 | return new JsonResponse(array('message' => 'Export not found'), 404); |
| 40 | } |
| 41 | |
| 42 | if ((int)$export->executing) { |
| 43 | return new JsonResponse(array("message" => "Export #" . $export->id . " is currently in manually process. Request skipped."), 409); |
| 44 | } |
| 45 | if ($export->processing and !$export->triggered) { |
| 46 | return new JsonResponse(array("message" => "Export #" . $export->id . " currently in process. Request skipped."), 409); |
| 47 | |
| 48 | } |
| 49 | if (!$export->processing and $export->triggered) { |
| 50 | return new JsonResponse(array("message" => "Export #" . $export->id . " already triggered. Request skipped."), 409); |
| 51 | } |
| 52 | |
| 53 | if (!$export->processing and !$export->triggered) { |
| 54 | $this->scheduledExportService->trigger($export); |
| 55 | |
| 56 | return new JsonResponse(array('message' => "#" . $export->id . " Cron job triggered.")); |
| 57 | } |
| 58 | |
| 59 | return new JsonResponse(array("message" => "Can't process"), 500); |
| 60 | } |
| 61 | |
| 62 | public function processAction(Request $request) |
| 63 | { |
| 64 | if (!$this->isRequestValid()) { |
| 65 | return new JsonResponse(array('message' => 'Export hash is invalid'), 401); |
| 66 | } |
| 67 | |
| 68 | $exportId = intval($request->get('export_id')); |
| 69 | |
| 70 | $export = new \PMXE_Export_Record(); |
| 71 | $export->getById($exportId); |
| 72 | |
| 73 | $this->disableExportsThatDontHaveAddon($export); |
| 74 | |
| 75 | if ($export->isEmpty()) { |
| 76 | return new JsonResponse(array('message' => 'Export not found'), 404); |
| 77 | } |
| 78 | |
| 79 | $logger = function($m) { |
| 80 | echo "<p>$m</p>\\n"; |
| 81 | }; |
| 82 | |
| 83 | if ($export->processing == 1 and (time() - strtotime($export->registered_on)) > 120) { |
| 84 | // it means processor crashed, so it will reset processing to false, and terminate. Then next run it will work normally. |
| 85 | $export->set(array( |
| 86 | 'processing' => 0 |
| 87 | ))->update(); |
| 88 | } |
| 89 | |
| 90 | // start execution imports that is in the cron process |
| 91 | if (!(int)$export->triggered) { |
| 92 | if (!empty($export->parent_id) or empty($queue_exports)) { |
| 93 | return new JsonResponse(array("message" => 'Export #' . $exportId . ' is not triggered. Request skipped.'), 400); |
| 94 | } |
| 95 | } elseif ((int)$export->executing) { |
| 96 | return new JsonResponse(array('message' => 'Export #' . $exportId . ' is currently in manually process. Request skipped.'), 409); |
| 97 | } elseif ((int)$export->triggered and !(int)$export->processing) { |
| 98 | |
| 99 | try { |
| 100 | $export->set(array('canceled' => 0))->execute($logger, true); |
| 101 | } catch (AddonNotFoundException $e) { |
| 102 | die($e->getMessage()); |
| 103 | } |
| 104 | if (!(int)$export->triggered and !(int)$export->processing) { |
| 105 | $this->scheduledExportService->process($export); |
| 106 | return new JsonResponse(array('Export #' . $exportId . ' complete'), 201); |
| 107 | } else { |
| 108 | return new JsonResponse(array('message' => 'Records Processed ' . (int)$export->exported . '.')); |
| 109 | } |
| 110 | |
| 111 | } else { |
| 112 | return new JsonResponse(array('message' => 'Export #' . $exportId . ' already processing. Request skipped.'), 409); |
| 113 | } |
| 114 | |
| 115 | return new JsonResponse(array("message" => "Can't process"), 500); |
| 116 | } |
| 117 | |
| 118 | public function versionAction() |
| 119 | { |
| 120 | return new JsonResponse(array('version' => self::VERSION)); |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * @return bool |
| 125 | */ |
| 126 | private function isRequestValid() |
| 127 | { |
| 128 | $cron_job_key = \PMXE_Plugin::getInstance()->getOption('cron_job_key'); |
| 129 | return |
| 130 | !empty($cron_job_key) and |
| 131 | !empty($_GET['export_id']) and |
| 132 | !empty($_GET['export_key']) and |
| 133 | $_GET['export_key'] == $cron_job_key; |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * @param $export |
| 138 | */ |
| 139 | private function disableExportsThatDontHaveAddon($export) |
| 140 | { |
| 141 | $cpt = $export->options['cpt']; |
| 142 | if (!is_array($cpt)) { |
| 143 | $cpt = array($cpt); |
| 144 | } |
| 145 | |
| 146 | $addons = new AddonService(); |
| 147 | |
| 148 | if ( |
| 149 | ((in_array('users', $cpt) || in_array('shop_customer', $cpt)) && !$addons->isUserAddonActive()) |
| 150 | || |
| 151 | ($export->options['export_type'] == 'advanced' && $export->options['wp_query_selector'] == 'wp_user_query' && !$addons->isUserAddonActive()) |
| 152 | ) { |
| 153 | die(\__('The User Export Add-On Pro is required to run this export. You can download the add-on here: <a href="http://www.wpallimport.com/portal/" target="_blank">http://www.wpallimport.com/portal/</a>', \PMXE_Plugin::LANGUAGE_DOMAIN)); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | } |