diff --git a/app/MergadoModels/MergadoApiModel.php b/app/MergadoModels/MergadoApiModel.php index 4819e8b..63e8a28 100755 --- a/app/MergadoModels/MergadoApiModel.php +++ b/app/MergadoModels/MergadoApiModel.php @@ -2,16 +2,164 @@ namespace App\MergadoModels; - +use ArrayAccess; +use Illuminate\Contracts\Support\Arrayable; +use Illuminate\Contracts\Support\Jsonable; use Illuminate\Support\Facades\Session; +use Illuminate\Support\Str; +use JsonSerializable; use MergadoClient\ApiClient; -abstract class MergadoApiModel { +abstract class MergadoApiModel +// implements ArrayAccess, Arrayable, Jsonable, JsonSerializable +{ protected $api; + const NEED_ID = true; + +// protected + + public function __construct($attributes = [], $token = null) + { + if($attributes) { + $this->populate($attributes); + } + + if($token){ + $this->api = new ApiClient($token, env('MODE')); + } elseif (Session::get("oauth")) { + $this->api = new ApiClient(Session::get('oauth')->getToken(), env('MODE')); + } else { + $this->api = null; + } + + } + + public function setToken($token) { + if($this->api) { + $this->api->setToken($token); + } else { + $this->api = new ApiClient($token, env('MODE')); + } + } + + public static function apiClient() { + return new ApiClient(Session::get('oauth')->getToken(), env('MODE')); + } + + protected function getPublicProperties() + { + $props = (new \ReflectionObject($this))->getProperties(\ReflectionProperty::IS_PUBLIC); + $returnArray = []; + foreach ($props as $prop) { + array_push($returnArray, $prop->name); + } + return $returnArray; + } + + public static function hydrate($collection = []) + { + $type = gettype($collection); + $returnArray = []; + + if ($type == "object") { + $collection = (array) $collection; + } elseif (!$type == "array") { + return $returnArray; + } + + if (static::NEED_ID) { + foreach ($collection as $atributes) { + $instance = new static($atributes->id, $atributes); + array_push($returnArray, $instance); + } + } else { + foreach ($collection as $atributes) { + $instance = new static($atributes); + array_push($returnArray, $instance); + } + } + + return $returnArray; + } + + protected function populate($atributes) + { + $type = gettype($atributes); + + if ($type == "object") { + $atributes = (array) $atributes; + } elseif (!$type == "array") { + return $this; + } + + foreach ($atributes as $key => $value) { + $this->setAttribute($key, $value); + } + + return $this; + } + + /** + * Get the fillable attributes of a given array. + * + * @param array $attributes + * @return array + */ + protected function fillableFromArray(array $attributes) + { + + $properties = static::getPublicProperties(); + return array_intersect_key($attributes, array_flip($properties)); + + } + + /** + * Determine if a set mutator exists for an attribute. + * + * @param string $key + * @return bool + */ + public function hasSetMutator($key) + { + return method_exists($this, 'set'.$this->camelCase($key)); + } + + protected function camelCase($string) { + $value = ucwords(str_replace(['-', '_'], ' ', $string)); + + return str_replace(' ', '', $value); + } + + /** + * Set a given attribute on the model. + * + * @param string $key + * @param mixed $value + * @return $this + */ + public function setAttribute($key, $value) + { + // First we will check for the presence of a mutator for the set operation + // which simply lets the developers tweak the attribute as it is set on + // the model, such as "json_encoding" an listing of data for storage. + if ($this->hasSetMutator($key)) { + $method = 'set'.$this->camelCase($key).'Attribute'; + + return $this->{$method}($value); + } + + $this->{$key} = $value; + + return $this; + } + + public function stripNullProperties() { + $object = (object) array_filter((array) $this, function ($val) { + return !is_null($val); + }); - public function __construct() { - $this->api = new ApiClient(Session::get('oauth')->getToken(), env('MODE')); + return $object; } } \ No newline at end of file diff --git a/app/MergadoModels/ProjectModel.php b/app/MergadoModels/ProjectModel.php index 7892d2f..0d9fac0 100755 --- a/app/MergadoModels/ProjectModel.php +++ b/app/MergadoModels/ProjectModel.php @@ -9,12 +9,368 @@ namespace App\MergadoModels; -class ProjectModel extends MergadoApiModel { +use App\Models\Query; +use App\Models\Rule; +use App\Models\RuleQuery; +class ProjectModel extends MergadoApiModel +{ - public function get($projectId) { - return $this->api->projects($projectId)->get(); - } +// protected $projectId; + + public $id; + public $shop_id; + public $creator_id; + public $name; + public $url; + public $activated; + public $created; + public $exported_items; + public $input_format; + public $output_format; + public $pairing_elements; + public $read_only; + public $rules_changed_at; + public $sklik_context; + public $sklik_search; + public $slug; + public $turned_off; + public $update_period; + public $last_access; + public $xml_synced_at; + public $xml_updated_at; + public $type; + + const NEED_ID = true; + + public function __construct($projectId, $attributes = [], $token = null) + { + $this->id = $projectId; + parent::__construct($attributes, $token); + } + + /** + * @param array $fields + * @return mixed + */ + public function get(array $fields = []) + { + $prepared = $this->api->projects($this->id); + + if (!empty($fields)) { + $prepared = $prepared->fields($fields); + } + + $fromApi = $prepared->get(); + + $this->populate($fromApi); + + return $this; + } + + + public static function getTypefromApi($projectId) + { + $model = new static($projectId); + + $model->get(["output_format"]); + + return $model->getType(); + } + + public function getType() + { + $outputFormat = $this->output_format; + + switch ($outputFormat) { + case preg_match('/\bfacebook?\b/', $outputFormat): + return "facebook"; + case "heureka.cz": + return "heureka"; + case "heureka.sk": + return "heureka"; + case "zbozi.cz": + return "zbozi"; + default: + return null; + } + } + + /** + * @param int $limit + * @param int $offset + * @param array $fields + * @return mixed + */ + public function getQueries($limit = 10, $offset = 0, array $fields = []) + { + $prepared = $this->api->projects($this->id)->queries->limit($limit)->offset($offset); + + if (!empty($fields)) { + $prepared = $prepared->fields($fields); + } + + $queries = $prepared->get()->data; + + $queries = QueryModel::hydrate($queries); + + return $queries; + } + + /** + * @param int $limit + * @param int $offset + * @param array $fields + * @return mixed + */ + public function getNamedQueries(array $fields = []) + { + $prepared = $this->api->projects($this->id)->queries->limit(300)->offset(0); + + if (!empty($fields)) { + $prepared = $prepared->fields($fields); + } + + $queries = $prepared->get()->data; + + $namedQueries = array_filter($queries, function ($query) { + return !is_null($query->name); + }); + + $queries = QueryModel::hydrate($namedQueries); + + foreach ($queries as $query) { + if ($query->name == "♥ALLPRODUCTS♥") { + $query->name = trans('fie.all_products'); + break; + } + } + + return $queries; + } + + /** + * @param int $limit + * @param int $offset + * @param array $fields + * @return mixed + */ + public function getRules($limit = 10, $offset = 0, array $fields = []) + { + $prepared = $this->api->projects($this->id)->rules->limit($limit)->offset($offset); + + if (!empty($fields)) { + $prepared = $prepared->fields($fields); + } + + $fromApi = $prepared->get()->data; + + $rules = RuleModel::hydrate($fromApi); + + return $rules; + } + + /** + * @param $rule + * @param array $attributes (additional attributes that are save to local database) + * @return mixed + */ + public function createRule($rule, $attributes = []) + { + $fromApi = $this->api->projects($this->id)->rules()->post($rule); + + $newRule = new RuleModel($fromApi->id, $fromApi); + + $localRule = array_add((array)$fromApi, 'element_id', $newRule->project_element_id); + $localRule = array_merge($localRule, $attributes); + Rule::create($localRule); + + return $newRule; + } + + public function createRuleWithQueries($rule, array $queries) + { + $fromApi = (object)$this->api->projects($this->id)->rules()->post($rule); + + $newRule = new RuleModel($fromApi->id, $fromApi); + Rule::create(array_add((array)$newRule, 'element_id', $newRule->project_element_id)); + + return $newRule; + } + + public function createQuery($query, $tag = null) + { + $fromApi = $this->api->projects($this->id)->queries()->post($query); + + $newRule = new QueryModel($fromApi->id, $fromApi); + + if ($tag) { + Query::create(array_merge((array)$newRule, ["tag" => $tag])); + } else { + Query::create((array)$newRule); + } + + return $newRule; + } + + /** + * @param int $limit + * @param int $offset + * @param array $fields + * @return mixed + */ + public function getElements($limit = 10, $offset = 0, array $fields = []) + { + + $prepared = $this->api->projects($this->id)->elements->limit($limit)->offset($offset); + + if (!empty($fields)) { + $prepared = $prepared->fields($fields); + } + + return $prepared->get()->data; + } + + public function createElement($element) + { + return $this->api->projects($this->id)->elements->post($element); + } + + public function setOutputFormatAttribute($value) + { + $this->output_format = $value; + $this->type = $this->getType(); + } + + public function fetchAndSaveAllProductsQuery() + { + $queries = $this->getQueries(200, 0); + $allProducts = null; + + foreach ($queries as $query) { + if ($query->name == "♥ALLPRODUCTS♥") { + $allProducts = $query; + break; + } + } + + if (!$allProducts) return false; + + Query::updateOrCreate([ + "id" => $allProducts->id, + "name" => $allProducts->name, + "project_id" => $allProducts->project_id, + "query" => $allProducts->query, + "read_only" => $allProducts->read_only, + "search_output" => $allProducts->search_output, + "tag" => "all" + ]); + + return $allProducts; + } + + /** + * @param int $limit + * @param int $offset + * @param array $fields + * @return mixed + */ + public function getProducts($limit = 10, $offset = 0, array $fields = []) + { + $prepared = $this->api->projects($this->id)->products->limit($limit)->offset($offset); + + if (!empty($fields)) { + $prepared = $prepared->fields($fields); + } + + return $prepared->get()->data; + } + + /** + * @param $projectId + * @param int $limit + * @param int $offset + * @param array $fields + * @param null $date + * @return mixed + */ + public function getAllProductsStats($limit = 10, $offset = 0, array $fields = [], $date = null) + { + $prepared = $this->api->projects($this->id)->stats->products->limit($limit)->offset($offset); + + if (!empty($fields)) { + $prepared = $prepared->fields($fields); + } + + if ($date) { + $prepared = $prepared->param("date", $date); + } + + $stats = $prepared->get()->data; + + return $stats; + } + + /** + * @param $projectId + * @param array $itemIds + * @param array $fields + * @param null $date + * @return mixed + */ + public function getAllProductsStatsByIds(array $itemIds = [], array $fields = [], $date = null) + { + $prepared = $this->api->projects($this->id)->stats->products; + + $postData = []; + + if (!empty($itemIds)) { + $postData["filter_by"] = ["item_id__in" => $itemIds]; + } + + if (!empty($fields)) { + $postData["fields"] = $fields; + } + + if ($date) { + $postData["date"] = $date; + } + + $stats = $prepared->post($postData)->data; + + return $stats; + } + + public function getGoogleAnalytics($limit = 10, $offset = 0, array $fields = [], $dimensions = [], $metrics = [], $startDate = null, $endDate = null) + { + $prepared = $this->api->projects($this->id)->google->analytics->limit($limit)->offset($offset); + + if (!empty($fields)) { + $prepared = $prepared->fields($fields); + } + + if ($startDate) { + $prepared = $prepared->param("start_date", $startDate); + } + + if ($endDate) { + $prepared = $prepared->param("end_date", $endDate); + } + + if ($dimensions) { + $dimensions = implode(',', $dimensions); + $prepared = $prepared->param("dimensions", $dimensions); + } + + if($metrics) { + $metrics = implode(',', $metrics); + $prepared = $prepared->param("metrics", $metrics); + } + + $analytics = $prepared->get()->data; + + return $analytics; + } } \ No newline at end of file diff --git a/app/Models/AccessToken.php b/app/Models/AccessToken.php old mode 100755 new mode 100644 diff --git a/resources/views/project/logs/show.blade.php b/resources/views/project/logs/show.blade.php index 3d3252f..6c309b1 100755 --- a/resources/views/project/logs/show.blade.php +++ b/resources/views/project/logs/show.blade.php @@ -39,7 +39,7 @@ {{----}} {{----}} - {{--@if($user->id == session('oauth')->getResourceOwnerId())--}} + {{--@if($user->id == session('oauth')->getUserId())--}} {{--