164 lines
5.6 KiB
PHP
164 lines
5.6 KiB
PHP
<?php
|
|
use app\models\register\Collections;
|
|
use app\models\register\CollectionsObjects;
|
|
use app\models\register\Fields;
|
|
use app\models\register\ObjectTags;
|
|
use app\models\RegisterObjects;
|
|
|
|
|
|
|
|
$lg = Yii::$app->language;
|
|
$per_page = 10;
|
|
$page = $_GET['page'] ?? 1;
|
|
|
|
|
|
|
|
/**
|
|
* @var \yii\web\View $this
|
|
*/
|
|
|
|
if (!empty($_GET['advance_filter']) || !empty($_GET['tags'])) {
|
|
|
|
$objects = RegisterObjects::find();
|
|
$objects = $objects->select('register_objects.ref_num')->where(['lib_type' => 1]);
|
|
|
|
$conditionOr = ['or'];
|
|
|
|
|
|
//[ FILTER BY NAME ]
|
|
if (!empty($_GET['object_name'])) {
|
|
$conditionOr[] = ['LIKE', 'name', $_GET['object_name']];
|
|
$conditionOr[] = ['LIKE', 'ts_en_name', $_GET['object_name']];
|
|
}
|
|
|
|
|
|
//[ FILTER BY CREATOR ]
|
|
if (!empty($_GET['created_by'])) {
|
|
$conditionOr[] = ['=', 'created_by', $_GET['created_by']];
|
|
}
|
|
|
|
$conditionAnd = ['and'];
|
|
|
|
//[ FILTER BY YEAR FROM ]
|
|
if (!empty($_GET['year_from'])) {
|
|
$conditionAnd[] = ['>=', 'created_year', $_GET['year_from']];
|
|
}
|
|
|
|
//[ FILTER BY YEAR TO ]
|
|
if (!empty($_GET['year_to'])) {
|
|
$conditionAnd[] = ['<=', 'created_year', $_GET['year_to']];
|
|
}
|
|
|
|
//region [FILTER BY OBJECT FIELDS]
|
|
$fields = Fields::find()->where(['lib_type' => 1, 'active' => 1, 'deleted' => 0])->all();
|
|
$objectFieldIdArray = [];
|
|
$objectFieldValueTextArray = [];
|
|
|
|
foreach ($fields as $field) {
|
|
if (!empty($_GET['name_id_' . $field->id]))
|
|
$objectFieldIdArray[] = $_GET['name_id_' . $field->id];
|
|
if (!empty($_GET['name_text_' . $field->id]))
|
|
$objectFieldValueTextArray[] = $_GET['name_text_' . $field->id];
|
|
}
|
|
|
|
if (sizeof($objectFieldIdArray) > 0 || sizeof($objectFieldValueTextArray) > 0) {
|
|
$objects = $objects->joinWith('registerObjectFields');
|
|
}
|
|
|
|
if(sizeof($objectFieldIdArray) > 0) {
|
|
$conditionOr[] = ['IN', 'value_id', $objectFieldIdArray];
|
|
}
|
|
|
|
if(sizeof($objectFieldValueTextArray) > 0) {
|
|
foreach ($objectFieldValueTextArray as $value) {
|
|
$conditionOr[] = ['=', 'value_text', $value];
|
|
}
|
|
}
|
|
$objectIds = [];
|
|
|
|
if($conditionOr != ['or'] || $conditionAnd != ['and']) {
|
|
$objects = $objects->andWhere($conditionOr ?? [])->andWhere($conditionAnd ?? [])->all();
|
|
foreach ($objects as $object) {
|
|
$objectIds[] = $object->ref_num;
|
|
}
|
|
}
|
|
|
|
if(!empty($_GET['tags'])) {
|
|
$tags = explode('_', $_GET['tags']);
|
|
$objectTags = ObjectTags::find()->where(['IN', 'tag_id', $tags])->andWhere(['active' => 1, 'deleted' => 0])->all();
|
|
foreach ($objectTags as $objectTag) {
|
|
if(!in_array($objectTag->object_id, $objectIds)) {
|
|
$objectIds[] = $objectTag->object_id;
|
|
}
|
|
}
|
|
}
|
|
|
|
$collectionObjectConditionAnd = ['and'];
|
|
$collectionObjectConditionAnd[] = ['IN', 'object_id', $objectIds];
|
|
$collectionObjectConditionAnd[] = ['=', 'deleted', 0];
|
|
$collectionObjectConditionAnd[] = ['=', 'active', 1];
|
|
$collectionObjectConditionAnd[] = ['=', 'published', 1];
|
|
|
|
if (!empty($_GET['partner_id']))
|
|
$collectionObjectConditionAnd[] = ['=', 'partner_id', $_GET['partner_id']];
|
|
|
|
$collectionObjectConditionOr = ['or'];
|
|
$collectionObjectConditionOr[] = ['IS', 'is_payable', NULL];
|
|
$collectionObjectConditionOr[] = ['=', 'is_payable', 0];
|
|
|
|
$collectionObjects = CollectionsObjects::find()->joinWith('collection')
|
|
->where($collectionObjectConditionOr)
|
|
->andWhere($collectionObjectConditionAnd)
|
|
->groupBy('collection_id');
|
|
|
|
$count = $collectionObjects->count();
|
|
if ($count <= $per_page) $page = 1;
|
|
$collectionObjects->offset($per_page * $page - $per_page)->limit($per_page);
|
|
|
|
if (!empty($_GET['order_title'])) {
|
|
$collectionObjects = $collectionObjects->orderBy([$_GET['order_title'] => SORT_ASC]);
|
|
} else {
|
|
$collectionObjects = $collectionObjects->orderBy(['id' => SORT_DESC]);
|
|
}
|
|
//region DISPLAY DATA
|
|
$count = $collectionObjects->count();
|
|
$data = [];
|
|
/** @var CollectionsObjects $collectionObject */
|
|
foreach ($collectionObjects->all() as $collectionObject) {
|
|
$model = new \stdClass();
|
|
|
|
$collection = $collectionObject->collection;
|
|
|
|
$model->title = $lg == 'en' ? ($collection->name_en ?? $collection->name) : $collection->name;
|
|
$model->type = 'collection';
|
|
$model->img = $collection->getImg(\Yii::$app->params['portal'] . '/_public/images/empty-169.png');
|
|
$model->id = $collection->id;
|
|
$data[] = $model;
|
|
}
|
|
header('Content-type: application/json');
|
|
echo json_encode(['data' => $data, 'lastPage' => ceil($count/$per_page), 'count' => $count]);
|
|
//endregion
|
|
} else {
|
|
$collections = Collections::find()->where(['or', ['=', 'is_payable', 0], ['IS', 'is_payable', null]])
|
|
->andWhere(['deleted' => 0, 'active' => 1, 'published' => 1]);
|
|
if (!empty($_GET['partner_id'])) {
|
|
$collections = $collections->andWhere(['partner_id' => $_GET['partner_id']]);
|
|
}
|
|
$collections = $collections->offset($per_page * $page - $per_page)->limit($per_page);
|
|
|
|
$count = $collections->count();
|
|
|
|
|
|
$data = [];
|
|
foreach ($collections->all() as $collection) {
|
|
$model = new \stdClass();
|
|
$model->title = $lg == 'en' ? $collection->name_en ?? $collection->name : $collection->name;
|
|
$model->type = 'collection';
|
|
$model->img = $collection->getImg(\Yii::$app->params['portal'] . '/_public/images/empty-169.png');
|
|
$model->id = $collection->id;
|
|
$data[] = $model;
|
|
}
|
|
header('Content-type: application/json');
|
|
echo json_encode(['data' => $data, 'lastPage' => ceil($count/$per_page), 'count' => $count]);
|
|
}
|