Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace controllers;
- use classes\Logger\Logger;
- use models\BaseModel;
- use models\BidsLogModel;
- use models\KeywordsModel;
- class AjaxController extends Controller
- {
- public function __construct()
- {
- parent::__construct();
- }
- public function GetClients()
- {
- $campsigns = $this->model->db->exec("SELECT DISTINCT agencyclients.login
- FROM bids_changes JOIN campaigns ON (campaigns.id = bids_changes.campaigns_id) JOIN agencyclients ON (campaigns.login = agencyclients.login)
- ORDER BY agencyclients.login ASC");
- $this->renderJson($campsigns);
- }
- public function GetCampaigns()
- {
- $campsigns = $this->model->db->exec("SELECT DISTINCT campaigns.id, campaigns.campaign_id, campaigns.name
- FROM bids_changes JOIN campaigns ON (campaigns.id = bids_changes.campaigns_id) WHERE campaigns.login = ? ORDER BY campaigns.login ASC", $this->f3->get('GET.login'));
- $this->renderJson($campsigns);
- }
- public function GetAdgroups()
- {
- $adgroups = $this->model->db->exec("SELECT DISTINCT adgroups.id, adgroups.group_id AS adgroup_id, adgroups.name
- FROM bids_changes JOIN adgroups ON (adgroups.id = bids_changes.adgroups_id) WHERE bids_changes.campaigns_id = ? ORDER BY adgroups.login ASC", $this->f3->get('GET.id'));
- $this->renderJson($adgroups);
- }
- public function GetLogsDatetimes(){
- $param = $this->f3->get('PARAMS.param');
- $id = $this->f3->get('PARAMS.id');
- $where = "(SELECT COUNT(ads.id) FROM ads WHERE ads.adgroups_id = adgroups.id AND ads.state IN ('ON')) > 0";
- switch ($param) {
- case 'campaigns':
- $where .= ' AND bids_changes.campaigns_id = ' . $id;
- break;
- case 'adgroups':
- $where .= ' AND bids_changes.adgroups_id = ' . $id;
- break;
- }
- $bidslogModel = new BidsLogModel();
- $times = $bidslogModel->findAll([
- 'select' => "DATE_FORMAT(bids_changes.time, '%H') as timerange",
- 'join' => 'adgroups ON (adgroups.id = bids_changes.adgroups_id)',
- 'where' => $where,
- 'group' => 'timerange',
- ]);
- $dates = $bidslogModel->findAll([
- 'select' => "DATE_FORMAT(bids_changes.date, '%d.%m.%Y') as daterange",
- 'join' => 'adgroups ON (adgroups.id = bids_changes.adgroups_id)',
- 'where' => $where,
- 'group' => 'daterange',
- 'order' => "DATE_FORMAT(bids_changes.date, '%Y-%d-%m') asc"
- ]);
- $_response = [];
- $_response['times'] = $times;
- $_response['dates'] = $dates;
- if($this->methodFlag === 'system'){
- return $_response;
- }else{
- $this->renderJson($_response);
- return true;
- }
- }
- public function changes()
- {
- $get = $this->f3->get('GET');
- $where = "(SELECT COUNT(ads.id) FROM ads WHERE ads.adgroups_id = adgroups.id AND ads.state IN ('ON')) > 0";
- if(isset($get['filter_tpl']) && is_integer($get['filter_tpl'])){
- $date_range = "AND (bids_changes.date BETWEEN CURRENT_DATE() - INTERVAL ".$get['filter_tpl']." DAY AND CURRENT_DATE())";
- $time_range = "AND (DATE_FORMAT(bids_changes.time, '%H') BETWEEN '00' AND '23')";
- }else {
- $date_range = (!empty($get['dstart']) && isset($get['dstart']) && !empty($get['dend']) && isset($get['dend'])) ? "AND (bids_changes.date BETWEEN '" . date('Y-m-d', strtotime($get['dstart'])) . "' AND '" . date('Y-m-d', strtotime($get['dend'])) . "')" : "AND bids_changes.date = CURRENT_DATE()";
- $time_range = (!empty($get['tstart']) && isset($get['tstart']) && !empty($get['tend']) && isset($get['tend'])) ? "AND (DATE_FORMAT(bids_changes.time, '%H') BETWEEN '" . $get['tstart'] . "' AND '" . $get['tend'] . "')" : "AND bids_changes.time >= NOW() - INTERVAL 3 HOUR";
- }
- switch ($this->f3->get('PARAMS.param')) {
- case 'campaigns':
- $where .= ' AND bids_changes.campaigns_id = ' . $this->f3->get('PARAMS.id');
- break;
- case 'adgroups':
- $where .= ' AND bids_changes.adgroups_id = ' . $this->f3->get('PARAMS.id');
- break;
- }
- $bidslogModel = new BidsLogModel();
- $bidslog = $bidslogModel->findAll([
- 'select' => 'bids_changes.date, bids_changes.time, bids_changes.changes',
- 'join' => 'adgroups ON (adgroups.id = bids_changes.adgroups_id)',
- 'where' => $where . ' ' . $date_range . ' ' . $time_range,
- 'order' => "DATE(bids_changes.date) DESC, TIME(bids_changes.time) DESC",
- 'limit' => 1000
- ]);
- $this->methodFlag = "system";
- $datetimes = $this->GetLogsDatetimes();
- $response = [];
- $response['x'] = [];
- $response['times'] = $datetimes['times'];
- $response['dates'] = $datetimes['dates'];
- foreach ($bidslog as $log) {
- $changes = json_decode($log['changes'], true);
- if (is_null($changes)) continue;
- if (! in_array($log['date'] . ' ' . $log['time'], $response['x'])) {
- $response['x'][]= $log['date'] . ' ' . $log['time'];
- }
- for ($i=0; count($changes) > $i; $i++) {
- if (is_null($changes[$i]['keyword']) || is_null($changes[$i]['newbid'])) continue;
- $response['keywords'][$changes[$i]['keyword']][] = (float) $changes[$i]['newbid'];
- $response['changes'][date('d.m.Y в H:i:s', strtotime($log['date'] . ' ' . $log['time']))][] = $changes[$i];
- }
- }
- $this->renderJson($response);
- }
- public function update()
- {
- $post = $this->f3->get('POST');
- $updating = [];
- if (isset($post['first_strategy']) && !empty($post['first_strategy'])) {
- array_push($updating, "keywords.strategies = CONCAT_WS(',', $post[first_strategy], SUBSTRING_INDEX(keywords.strategies, ',', -1))");
- }
- if (isset($post['second_strategy']) && !empty($post['second_strategy'])) {
- array_push($updating, "keywords.strategies = CONCAT_WS(',', SUBSTRING_INDEX(keywords.strategies, ',', 1), $post[second_strategy])");
- }
- if ((isset($post['first_strategy']) && !empty($post['first_strategy'])) && (isset($post['second_strategy']) && !empty($post['second_strategy']))) {
- $strategies = $post['first_strategy'] . "," . $post['second_strategy'];
- array_push($updating, "keywords.strategies = '$strategies'");
- }
- if (isset($post['maxbet']) && !empty($post['maxbet']) && intval($post['maxbet']) >= 0) {
- array_push($updating, 'maxbet = ' . intval($post['maxbet']) * 1000000);
- }
- if (isset($post['jobtime'])) {
- array_push($updating, 'jobtime = ' . $post['jobtime']);
- }
- if (count($updating) === 0) {
- $this->renderJson(['error' => 'Выберите парамерты для обновления.']);
- exit;
- }
- $sql = 'UPDATE keywords JOIN adgroups ON (adgroups.group_id = keywords.adgroup_id) SET ' . join(', ', $updating);
- if ($this->f3->get('PARAMS.param') === 'campaigns') {
- $sql .= " WHERE keywords.campaign_id IN (" . $post['ids'] . ") AND (SELECT COUNT(ads.id) FROM ads WHERE ads.adgroups_id = adgroups.id AND ads.state IN ('ON')) > 0";
- } else {
- $sql .= " WHERE keywords.kid IN (" . $post['ids'] . ") AND (SELECT COUNT(ads.id) FROM ads WHERE ads.adgroups_id = adgroups.id AND ads.state IN ('ON')) > 0";
- }
- $keywordsModel = new KeywordsModel();
- try {
- $keywordsModel->execute($sql);
- $logger = new Logger('keywords_update.log', 31);
- $logger->WriteData($this->f3->SESSION['user']['username'] . ' изменил(а) параметры ставок. SQL-запрос: <pre><code class="sql">' . $sql . '</code></pre>');
- } catch (\Exception $exception) {
- $this->renderJson(['error' => $exception->getMessage()]);
- }
- return true;
- }
- public function online()
- {
- $model = new BaseModel();
- $users = $model->mapper('sessions')->select('DISTINCT login', 'login IS NOT NULL AND TIMESTAMP(stamp) >= CURRENT_TIMESTAMP() - INTERVAL 1 MINUTE');
- if (count($users) === 0) {
- return null;
- }
- $result = [];
- foreach ($users as $user) {
- array_push($result, $user->cast()['login']);
- }
- $this->renderJson($result);
- }
- public function removekeywords(){
- $post = $this->f3->get('POST');
- $this->model->db->exec("DELETE FROM keywordbids WHERE keywordbids.keyword_id IN (".$post['ids'].")");
- $kid = $this->model->db->exec("DELETE FROM keywords WHERE keywords.kid = IN (".$post['ids'].")");
- }
- public function edit_adtext_href(){
- $post = $this->f3->get('POST');
- $this->model->db->exec("UPDATE ads_text SET href= ? WHERE ads_text.adgroup_id = ?", [$post['adtext_href'],$post['adgroup_id']]);
- //print($this->model->db->log());
- }
- public function saveSiteLinks(){
- $post = $this->f3->get('POST');
- for ($i=0; count($post['Title']) > $i; $i++) {
- $sitelinks[] = [
- "Title" => $post['Title'][$i],
- "Href" => $post['Href'][$i],
- "Description" => $post['Description'][$i]
- ];
- }
- print_r($sitelinks);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement