From 867773562bfae73e2a06e9d78a0429ef4a4f1d6e Mon Sep 17 00:00:00 2001 From: Alex Date: Tue, 21 Mar 2017 13:37:38 +0200 Subject: [PATCH] кастомные фильды "id", "view_id" для га запросов + валидация для юзера --- app/library/App/Controllers/GaController.php | 48 +++++++++++++++++++++++++++++++----------------- app/library/App/Controllers/UserController.php | 22 ---------------------- app/library/App/Model/User.php | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 105 insertions(+), 40 deletions(-) diff --git a/app/library/App/Controllers/GaController.php b/app/library/App/Controllers/GaController.php index 7b4a948..4e73a6b 100644 --- a/app/library/App/Controllers/GaController.php +++ b/app/library/App/Controllers/GaController.php @@ -60,6 +60,7 @@ class GaController extends CrudResourceController { if (!empty($view_id)) { $result[] = $this->sendGaRequest( $project->name, + $project->id, $view_id, $get_metrics, $get_dimensions, @@ -80,6 +81,7 @@ class GaController extends CrudResourceController { ]); $result = $this->sendGaRequest( $project->name, + $project->id, $view_id, $get_metrics, $get_dimensions, @@ -101,6 +103,7 @@ class GaController extends CrudResourceController { * Send request to Google Analytics Reporting API * * @param string $project_name + * @param int $project_id * @param string $view * @param string $get_metrics * @param string $get_dimensions @@ -113,7 +116,7 @@ class GaController extends CrudResourceController { * @param int $max_result * @return array */ - 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) { + 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) { putenv('GOOGLE_APPLICATION_CREDENTIALS=/var/www/phalcon/'.self::SECRET_JSON); $client = new Google_Client(); @@ -199,12 +202,12 @@ class GaController extends CrudResourceController { $response = $response->toSimpleObject(); $response = $response->reports[0]['data']['rows']; + $custom_fields = ['name' => $project_name, 'view_id' => (int)$view, 'id' => $project_id]; if ($chart) { - //$result = self::responseChartTransform($response, $project_name); - $result = self::responseDataTransform($response, $iterations, $request_dim, $project_name); + $result = self::responseDataTransform($response, $iterations, $request_dim, $custom_fields); $result = self::chartTransform($result); } else { - $result = self::responseDataTransform($response, $iterations, $request_dim, $project_name); + $result = self::responseDataTransform($response, $iterations, $request_dim, $custom_fields); } return $result; @@ -217,10 +220,10 @@ class GaController extends CrudResourceController { * @param array $response * @param int $iterations * @param string $request_dimension - * @param string $project_name + * @param array $custom_fields * @return array */ - public static function responseDataTransform(array $response, $iterations, $request_dimension, $project_name) { + public static function responseDataTransform(array $response, $iterations, $request_dimension, $custom_fields) { $result = []; $int_query = true; @@ -290,7 +293,9 @@ class GaController extends CrudResourceController { /** --------------------------------- **/ /** ----- add custom fields ------ **/ - $result['name'] = $project_name ?? 'Неизвестный проект'; + foreach ($custom_fields as $key => $value) { + $result[$key] = $value ?? 'Неизвестный'; + } /** ------------------------------ **/ return $result; @@ -304,22 +309,31 @@ class GaController extends CrudResourceController { * @return array */ public static function chartTransform(array $data) { + $result = []; + foreach ($data as $key => $value) { - if ($key === 'name') { - $result[$key] = $value; - } - else { - if (!is_array($value)) { - $result['data'][] = $value; + + /** Skip custom field **/ + if ($key === 'name' || $key == 'id' || $key == 'view_id') { + $result[$key] = $value; } + /** ---------------- **/ + + /** Remove keys and add 'data' array **/ else { - foreach ($value as $v_key => $v_value) { - $result['data'][$key][$v_key] = $v_value; + if (!is_array($value)) { + $result['data'][] = $value; + } + else { + foreach ($value as $v_key => $v_value) { + $result['data'][$key][$v_key] = $v_value; + } + ksort($result['data'][$key]); } - ksort($result['data'][$key]); } - } + /** ------------------------------- **/ + } return $result; diff --git a/app/library/App/Controllers/UserController.php b/app/library/App/Controllers/UserController.php index 322f08a..94febf9 100755 --- a/app/library/App/Controllers/UserController.php +++ b/app/library/App/Controllers/UserController.php @@ -107,26 +107,4 @@ class UserController extends CrudResourceController } } - protected function postDataValid($data, $isUpdate) - { - //проверка на регистрацию существующего username - $input_name = $data['username']; - if (!$isUpdate) - { - $same_user = User::find(["username = '".$input_name."'"]); - if (isset($same_user[0]) && !empty($same_user[0]->username)) - { - return false; - } - } - - return true; - } - - protected function onDataInvalid($data) - { - $msg = 'Post-data is invalid, trying to use non-unique value of `username`'; - throw new Exception(ErrorCodes::POST_DATA_INVALID, $msg, ['data' => $data]); - } - } \ No newline at end of file diff --git a/app/library/App/Model/User.php b/app/library/App/Model/User.php index 29d9d80..8be8fb5 100755 --- a/app/library/App/Model/User.php +++ b/app/library/App/Model/User.php @@ -2,12 +2,19 @@ namespace App\Model; -class User extends \App\Mvc\DateTrackingModel +use App\Constants\AclRoles; +use App\Mvc\DateTrackingModel; +use App\User\Service; +use PhalconApi\Exception; +use PhalconApi\Constants\ErrorCodes; + +class User extends DateTrackingModel { public $id; public $role; public $username; public $password; + public $email; public function getSource() { @@ -32,4 +39,70 @@ class User extends \App\Mvc\DateTrackingModel ]); } + public function getUsername() + { + return $this->username; + } + + public function setUsername($username) + { + /** validation: unique, non-empty, 4+ letters **/ + $same_user = User::find(["username = '".$username."'"]); + if (isset($same_user[0]) && !empty($same_user[0]->username)) + { + $msg = 'Post-data is invalid, trying to use non-unique value `'.$username.'` of `username`'; + throw new Exception(ErrorCodes::POST_DATA_INVALID, $msg, ['username' => $username]); + } + elseif (empty($username)) + { + $msg = 'Post-data is invalid, trying to use empty value of `username`'; + throw new Exception(ErrorCodes::POST_DATA_INVALID, $msg, ['username' => $username]); + } + elseif (strlen($username) < 4) + { + $msg = 'Post-data is invalid, value of `username` should be more than 4 letters'; + throw new Exception(ErrorCodes::POST_DATA_INVALID, $msg, ['username' => $username]); + } + /** ---------------------------------------- **/ + + $this->username = $username; + } + + public function getEmail() + { + return $this->email; + } + + public function setEmail($email) + { + /** validation: FILTER_VALIDATE_EMAIL **/ + if (!filter_var($email, FILTER_VALIDATE_EMAIL)) + { + $msg = 'Post-data is invalid, bad email value'; + throw new Exception(ErrorCodes::POST_DATA_INVALID, $msg, ['email' => $email]); + } + /** ---------- */ + + $this->email = $email; + } + + public function getRole() + { + $service = new Service(); + return $service->getRole(); + } + + public function setRole($role) + { + /** validation: constant value **/ + if (!in_array($role, AclRoles::ALL_ROLES)) + { + $msg = 'Post-data is invalid, bad `role` value'; + throw new Exception(ErrorCodes::POST_DATA_INVALID, $msg, ['role' => $role]); + } + /** -------------------------- **/ + + $this->role = $role; + } + } -- libgit2 0.21.4