Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace console\controllers;
- use common\helpers\NoticeHelper;
- use common\helpers\StringHelper;
- use common\helpers\Translit;
- use common\models\job\Blacklist;
- use common\models\job\Job;
- use common\models\job\JobImportUpload;
- use common\models\job\Log;
- use common\modules\sites\models\SitesTasks;
- use common\modules\user\models\notify\UserNotification;
- use common\modules\user\models\User;
- use Yii;
- use yii\console\Controller;
- use yii\console\Exception as ConsoleException;
- use yii\data\Pagination;
- use yii\db\Query;
- use yii\helpers\Json;
- use yii\helpers\Url;
- ini_set('error_reporting', E_ALL);
- ini_set('display_errors', 1);
- ini_set('display_startup_errors', 1);
- class JobController extends Controller
- {
- public function beforeAction($action)
- {
- if (parent::beforeAction($action)) {
- echo "\r\n" . 'START' . "\r\n" . '...' . "\r\n";
- return true;
- } else
- return false;
- }
- public function afterAction($action, $result)
- {
- echo "\r\n" . '...' . "\r\n" . 'END.' . "\r\n\r\n";
- return parent::afterAction($action, $result);
- }
- /**
- * Импорт задач массово
- * /path/to/yii job/import-job-webmaster $limit
- */
- public function actionImportJobWebmaster($limit)
- {
- echo "Import task here. Limit xml file check: $limit\n";
- $xmls = JobImportUpload::find()->where('status = :status', [':status' => 'upload'])->limit($limit)->all();
- if (!empty($xmls)) {
- foreach ($xmls as $xmlFile) {
- $countSuccess = 0;
- $countError = 0;
- $errors = [];
- $url = Yii::getAlias('@frontend/web') . JobImportUpload::PATH_UPLOAD_XML . $xmlFile->url;
- $xml = simplexml_load_file($url, 'SimpleXMLElement', LIBXML_NOCDATA);
- $countTask = count($xml->task);
- if ($countTask > 0) {
- $siteStructure = SitesTasks::findOne($xmlFile->st_id);
- if ($siteStructure !== NULL) {
- $keyTask = 0;
- $sendNoticeNewJob = [];
- foreach ($xml->task as $task) {
- $validated = true;
- if (count($task->job)) {
- if (isset($task->structure->_source_text)) {
- $task->structure->_source_text = htmlspecialchars($task->structure->_source_text);
- }
- $job = new Job();
- $job->isCreateUpdate = true;
- $job->setAttributes((array)$task->job);
- $job->price_per = Yii::$app->params['job.price_per'];
- $job->site_task_id = $xmlFile->st_id;
- $job->creator_id = $xmlFile->user_id;
- if (empty($job->skype)) {
- $job->skype = 'adcombo_job';
- }
- $job->import_id = $xmlFile->id;
- $userCreator = User::findOne($job->creator_id);
- $job->balanceWebmaster = $userCreator->userMoneys->balance;
- if ($job->validate()) {
- $job->structure = $siteStructure->structure;
- if (count($task->structure)) {
- $structureTask = (array)$task->structure;
- $fieldsOrig = json_decode($job->structure, true);
- $fieldsJob = [];
- if (isset($fieldsOrig['groupField']))
- $fieldsJob['groupField'] = $fieldsOrig['groupField'];
- foreach ($fieldsOrig['formField'] as $keyGroup => $fields) {
- //Проверка на группы
- $keyGroupInput = explode('#', $keyGroup);
- $inGroup = 0;
- $countInGroup = 1;
- if ($keyGroupInput[0] == 'group') {
- $inGroup = 1;
- $countInGroup = $fieldsOrig['groupField'][$keyGroupInput[1]]['minList'];
- $nameGroupTranslit = '_' . Translit::translate($fieldsOrig['groupField'][$keyGroupInput[1]]['nameGroup']);
- $countInGroupXml = 0;
- if (isset($structureTask[$nameGroupTranslit]))
- $countInGroupXml = count($structureTask[$nameGroupTranslit]->item);
- if ($countInGroupXml > $countInGroup) {
- $countInGroup = $countInGroupXml;
- $fieldsJob['groupField'][$keyGroupInput[1]]['minList'] = $countInGroupXml;
- }
- if ($countInGroupXml > $fieldsJob['groupField'][$keyGroupInput[1]]['maxList'])
- $fieldsJob['groupField'][$keyGroupInput[1]]['maxList'] = $countInGroupXml;
- }
- for ($i = 0; $i < $countInGroup; $i++) {
- foreach ($fields as $key => $field) {
- $nameFieldTranslit = '_' . Translit::translate($field['options']['nameField']);
- if ($inGroup) {
- if (isset($structureTask[$nameGroupTranslit]->item[$i]->{$nameFieldTranslit}))
- $fieldsJob['formField'][$keyGroup][$key]['value'][$i] = (string)$structureTask[$nameGroupTranslit]->item[$i]->{$nameFieldTranslit};
- } else {
- if (isset($structureTask[$nameFieldTranslit]))
- $fieldsJob['formField'][$keyGroup][$key]['value'] = $structureTask[$nameFieldTranslit];
- }
- }
- }
- }
- if (!isset($fieldsJob['formField'])) {
- $fieldsJob['formField'] = [];
- $validated = $validated && false;
- $errors['jobs'][$keyTask]['errors'][] = Yii::t('app', 'Incorrect fields in structure.');
- }
- // Формируем новый массив(данные) с заполненными полями из оригинального и обработанного
- $result = [];
- if (isset($fieldsOrig['formField'])) {
- if (count($fieldsJob['formField']) <> count($siteStructure->getOnlyPerfomerFieldStructure(1))) {
- $validated = $validated && false;
- $errors['jobs'][$keyTask]['errors'][] = Yii::t('app', 'The number of fields in the structure is different from the number of fields in the XML.');
- }
- $result['formField'] = array_replace_recursive($fieldsOrig['formField'], $fieldsJob['formField']);
- }
- if (isset($fieldsOrig['groupField'])) {
- $result['groupField'] = array_replace_recursive($fieldsOrig['groupField'], $fieldsJob['groupField']);
- }
- $job->structure = json_encode($result);
- unset($result);
- }
- $job->status = $xmlFile->job_status;
- $job->original_structure = $job->structure;
- if ($job->status == Job::STATUS_ACTIVATED && $job->type == Job::TYPE_TYPICAL) {
- $job->scenario = 'create';
- if (!$job->validate()) {
- $job->scenario = 'empty';
- $job->status = Job::STATUS_DRAFT;
- }
- }
- if ($validated && $job->save()) {
- if ($job->isActivated()) {
- $job->addInLastJobsCache();
- $sendNoticeNewJob[$job->orig_lang . '-' . $job->target_lang] = $job;
- }
- if ($job->status == Job::STATUS_ACTIVATED && $job->type == Job::TYPE_TYPICAL) {
- $userCreator->userMoneys->balance = $userCreator->userMoneys->balance - $job->getTotalPrice();
- $userCreator->userMoneys->save();
- $userCreator->saveMoneyLog(['sum' => $job->getTotalPrice(), 'type' => 'minus', 'ual_id' => '6', 'comment' => 'job #' . $job->id]);
- $userCreator->reservedMoney($job->getTotalPrice());
- }
- Log::create($job->creator_id, $job->id, Log::TYPE_CREATE_AWAY);
- $countSuccess++;
- } else
- $countError++;
- } else
- $countError++;
- if ($job->hasErrors())
- foreach ($job->errors as $modelErrors)
- foreach ($modelErrors as $error)
- $errors['jobs'][$keyTask]['errors'][] = $error;
- unset($modelErrors, $error);
- if (isset($errors['jobs'][$keyTask]))
- $errors['jobs'][$keyTask]['title'] = $job->title;
- } else
- $countError++;
- $keyTask++;
- }
- if (!empty($sendNoticeNewJob)) {
- foreach ($sendNoticeNewJob as $job)
- $job->addJobNoticeCache();
- //$job->sendNoticeNewJobToEmail();
- }
- $xmlFile->status = 'success';
- } else {
- $xmlFile->status = 'error';
- $errors['total'][] = Yii::t('app', 'Your structure site not found');
- }
- } else {
- $xmlFile->status = 'error';
- $errors['total'][] = Yii::t('app', 'Wrong format file');
- }
- $xmlFile->stats = Yii::t('app', 'Check {n} jobs', ['n' => $countTask]) . "\r\n";
- $xmlFile->stats .= Yii::t('app', 'Success {n} jobs', ['n' => $countSuccess]) . "\r\n";
- $xmlFile->stats .= Yii::t('app', 'Error {n} jobs', ['n' => $countError]) . "\r\n";
- if (!empty($errors))
- $xmlFile->message = serialize($errors);
- else
- $xmlFile->message = null;
- echo "#{$xmlFile->id} - {$xmlFile->stats}\n";
- echo "##------------------------------------------------##\n";
- $xmlFile->save(false);
- }
- }
- }
- /**
- * Снятие просроченных фрилансером задач
- */
- public function actionUnsetByDeadline()
- {
- $memory = memory_get_usage();
- $date = date('Y-m-d H:i:s');
- $query = new Query;
- $jobs = $query->select('id, original_structure, creator_id, user_id')
- ->from(Job::tableName())
- ->where(['or',
- ['status' => Job::STATUS_TAKEN],
- ['status' => Job::STATUS_REWORK]])
- ->andWhere(['<', 'deadline_rework', $date])
- ->andWhere(['not', ['user_id' => null]])
- //SELECT `id`, `original_structure`, `creator_id`, `user_id` FROM `job` WHERE
- // `status` IN ('taken','rework') AND `deadline_rework` < '2017-03-09 10:48:20' AND `user_id` IS NOT NULL
- /*
- ->where(['and',
- ['status' => Job::STATUS_TAKEN],
- ['<', 'deadline', $date],
- ])
- ->orWhere(['and',
- ['status' => Job::STATUS_REWORK],
- ['<', 'deadline_rework', $date],
- ])
- ->andWhere(['not', ['user_id' => null]])*/
- ->all();
- unset($query, $date);
- if (!empty($jobs)) {
- $i = 1;
- foreach ($jobs as $key => $job) {
- echo $i++ . " | id: {$job['id']} | ";
- $transaction = Yii::$app->db->beginTransaction();
- try {
- $jobObject = Job::findOne($job['id']);
- $jobObject->removeUser();
- unset($jobObject);
- // Делаем запись, чтобы в дальнейшем пользователь не мог взять эту задачу
- Blacklist::create($job['id'], $job['user_id']);
- // Записываем логи по задаче
- Log::create($job['user_id'], $job['id'], Log::TYPE_TAKE_AWAY);
- $notifyTitle = Yii::t('app', 'Job time expired');
- $notifyBody = Yii::t('app', 'Job time expired - "{job}"', [
- 'job' => '{job:' . $job['id'] . '}',
- ]);
- // Создаем уведомление для вебмастера что c его задачи автоматом сняли пользователя
- UserNotification::create($job['creator_id'], $notifyTitle, $notifyBody);
- // Создаем уведомление для исполнителя что у него забрали задачу автоматом
- UserNotification::create($job['user_id'], $notifyTitle, $notifyBody);
- echo 'success | ';
- $transaction->commit();
- } catch (\Exception $e) {
- $transaction->rollBack();
- throw new ConsoleException($e->getMessage());
- }
- unset($transaction, $jobs[$key], $job);
- echo "\n";
- }
- } else
- echo "Jobs is empty...\n";
- unset($jobs);
- $memory = memory_get_usage() - $memory;
- echo "Total memory: {$memory}\n";
- }
- /**
- * Автоматическое принятие задач при просрочки проверки
- */
- public function actionApproveByDeadline()
- {
- $memory = memory_get_usage();
- $date = date('Y-m-d H:i:s');
- $query = Job::find()->where(['and',
- ['status' => Job::STATUS_VERIFIED],
- ['<', 'deadline_verified', $date],
- ]);
- $count = $query->count('id');
- if ($count > 0) {
- $pagination = new Pagination([
- 'totalCount' => $count,
- 'defaultPageSize' => 1000,
- ]);
- unset($count);
- $part = 0;
- $i = 1;
- while ($part < $pagination->pageCount) {
- echo "Part - {$part}:\n";
- $pagination->page = $part;
- $jobs = $query->offset($pagination->offset)
- ->limit($pagination->limit)
- ->all();
- foreach ($jobs as $key => $job) {
- echo $i++ . " | {$job['id']} | ";
- if ($job->approve())
- echo 'approve | ';
- else
- echo 'error : ' . print_r($job->errors, true);
- echo "\n";
- unset($job, $jobs[$key]);
- }
- $part++;
- }
- } else
- echo "Jobs is empty...\n";
- $memory = memory_get_usage() - $memory;
- echo "Total memory: {$memory}\n";
- }
- /**
- * Автоматическое принятие
- */
- public function actionAutoApprove()
- {
- $memory = memory_get_usage();
- $query = Job::find()->where([
- 'status' => Job::STATUS_VERIFIED
- ]);
- $count = $query->count('id');
- if ($count > 0) {
- $pagination = new Pagination([
- 'totalCount' => $count,
- 'defaultPageSize' => 1000,
- ]);
- unset($count);
- $part = 0;
- $i = 1;
- while ($part < $pagination->pageCount) {
- echo "Part - {$part}:\n";
- $pagination->page = $part++;
- $jobs = $query->offset($pagination->offset)
- ->limit($pagination->limit)
- ->all();
- /**
- * @var $job Job
- */
- foreach ($jobs as $key => $job) {
- echo $i++ . " | {$job['id']} | ";
- $verifyTime = strtotime($job->updated_at);
- $timeNow = strtotime(date("Y-m-d H:i:s"));
- $absTime = abs($timeNow - $verifyTime);
- if($absTime > 60 * 60 * 120 ||
- ($job->delivery_up == 1 && $absTime > 60 * 60 * 24) ||
- $job->creator->isAutoApprove()){
- if ($job->approve())
- echo 'approve | ';
- else
- echo 'error : ' . print_r($job->errors, true);
- } else
- echo 'not is auto approve | ';
- echo "\n";
- unset($job, $jobs[$key]);
- }
- }
- } else
- echo "Jobs is empty...\n";
- $memory = memory_get_usage() - $memory;
- echo "Total memory: {$memory}\n";
- }
- /**
- * Скачивание лэндинга
- */
- public function actionPrepareLanding($limit = 5)
- {
- $jobs = Job::find()
- ->where(['status' => Job::STATUS_PREPARE])
- ->limit((int)$limit)
- ->all();
- if (!empty($jobs)) {
- $i = 1;
- foreach ($jobs as $job) {
- echo $i++ . " | id: {$job->id} | ";
- if ($job->isLandingType()) {
- $errorsSave = [];
- foreach ($job->landings as $landing) {
- if (!$landing->download()) {
- $errors = implode("\r\n", $landing->errors);
- $errorsSave[] = "{$landing->url}: {$errors}";
- unset($errors);
- }
- }
- if (empty($errorsSave)) {
- echo 'success | ';
- $structure = $job->structureArray;
- foreach ($job->landings as $landing) {
- if (isset($structure['formField']["single#{$landing->id}"]["landing#{$landing->id}"]))
- $structure['formField']["single#{$landing->id}"]["landing#{$landing->id}"]['options']['download'] = Url::to(['/landing/download', 'id' => $landing->id, 'jobId' => $landing->job->id]);
- if (isset($structure['formField']["group#{$landing->id}"]["landing#{$landing->id}"]))
- $structure['formField']["group#{$landing->id}"]["landing#{$landing->id}"]['options']['download'] = Url::to(['/landing/download', 'id' => $landing->id, 'jobId' => $landing->job->id]);
- if (!empty($landing->data)) {
- if (isset($structure['formField']["single#{$landing->id}"]["landing#{$landing->id}"]))
- $structure['formField']["single#{$landing->id}"]["landing#{$landing->id}"]['data'] = $landing->data;
- if (isset($structure['formField']["group#{$landing->id}"]["landing#{$landing->id}"]))
- $structure['formField']["group#{$landing->id}"]["landing#{$landing->id}"]['data'] = $landing->data;
- }
- }
- $job->structure = $job->original_structure = Json::encode($structure);
- if ($job->isDraftAfterPrepare()) {
- $job->draft_after_prepare = Job::DRAFT_AFTER_PREPARE_NO;
- $job->status = Job::STATUS_DRAFT;
- } else
- $job->status = Job::STATUS_ACTIVATED;
- } else {
- echo 'error | ';
- $job->landing_errors = implode("\r\n", $errorsSave);
- }
- } else {
- echo 'is not landing type | ';
- $job->landing_errors = Yii::t('app', 'Job is not landing type.');
- }
- $job->save(false, ['status', 'updated_at', 'activated_at', 'structure', 'original_structure', 'landing_errors', 'draft_after_prepare']);
- echo "\n";
- if ($job->isActivated()) {
- $job->addInLastJobsCache();
- $job->addJobNoticeCache();
- // $job->sendNoticeNewJobToEmail();
- }
- }
- } else
- echo "Jobs is empty...\n";
- }
- /**
- * Сравнение с Google translate
- */
- public function actionCompareWithGoogle($limit = 5)
- {
- $jobs = Job::find()->with('origLanguage', 'targetLanguage')
- ->where([
- 'deleted' => Job::DELETED_FALSE,
- 'status' => Job::STATUS_VERIFIED,
- ])
- ->andWhere(['google_translate' => null])
- ->orderBy('activated_at ASC, id ASC')
- ->limit($limit)
- ->all();
- if (!empty($jobs)) {
- $i = 1;
- $shingler = new \common\components\Shingler([
- 'sizeShingle' => 3,
- ]);
- foreach ($jobs as $job) {
- var_dump($job->google_translate);
- if ($job->isLandingType()){
- $job->google_translate = 101;
- $job->save(false, ['status', 'google_translate']);
- continue;
- }
- echo $i++ . " | id: {$job->id} | ";
- // Текст вебмастера
- $origText = $job->textFromStructure(1);
- $origText = StringHelper::stripTags($origText);
- $origText = StringHelper::proposals($origText, 4);
- // Текст фрилансера
- $targetText = $job->textFromStructure(0);
- $targetText = StringHelper::stripTags($targetText);
- $targetText = StringHelper::proposals($targetText, 4);
- // Исходный язык
- $sourceLang = $job->origLanguage->code;
- $sourceLang = mb_substr($sourceLang, 0, 2);
- // Целевой язык
- $targetLang = $job->targetLanguage->code;
- $targetLang = mb_substr($targetLang, 0, 2);
- // Делаем запрос в гугл
- $url = 'https://www.googleapis.com/language/translate/v2?' . http_build_query([
- 'key' => 'AIzaSyA1JCMmlzy9UoSJnWuO_FcA8uiZ7hid4Sk',// 'AIzaSyAwG3GcY52AYe3id3fRtRLr7l-I_w1VjL8',
- 'q' => $origText,
- 'source' => $sourceLang,
- 'target' => $targetLang,
- ]);
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- $data = curl_exec($ch);
- $info = curl_getinfo($ch);
- curl_close($ch);
- if (preg_match('/^2/', $info['http_code'])) {
- try {
- /* Пример JSON с Google:
- {
- "data": {
- "translations": [
- {
- "translatedText": "привет мир"
- }
- ]
- }
- }*/
- $data = Json::decode($data);
- } catch (\yii\base\InvalidParamException $e) {
- $data = false;
- }
- if ($data !== false && is_array($data)) {
- if (!empty($data['data']['translations']) && is_array($data['data']['translations'])) {
- $googleText = '';
- foreach ($data['data']['translations'] as $translation)
- $googleText .= implode(' ', $translation) . ' ';
- $googleText = trim($googleText);
- $job->status = Job::STATUS_VERIFIED;
- $percentage = similar_text($targetText, $googleText, $percent);
- $job->google_translate = round($percent,2);//$shingler->compare($targetText, $googleText);
- if ($job->google_translate == 0) {
- $job->google_translate = 101;
- }
- $job->save(false, ['status', 'google_translate']);
- echo "success | result: {$job->google_translate} | ";
- } else
- echo 'Not found translated text in JSON from Google | ';
- } else
- echo 'Error - JSON from google not valid | ';
- } else
- echo "Error - http code from Google {$info['http_code']} | ";
- echo "\n";
- }
- } else
- echo "Jobs is empty...\n";
- }
- public function actionDeallocateByDeadline()
- {
- $memory = memory_get_usage();
- $date = date('Y-m-d H:i:s');
- $query = new Query;
- $jobs = $query->select('id, original_structure, creator_id, user_id')
- ->from(Job::tableName())
- ->where(['status' => Job::STATUS_REWORK])
- ->andWhere(['<', 'deadline_rework', $date])
- ->andWhere(['not', ['user_id' => null]])
- //SELECT `id`, `original_structure`, `creator_id`, `user_id` FROM `job` WHERE
- // `status` IN ('taken','rework') AND `deadline_rework` < '2017-03-09 10:48:20' AND `user_id` IS NOT NULL
- /*
- ->where(['and',
- ['status' => Job::STATUS_TAKEN],
- ['<', 'deadline', $date],
- ])
- ->orWhere(['and',
- ['status' => Job::STATUS_REWORK],
- ['<', 'deadline_rework', $date],
- ])
- ->andWhere(['not', ['user_id' => null]])*/
- ->all();
- unset($query, $date);
- if (!empty($jobs)) {
- $i = 1;
- foreach ($jobs as $key => $job) {
- echo $i++ . " | id: {$job['id']} | ";
- $transaction = Yii::$app->db->beginTransaction();
- try {
- $jobObject = Job::findOne($job['id']);
- //$jobObject->setDeallocatedStatus();
- $jobObject->removeUser(Job::STATUS_DEALLOCATE);
- unset($jobObject);
- // Делаем запись, чтобы в дальнейшем пользователь не мог взять эту задачу
- //Blacklist::create($job['id'], $job['user_id']);
- // Записываем логи по задаче
- Log::create($job['user_id'], $job['id'], Log::TYPE_DEALLOCATE);
- $notifyTitle = Yii::t('app', 'Job time expired');
- $notifyBody = Yii::t('app', 'Job time expired - "{job}"', [
- 'job' => '{job:' . $job['id'] . '}',
- ]);
- // Создаем уведомление для вебмастера что c его задачи автоматом сняли пользователя
- UserNotification::create($job['creator_id'], $notifyTitle, $notifyBody);
- // Создаем уведомление для исполнителя что у него забрали задачу автоматом
- UserNotification::create($job['user_id'], $notifyTitle, $notifyBody);
- echo 'success | ';
- $transaction->commit();
- } catch (\Exception $e) {
- $transaction->rollBack();
- throw new ConsoleException($e->getMessage());
- }
- unset($transaction, $jobs[$key], $job);
- echo "\n";
- }
- } else
- echo "Jobs is empty...\n";
- unset($jobs);
- $memory = memory_get_usage() - $memory;
- echo "Total memory: {$memory}\n";
- }
- public function actionSendNotifications(){
- NoticeHelper::sendNoticeNewJobToEmail();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement