Commit 867773562bfae73e2a06e9d78a0429ef4a4f1d6e

Authored by Alex Savenko
1 parent 5de5ebae

кастомные фильды "id", "view_id" для га запросов + валидация для юзера

app/library/App/Controllers/GaController.php
@@ -60,6 +60,7 @@ class GaController extends CrudResourceController { @@ -60,6 +60,7 @@ class GaController extends CrudResourceController {
60 if (!empty($view_id)) { 60 if (!empty($view_id)) {
61 $result[] = $this->sendGaRequest( 61 $result[] = $this->sendGaRequest(
62 $project->name, 62 $project->name,
  63 + $project->id,
63 $view_id, 64 $view_id,
64 $get_metrics, 65 $get_metrics,
65 $get_dimensions, 66 $get_dimensions,
@@ -80,6 +81,7 @@ class GaController extends CrudResourceController { @@ -80,6 +81,7 @@ class GaController extends CrudResourceController {
80 ]); 81 ]);
81 $result = $this->sendGaRequest( 82 $result = $this->sendGaRequest(
82 $project->name, 83 $project->name,
  84 + $project->id,
83 $view_id, 85 $view_id,
84 $get_metrics, 86 $get_metrics,
85 $get_dimensions, 87 $get_dimensions,
@@ -101,6 +103,7 @@ class GaController extends CrudResourceController { @@ -101,6 +103,7 @@ class GaController extends CrudResourceController {
101 * Send request to Google Analytics Reporting API 103 * Send request to Google Analytics Reporting API
102 * 104 *
103 * @param string $project_name 105 * @param string $project_name
  106 + * @param int $project_id
104 * @param string $view 107 * @param string $view
105 * @param string $get_metrics 108 * @param string $get_metrics
106 * @param string $get_dimensions 109 * @param string $get_dimensions
@@ -113,7 +116,7 @@ class GaController extends CrudResourceController { @@ -113,7 +116,7 @@ class GaController extends CrudResourceController {
113 * @param int $max_result 116 * @param int $max_result
114 * @return array 117 * @return array
115 */ 118 */
116 - public function sendGaRequest($project_name, $view, $get_metrics, $get_dimensions, $start, $end, $chart = false, $filter_expression = null, $sort = null, $sort_type = 'desc', $max_result = 50000) { 119 + public function sendGaRequest($project_name, $project_id, $view, $get_metrics, $get_dimensions, $start, $end, $chart = false, $filter_expression = null, $sort = null, $sort_type = 'desc', $max_result = 50000) {
117 120
118 putenv('GOOGLE_APPLICATION_CREDENTIALS=/var/www/phalcon/'.self::SECRET_JSON); 121 putenv('GOOGLE_APPLICATION_CREDENTIALS=/var/www/phalcon/'.self::SECRET_JSON);
119 $client = new Google_Client(); 122 $client = new Google_Client();
@@ -199,12 +202,12 @@ class GaController extends CrudResourceController { @@ -199,12 +202,12 @@ class GaController extends CrudResourceController {
199 $response = $response->toSimpleObject(); 202 $response = $response->toSimpleObject();
200 $response = $response->reports[0]['data']['rows']; 203 $response = $response->reports[0]['data']['rows'];
201 204
  205 + $custom_fields = ['name' => $project_name, 'view_id' => (int)$view, 'id' => $project_id];
202 if ($chart) { 206 if ($chart) {
203 - //$result = self::responseChartTransform($response, $project_name);  
204 - $result = self::responseDataTransform($response, $iterations, $request_dim, $project_name); 207 + $result = self::responseDataTransform($response, $iterations, $request_dim, $custom_fields);
205 $result = self::chartTransform($result); 208 $result = self::chartTransform($result);
206 } else { 209 } else {
207 - $result = self::responseDataTransform($response, $iterations, $request_dim, $project_name); 210 + $result = self::responseDataTransform($response, $iterations, $request_dim, $custom_fields);
208 } 211 }
209 212
210 return $result; 213 return $result;
@@ -217,10 +220,10 @@ class GaController extends CrudResourceController { @@ -217,10 +220,10 @@ class GaController extends CrudResourceController {
217 * @param array $response 220 * @param array $response
218 * @param int $iterations 221 * @param int $iterations
219 * @param string $request_dimension 222 * @param string $request_dimension
220 - * @param string $project_name 223 + * @param array $custom_fields
221 * @return array 224 * @return array
222 */ 225 */
223 - public static function responseDataTransform(array $response, $iterations, $request_dimension, $project_name) { 226 + public static function responseDataTransform(array $response, $iterations, $request_dimension, $custom_fields) {
224 227
225 $result = []; 228 $result = [];
226 $int_query = true; 229 $int_query = true;
@@ -290,7 +293,9 @@ class GaController extends CrudResourceController { @@ -290,7 +293,9 @@ class GaController extends CrudResourceController {
290 /** --------------------------------- **/ 293 /** --------------------------------- **/
291 294
292 /** ----- add custom fields ------ **/ 295 /** ----- add custom fields ------ **/
293 - $result['name'] = $project_name ?? 'Неизвестный проект'; 296 + foreach ($custom_fields as $key => $value) {
  297 + $result[$key] = $value ?? 'Неизвестный';
  298 + }
294 /** ------------------------------ **/ 299 /** ------------------------------ **/
295 300
296 return $result; 301 return $result;
@@ -304,22 +309,31 @@ class GaController extends CrudResourceController { @@ -304,22 +309,31 @@ class GaController extends CrudResourceController {
304 * @return array 309 * @return array
305 */ 310 */
306 public static function chartTransform(array $data) { 311 public static function chartTransform(array $data) {
  312 +
307 $result = []; 313 $result = [];
  314 +
308 foreach ($data as $key => $value) { 315 foreach ($data as $key => $value) {
309 - if ($key === 'name') {  
310 - $result[$key] = $value;  
311 - }  
312 - else {  
313 - if (!is_array($value)) {  
314 - $result['data'][] = $value; 316 +
  317 + /** Skip custom field **/
  318 + if ($key === 'name' || $key == 'id' || $key == 'view_id') {
  319 + $result[$key] = $value;
315 } 320 }
  321 + /** ---------------- **/
  322 +
  323 + /** Remove keys and add 'data' array **/
316 else { 324 else {
317 - foreach ($value as $v_key => $v_value) {  
318 - $result['data'][$key][$v_key] = $v_value; 325 + if (!is_array($value)) {
  326 + $result['data'][] = $value;
  327 + }
  328 + else {
  329 + foreach ($value as $v_key => $v_value) {
  330 + $result['data'][$key][$v_key] = $v_value;
  331 + }
  332 + ksort($result['data'][$key]);
319 } 333 }
320 - ksort($result['data'][$key]);  
321 } 334 }
322 - } 335 + /** ------------------------------- **/
  336 +
323 } 337 }
324 338
325 return $result; 339 return $result;
app/library/App/Controllers/UserController.php
@@ -107,26 +107,4 @@ class UserController extends CrudResourceController @@ -107,26 +107,4 @@ class UserController extends CrudResourceController
107 } 107 }
108 } 108 }
109 109
110 - protected function postDataValid($data, $isUpdate)  
111 - {  
112 - //проверка на регистрацию существующего username  
113 - $input_name = $data['username'];  
114 - if (!$isUpdate)  
115 - {  
116 - $same_user = User::find(["username = '".$input_name."'"]);  
117 - if (isset($same_user[0]) && !empty($same_user[0]->username))  
118 - {  
119 - return false;  
120 - }  
121 - }  
122 -  
123 - return true;  
124 - }  
125 -  
126 - protected function onDataInvalid($data)  
127 - {  
128 - $msg = 'Post-data is invalid, trying to use non-unique value of `username`';  
129 - throw new Exception(ErrorCodes::POST_DATA_INVALID, $msg, ['data' => $data]);  
130 - }  
131 -  
132 } 110 }
133 \ No newline at end of file 111 \ No newline at end of file
app/library/App/Model/User.php
@@ -2,12 +2,19 @@ @@ -2,12 +2,19 @@
2 2
3 namespace App\Model; 3 namespace App\Model;
4 4
5 -class User extends \App\Mvc\DateTrackingModel 5 +use App\Constants\AclRoles;
  6 +use App\Mvc\DateTrackingModel;
  7 +use App\User\Service;
  8 +use PhalconApi\Exception;
  9 +use PhalconApi\Constants\ErrorCodes;
  10 +
  11 +class User extends DateTrackingModel
6 { 12 {
7 public $id; 13 public $id;
8 public $role; 14 public $role;
9 public $username; 15 public $username;
10 public $password; 16 public $password;
  17 + public $email;
11 18
12 public function getSource() 19 public function getSource()
13 { 20 {
@@ -32,4 +39,70 @@ class User extends \App\Mvc\DateTrackingModel @@ -32,4 +39,70 @@ class User extends \App\Mvc\DateTrackingModel
32 ]); 39 ]);
33 } 40 }
34 41
  42 + public function getUsername()
  43 + {
  44 + return $this->username;
  45 + }
  46 +
  47 + public function setUsername($username)
  48 + {
  49 + /** validation: unique, non-empty, 4+ letters **/
  50 + $same_user = User::find(["username = '".$username."'"]);
  51 + if (isset($same_user[0]) && !empty($same_user[0]->username))
  52 + {
  53 + $msg = 'Post-data is invalid, trying to use non-unique value `'.$username.'` of `username`';
  54 + throw new Exception(ErrorCodes::POST_DATA_INVALID, $msg, ['username' => $username]);
  55 + }
  56 + elseif (empty($username))
  57 + {
  58 + $msg = 'Post-data is invalid, trying to use empty value of `username`';
  59 + throw new Exception(ErrorCodes::POST_DATA_INVALID, $msg, ['username' => $username]);
  60 + }
  61 + elseif (strlen($username) < 4)
  62 + {
  63 + $msg = 'Post-data is invalid, value of `username` should be more than 4 letters';
  64 + throw new Exception(ErrorCodes::POST_DATA_INVALID, $msg, ['username' => $username]);
  65 + }
  66 + /** ---------------------------------------- **/
  67 +
  68 + $this->username = $username;
  69 + }
  70 +
  71 + public function getEmail()
  72 + {
  73 + return $this->email;
  74 + }
  75 +
  76 + public function setEmail($email)
  77 + {
  78 + /** validation: FILTER_VALIDATE_EMAIL **/
  79 + if (!filter_var($email, FILTER_VALIDATE_EMAIL))
  80 + {
  81 + $msg = 'Post-data is invalid, bad email value';
  82 + throw new Exception(ErrorCodes::POST_DATA_INVALID, $msg, ['email' => $email]);
  83 + }
  84 + /** ---------- */
  85 +
  86 + $this->email = $email;
  87 + }
  88 +
  89 + public function getRole()
  90 + {
  91 + $service = new Service();
  92 + return $service->getRole();
  93 + }
  94 +
  95 + public function setRole($role)
  96 + {
  97 + /** validation: constant value **/
  98 + if (!in_array($role, AclRoles::ALL_ROLES))
  99 + {
  100 + $msg = 'Post-data is invalid, bad `role` value';
  101 + throw new Exception(ErrorCodes::POST_DATA_INVALID, $msg, ['role' => $role]);
  102 + }
  103 + /** -------------------------- **/
  104 +
  105 + $this->role = $role;
  106 + }
  107 +
35 } 108 }