Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.37 KB | None | 0 0
  1. <?php
  2.  
  3. namespace controllers;
  4.  
  5. use classes\Logger\Logger;
  6. use models\BaseModel;
  7. use models\BidsLogModel;
  8. use models\KeywordsModel;
  9.  
  10. class AjaxController extends Controller
  11. {
  12.  
  13. public function __construct()
  14. {
  15. parent::__construct();
  16. }
  17.  
  18. public function GetClients()
  19. {
  20. $campsigns = $this->model->db->exec("SELECT DISTINCT agencyclients.login
  21. FROM bids_changes JOIN campaigns ON (campaigns.id = bids_changes.campaigns_id) JOIN agencyclients ON (campaigns.login = agencyclients.login)
  22. ORDER BY agencyclients.login ASC");
  23. $this->renderJson($campsigns);
  24. }
  25.  
  26. public function GetCampaigns()
  27. {
  28. $campsigns = $this->model->db->exec("SELECT DISTINCT campaigns.id, campaigns.campaign_id, campaigns.name
  29. 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'));
  30. $this->renderJson($campsigns);
  31. }
  32.  
  33. public function GetAdgroups()
  34. {
  35. $adgroups = $this->model->db->exec("SELECT DISTINCT adgroups.id, adgroups.group_id AS adgroup_id, adgroups.name
  36. 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'));
  37. $this->renderJson($adgroups);
  38. }
  39.  
  40. public function GetLogsDatetimes(){
  41. $param = $this->f3->get('PARAMS.param');
  42. $id = $this->f3->get('PARAMS.id');
  43. $where = "(SELECT COUNT(ads.id) FROM ads WHERE ads.adgroups_id = adgroups.id AND ads.state IN ('ON')) > 0";
  44. switch ($param) {
  45. case 'campaigns':
  46. $where .= ' AND bids_changes.campaigns_id = ' . $id;
  47. break;
  48. case 'adgroups':
  49. $where .= ' AND bids_changes.adgroups_id = ' . $id;
  50. break;
  51. }
  52.  
  53. $bidslogModel = new BidsLogModel();
  54.  
  55. $times = $bidslogModel->findAll([
  56. 'select' => "DATE_FORMAT(bids_changes.time, '%H') as timerange",
  57. 'join' => 'adgroups ON (adgroups.id = bids_changes.adgroups_id)',
  58. 'where' => $where,
  59. 'group' => 'timerange',
  60. ]);
  61.  
  62. $dates = $bidslogModel->findAll([
  63. 'select' => "DATE_FORMAT(bids_changes.date, '%d.%m.%Y') as daterange",
  64. 'join' => 'adgroups ON (adgroups.id = bids_changes.adgroups_id)',
  65. 'where' => $where,
  66. 'group' => 'daterange',
  67. 'order' => "DATE_FORMAT(bids_changes.date, '%Y-%d-%m') asc"
  68. ]);
  69. $_response = [];
  70. $_response['times'] = $times;
  71. $_response['dates'] = $dates;
  72. if($this->methodFlag === 'system'){
  73. return $_response;
  74. }else{
  75. $this->renderJson($_response);
  76. return true;
  77. }
  78. }
  79.  
  80. public function changes()
  81. {
  82. $get = $this->f3->get('GET');
  83.  
  84. $where = "(SELECT COUNT(ads.id) FROM ads WHERE ads.adgroups_id = adgroups.id AND ads.state IN ('ON')) > 0";
  85. if(isset($get['filter_tpl']) && is_integer($get['filter_tpl'])){
  86. $date_range = "AND (bids_changes.date BETWEEN CURRENT_DATE() - INTERVAL ".$get['filter_tpl']." DAY AND CURRENT_DATE())";
  87. $time_range = "AND (DATE_FORMAT(bids_changes.time, '%H') BETWEEN '00' AND '23')";
  88. }else {
  89. $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()";
  90. $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";
  91. }
  92. switch ($this->f3->get('PARAMS.param')) {
  93. case 'campaigns':
  94. $where .= ' AND bids_changes.campaigns_id = ' . $this->f3->get('PARAMS.id');
  95. break;
  96. case 'adgroups':
  97. $where .= ' AND bids_changes.adgroups_id = ' . $this->f3->get('PARAMS.id');
  98. break;
  99. }
  100.  
  101. $bidslogModel = new BidsLogModel();
  102. $bidslog = $bidslogModel->findAll([
  103. 'select' => 'bids_changes.date, bids_changes.time, bids_changes.changes',
  104. 'join' => 'adgroups ON (adgroups.id = bids_changes.adgroups_id)',
  105. 'where' => $where . ' ' . $date_range . ' ' . $time_range,
  106. 'order' => "DATE(bids_changes.date) DESC, TIME(bids_changes.time) DESC",
  107. 'limit' => 1000
  108. ]);
  109.  
  110. $this->methodFlag = "system";
  111. $datetimes = $this->GetLogsDatetimes();
  112.  
  113. $response = [];
  114. $response['x'] = [];
  115. $response['times'] = $datetimes['times'];
  116. $response['dates'] = $datetimes['dates'];
  117.  
  118. foreach ($bidslog as $log) {
  119. $changes = json_decode($log['changes'], true);
  120. if (is_null($changes)) continue;
  121.  
  122. if (! in_array($log['date'] . ' ' . $log['time'], $response['x'])) {
  123. $response['x'][]= $log['date'] . ' ' . $log['time'];
  124. }
  125.  
  126. for ($i=0; count($changes) > $i; $i++) {
  127. if (is_null($changes[$i]['keyword']) || is_null($changes[$i]['newbid'])) continue;
  128. $response['keywords'][$changes[$i]['keyword']][] = (float) $changes[$i]['newbid'];
  129. $response['changes'][date('d.m.Y в H:i:s', strtotime($log['date'] . ' ' . $log['time']))][] = $changes[$i];
  130. }
  131. }
  132. $this->renderJson($response);
  133. }
  134.  
  135. public function update()
  136. {
  137. $post = $this->f3->get('POST');
  138. $updating = [];
  139. if (isset($post['first_strategy']) && !empty($post['first_strategy'])) {
  140. array_push($updating, "keywords.strategies = CONCAT_WS(',', $post[first_strategy], SUBSTRING_INDEX(keywords.strategies, ',', -1))");
  141. }
  142. if (isset($post['second_strategy']) && !empty($post['second_strategy'])) {
  143. array_push($updating, "keywords.strategies = CONCAT_WS(',', SUBSTRING_INDEX(keywords.strategies, ',', 1), $post[second_strategy])");
  144. }
  145. if ((isset($post['first_strategy']) && !empty($post['first_strategy'])) && (isset($post['second_strategy']) && !empty($post['second_strategy']))) {
  146. $strategies = $post['first_strategy'] . "," . $post['second_strategy'];
  147. array_push($updating, "keywords.strategies = '$strategies'");
  148. }
  149.  
  150. if (isset($post['maxbet']) && !empty($post['maxbet']) && intval($post['maxbet']) >= 0) {
  151. array_push($updating, 'maxbet = ' . intval($post['maxbet']) * 1000000);
  152. }
  153. if (isset($post['jobtime'])) {
  154. array_push($updating, 'jobtime = ' . $post['jobtime']);
  155. }
  156. if (count($updating) === 0) {
  157. $this->renderJson(['error' => 'Выберите парамерты для обновления.']);
  158. exit;
  159. }
  160. $sql = 'UPDATE keywords JOIN adgroups ON (adgroups.group_id = keywords.adgroup_id) SET ' . join(', ', $updating);
  161. if ($this->f3->get('PARAMS.param') === 'campaigns') {
  162. $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";
  163. } else {
  164. $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";
  165. }
  166. $keywordsModel = new KeywordsModel();
  167. try {
  168. $keywordsModel->execute($sql);
  169. $logger = new Logger('keywords_update.log', 31);
  170. $logger->WriteData($this->f3->SESSION['user']['username'] . ' изменил(а) параметры ставок. SQL-запрос: <pre><code class="sql">' . $sql . '</code></pre>');
  171. } catch (\Exception $exception) {
  172. $this->renderJson(['error' => $exception->getMessage()]);
  173. }
  174. return true;
  175. }
  176.  
  177. public function online()
  178. {
  179. $model = new BaseModel();
  180. $users = $model->mapper('sessions')->select('DISTINCT login', 'login IS NOT NULL AND TIMESTAMP(stamp) >= CURRENT_TIMESTAMP() - INTERVAL 1 MINUTE');
  181. if (count($users) === 0) {
  182. return null;
  183. }
  184. $result = [];
  185. foreach ($users as $user) {
  186. array_push($result, $user->cast()['login']);
  187. }
  188. $this->renderJson($result);
  189. }
  190.  
  191. public function removekeywords(){
  192. $post = $this->f3->get('POST');
  193. $this->model->db->exec("DELETE FROM keywordbids WHERE keywordbids.keyword_id IN (".$post['ids'].")");
  194. $kid = $this->model->db->exec("DELETE FROM keywords WHERE keywords.kid = IN (".$post['ids'].")");
  195. }
  196.  
  197. public function edit_adtext_href(){
  198. $post = $this->f3->get('POST');
  199. $this->model->db->exec("UPDATE ads_text SET href= ? WHERE ads_text.adgroup_id = ?", [$post['adtext_href'],$post['adgroup_id']]);
  200. //print($this->model->db->log());
  201. }
  202.  
  203. public function saveSiteLinks(){
  204. $post = $this->f3->get('POST');
  205. for ($i=0; count($post['Title']) > $i; $i++) {
  206. $sitelinks[] = [
  207. "Title" => $post['Title'][$i],
  208. "Href" => $post['Href'][$i],
  209. "Description" => $post['Description'][$i]
  210. ];
  211. }
  212. print_r($sitelinks);
  213. }
  214. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement