Initial import
This commit is contained in:
@@ -0,0 +1,109 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace app\services\api;
|
||||
|
||||
use app\models\Categories;
|
||||
use app\models\CategoriesOt;
|
||||
use app\models\ObjectTemplate;
|
||||
|
||||
class NomenclatureService
|
||||
{
|
||||
public static function categorySelect()
|
||||
{
|
||||
$categories = Categories::find()->where(['IS', 'parent_id', NULL])->all();
|
||||
$keyList = [];
|
||||
foreach ($categories as $category) {
|
||||
foreach ($category->subCategories as $subCategory) {
|
||||
if ($subCategory->name) {
|
||||
$keyList[] = [
|
||||
'id' => $subCategory->id,
|
||||
'name' => $category->name . ' - ' . $subCategory->name,
|
||||
'name_en' => $category->ts_en_name . ' - ' . $subCategory->ts_en_name
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
return $keyList;
|
||||
}
|
||||
|
||||
public static function categoryFilterSelect()
|
||||
{
|
||||
$heritage_type = null;
|
||||
if (!empty($_GET['q'])) {
|
||||
$f = explode('|', $_GET['q']);
|
||||
foreach ($f as $fkv) {
|
||||
$filter = explode(':', $fkv);
|
||||
if($filter[0] == 'heritage_type' && !empty($filter[1]))
|
||||
$heritage_type = $filter[1];
|
||||
}
|
||||
}
|
||||
if($heritage_type) {
|
||||
$ids = [];
|
||||
$objectTemplates = ObjectTemplate::find()->where(['heritage_type' => $heritage_type])->all();
|
||||
foreach ($objectTemplates as $objectTemplate) {
|
||||
$ids[] = $objectTemplate->id;
|
||||
}
|
||||
}
|
||||
$categories = Categories::find()->where(['IS', 'parent_id', NULL])->all();
|
||||
$keyList = [];
|
||||
foreach ($categories as $category) {
|
||||
$subCategoryList = $heritage_type ? $category->getSubCategoriesByTemplate($ids) : $category->subCategories;
|
||||
foreach ($subCategoryList as $subCategory) {
|
||||
if ($subCategory->name) {
|
||||
$keyList[$subCategory->id] = $category->name . ' - ' . $subCategory->name;
|
||||
}
|
||||
}
|
||||
}
|
||||
return $keyList;
|
||||
}
|
||||
|
||||
public static function categoryTree()
|
||||
{
|
||||
$categories = Categories::find()->where(['IS', 'parent_id', NULL])->all();
|
||||
$keyList = [];
|
||||
foreach ($categories as $category) {
|
||||
$sub = [];
|
||||
foreach ($category->subCategories as $subCategory) {
|
||||
if ($subCategory->name) {
|
||||
$sub[] = [
|
||||
'id' => $subCategory->id,
|
||||
'name' => $subCategory->name,
|
||||
'name_en' => $subCategory->ts_en_name
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
$keyList[] = [
|
||||
'id' => $category->id,
|
||||
'name' => $category->name,
|
||||
'name_en' => $category->ts_en_name,
|
||||
'sub' => $sub
|
||||
];
|
||||
}
|
||||
return $keyList;
|
||||
}
|
||||
|
||||
public static function objectTemplates($sub_category_id = null)
|
||||
{
|
||||
$sub = [];
|
||||
if ($sub_category_id)
|
||||
$sub['sc_id'] = $sub_category_id;
|
||||
|
||||
$retrieve = CategoriesOt::find()->where($sub)->all();
|
||||
$data = [];
|
||||
foreach ($retrieve as $item) {
|
||||
$objectTemplate = $item->objectTemplate;
|
||||
$objectTemplateFields = [];
|
||||
foreach ($objectTemplate->objectTemplateFields as $objectTemplateField) {
|
||||
$objectTemplateFields[] = $objectTemplateField->toArray();
|
||||
}
|
||||
$data[] = [
|
||||
'id' => $objectTemplate->id,
|
||||
'name' => $objectTemplate->name,
|
||||
'fields' => $objectTemplateFields
|
||||
];
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
namespace app\services\api;
|
||||
|
||||
class Sync
|
||||
{
|
||||
public function container($action)
|
||||
{
|
||||
if (method_exists($this, $action))
|
||||
return $this->{$action}($this->parse());
|
||||
return $this->execError('Wrong action');
|
||||
}
|
||||
|
||||
private function parse()
|
||||
{
|
||||
$data = json_decode(\Yii::$app->request->getRawBody());
|
||||
if ($data) return $data;
|
||||
return $this->execError('Wrong data format');
|
||||
}
|
||||
|
||||
protected function required($strong_params, $params, $data) {
|
||||
foreach ($strong_params as $param) {
|
||||
if(empty($data->{$param}))
|
||||
if(empty($data->{$param}))
|
||||
return $this->execError("Missing parameter $param");
|
||||
}
|
||||
foreach ($params as $param) {
|
||||
if(!isset($data->{$param}))
|
||||
return $this->execError("Missing parameter $param");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
protected function exist($class, $key, $value) {
|
||||
/** @var \yii\db\ActiveRecord $class */
|
||||
if($class::find()->where([$key => $value])->exists()) {
|
||||
return $this->execError("Record already exists with $key:$value");
|
||||
}
|
||||
}
|
||||
protected function notExist($class, $key, $value) {
|
||||
/** @var \yii\db\ActiveRecord $class */
|
||||
if(!$class::find()->where([$key => $value])->exists()) {
|
||||
return $this->execError("Record does not exists $key:$value");
|
||||
}
|
||||
}
|
||||
|
||||
protected function execError($message)
|
||||
{
|
||||
return ['error' => 1, 'message' => $message];
|
||||
}
|
||||
|
||||
protected function execSuccess()
|
||||
{
|
||||
return['success' => 1];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
<?php
|
||||
|
||||
namespace app\services\api;
|
||||
|
||||
use app\models\Categories;
|
||||
|
||||
class SyncCategory extends Sync
|
||||
{
|
||||
public function main_create($data)
|
||||
{
|
||||
if(!empty($data->{'error'})) return $data;
|
||||
$missing = $this->required(['rr_category_id', 'name'], ['name_en'], $data);
|
||||
if($missing) return $missing;
|
||||
$exits = $this->exist(Categories::class, 'rr_category_id', $data->{'rr_category_id'});
|
||||
if($exits) return $exits;
|
||||
$category = new Categories();
|
||||
$category->rr_category_id = $data->{'rr_category_id'};
|
||||
$category->name = $data->{'name'};
|
||||
$category->ts_en_name = $data->{'name_en'};
|
||||
$category->save();
|
||||
return $this->execSuccess();
|
||||
}
|
||||
public function main_update($data)
|
||||
{
|
||||
if(!empty($data->{'error'})) return $data;
|
||||
$missing = $this->required(['rr_category_id', 'name'], ['name_en'], $data);
|
||||
if($missing) return $missing;
|
||||
$category = Categories::find()->where(['rr_category_id' => $data->{'rr_category_id'}])->one();
|
||||
if($category) {
|
||||
$category->rr_category_id = $data->{'rr_category_id'};
|
||||
$category->name = $data->{'name'};
|
||||
$category->ts_en_name = $data->{'name_en'};
|
||||
$category->save();
|
||||
} else {
|
||||
return $this->execError('Record does not exist rr_category_id:'.$data->{'rr_category_id'});
|
||||
}
|
||||
return $this->execSuccess();
|
||||
}
|
||||
public function main_delete($data)
|
||||
{
|
||||
if(!empty($data->{'error'})) return $data;
|
||||
$missing = $this->required(['rr_category_id'], [], $data);
|
||||
if($missing) return $missing;
|
||||
$category = Categories::find()->where(['rr_category_id' => $data->{'rr_category_id'}])->one();
|
||||
if($category) {
|
||||
$category->delete();
|
||||
} else {
|
||||
return $this->execError('Record does not exist rr_category_id:'.$data->{'rr_category_id'});
|
||||
}
|
||||
|
||||
return $this->execSuccess();
|
||||
}
|
||||
public function sub_create($data) {
|
||||
if(!empty($data->{'error'})) return $data;
|
||||
$missing = $this->required(['rr_category_id', 'rr_sub_category_id', 'name'], ['name_en'], $data);
|
||||
if($missing) return $missing;
|
||||
$exits = $this->exist(Categories::class, 'rr_sub_category_id', $data->{'rr_sub_category_id'});
|
||||
if($exits) return $exits;
|
||||
$category = Categories::find()->where(['rr_category_id' => $data->{'rr_category_id'}])->one();
|
||||
if($category) {
|
||||
$sub = new Categories();
|
||||
$sub->parent_id = $category->id;
|
||||
$sub->rr_category_id = $category->rr_category_id;
|
||||
$sub->rr_sub_category_id = $data->{'rr_sub_category_id'};
|
||||
$sub->name = $data->{'name'};
|
||||
$sub->ts_en_name = $data->{'name_en'};
|
||||
$sub->order_index = Categories::getNextOrderIndex($category->id);
|
||||
$sub->save();
|
||||
} else {
|
||||
return $this->execError("Main category record does not exist: rr_category_id':$data->{'rr_category_id'}");
|
||||
}
|
||||
return $this->execSuccess();
|
||||
}
|
||||
|
||||
public function sub_update($data) {
|
||||
if(!empty($data->{'error'})) return $data;
|
||||
$missing = $this->required(['rr_sub_category_id', 'name'], ['name_en'], $data);
|
||||
if($missing) return $missing;
|
||||
$sub = Categories::find()->where(['rr_sub_category_id' => $data->{'rr_sub_category_id'}])->one();
|
||||
if($sub) {
|
||||
$sub->name = $data->{'name'};
|
||||
$sub->ts_en_name = $data->{'name_en'};
|
||||
$sub->save();
|
||||
} else {
|
||||
return $this->execError('Record does not exist rr_sub_category_id:'.$data->{'rr_sub_category_id'});
|
||||
}
|
||||
}
|
||||
|
||||
public function sub_delete($data) {
|
||||
if(!empty($data->{'error'})) return $data;
|
||||
$missing = $this->required(['rr_sub_category_id'], [], $data);
|
||||
if($missing) return $missing;
|
||||
$sub = Categories::find()->where(['rr_sub_category_id' => $data->{'rr_sub_category_id'}])->one();
|
||||
if($sub) {
|
||||
$sub->delete();
|
||||
} else {
|
||||
return $this->execError('Record does not exist rr_sub_category_id:'.$data->{'rr_sub_category_id'});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,162 @@
|
||||
<?php
|
||||
|
||||
namespace app\services\api;
|
||||
|
||||
use app\models\Library;
|
||||
use app\models\LibraryLog;
|
||||
use app\models\RegisterObjectFields;
|
||||
use app\models\RegisterObjectFiles;
|
||||
use app\models\RegisterObjects;
|
||||
use yii\base\BaseObject;
|
||||
|
||||
class SyncObject extends Sync
|
||||
{
|
||||
public function setObject($data)
|
||||
{
|
||||
//echo $data->general->ref_num;
|
||||
|
||||
//$file = $_SERVER['DOCUMENT_ROOT'].'/new_publication.json';
|
||||
//file_put_contents($file, $incoming);
|
||||
|
||||
$lib_log = new LibraryLog();
|
||||
$lib_log->log = json_encode($data);
|
||||
$lib_log->save();
|
||||
|
||||
$object = RegisterObjects::find()->where(['ref_num' => $data->general->ref_num])->one();
|
||||
if (!$object) {
|
||||
$object = new RegisterObjects();
|
||||
$object->date_added = date('Y-m-d H:i:s');
|
||||
$object->publish_date = date('Y-m-d H:i:s');
|
||||
}
|
||||
|
||||
$data->general = $data->general ?? new \stdClass();
|
||||
$data->bg = $data->bg ?? new \stdClass();
|
||||
$data->en = $data->en ?? new \stdClass();
|
||||
$data->fields = $data->fields ?? [];
|
||||
$data->files = $data->files ?? [];
|
||||
|
||||
|
||||
// General
|
||||
$object->is_active = 1;
|
||||
$object->ref_num = $data->general->ref_num;
|
||||
$object->lib_type = $data->general->lib_type;
|
||||
if (!empty($data->general->is_payable))
|
||||
$object->is_payable = $data->general->is_payable == 1 ? 1 : null;
|
||||
$object->city_id = $data->general->city_id ?? null;
|
||||
$object->partner_id = $data->general->partner_id ?? null;
|
||||
$object->infocenter_email = $data->general->infocenter_email ?? null;
|
||||
$object->infocenter_website = $data->general->infocenter_website ?? null;
|
||||
$object->administrative_latitude = $data->general->administrative_latitude ?? null;
|
||||
$object->administrative_longitude = $data->general->administrative_longitude ?? null;
|
||||
$object->created_year = $data->general->created_year ?? null;
|
||||
$object->created_by = $data->general->created_by ?? null;
|
||||
$object->date_updated = date('Y-m-d H:i:s');
|
||||
$object->object_thumbnail_url = $data->general->object_thumbnail_url ?? null;
|
||||
|
||||
//BG
|
||||
$object->name = $data->bg->name ?? null;
|
||||
$object->description = $data->bg->description ?? null;
|
||||
$object->short_description = $data->bg->short_description ?? null;
|
||||
$object->annotation = $data->bg->annotation ?? null;
|
||||
$object->location_description = $data->bg->location_description ?? null;
|
||||
$object->admistrative_address = $data->bg->admistrative_address ?? null;
|
||||
$object->temporary_address = $data->bg->temporary_address ?? null;
|
||||
$object->infocenter_name = $data->bg->infocenter_name ?? null;
|
||||
$object->infocenter_address = $data->bg->infocenter_address ?? null;
|
||||
|
||||
//EN
|
||||
$object->ts_en_name = $data->en->name ?? null;
|
||||
$object->ts_en_description = $data->en->description ?? null;
|
||||
$object->ts_en_short_description = $data->en->short_description ?? null;
|
||||
$object->ts_en_annotation = $data->en->annotation ?? null;
|
||||
$object->ts_en_location_description = $data->en->location_description ?? null;
|
||||
$object->ts_en_admistrative_address = $data->en->admistrative_address ?? null;
|
||||
$object->ts_en_temporary_address = $data->en->temporary_address ?? null;
|
||||
$object->ts_en_infocenter_name = $data->en->infocenter_name ?? null;
|
||||
$object->ts_en_infocenter_address = $data->en->infocenter_address ?? null;
|
||||
|
||||
$object->save();
|
||||
list($fieldIds, $fileIds) = [[], []];
|
||||
|
||||
foreach ($data->fields as $field) {
|
||||
$objectField = RegisterObjectFields::find()->where(['ref_num' => $field->object_id, 'field_id' => $field->field_id])->one();
|
||||
|
||||
if (!$objectField)
|
||||
$objectField = new RegisterObjectFields();
|
||||
$objectField->field_id = $field->field_id;
|
||||
$objectField->object_id = $object->id;
|
||||
$objectField->ref_num = $field->object_id;
|
||||
$objectField->value_id = $field->value_id ?? null;
|
||||
$objectField->value_text = $field->value_text ?? null;
|
||||
$objectField->save();
|
||||
$fieldIds[] = $objectField->field_id;
|
||||
}
|
||||
foreach ($data->files as $registerFile) {
|
||||
$file = RegisterObjectFiles::find()->where(['file_ref_num' => $registerFile->file_ref_num])->one();
|
||||
if (!$file)
|
||||
$file = new RegisterObjectFiles();
|
||||
$file->file_url = $registerFile->file_url ?? null;
|
||||
$file->streaming_url = $registerFile->streaming_url ?? null;
|
||||
$file->extension = $registerFile->extension ?? null;
|
||||
$file->file_ref_num = $registerFile->file_ref_num;
|
||||
$file->size = $registerFile->size ?? null;
|
||||
$file->object_id = $object->id;
|
||||
$file->video_thumbnail = $registerFile->file_thumb_url ?? null;
|
||||
$file->video_title = $registerFile->title ?? null;
|
||||
$file->video_duration = $registerFile->duration ?? null;
|
||||
$file->file_content_type = $registerFile->file_content_type ?? null;
|
||||
$file->save();
|
||||
$fileIds[] = $file->file_ref_num;
|
||||
}
|
||||
|
||||
// DELETE FIELDS
|
||||
foreach ($object->registerObjectFields as $registerObjectField) {
|
||||
if (!in_array($registerObjectField->field_id, $fieldIds))
|
||||
$registerObjectField->delete();
|
||||
}
|
||||
//DELETE FILES
|
||||
foreach ($object->registerObjectFiles as $registerObjectFile) {
|
||||
if (!in_array($registerObjectFile->file_ref_num, $fileIds))
|
||||
$registerObjectFile->delete();
|
||||
}
|
||||
|
||||
|
||||
//$library = $object->
|
||||
if ($data->general->lib_type == 2) {
|
||||
$lib = $object->library ?? new Library();
|
||||
//Library
|
||||
foreach ($data->general as $key => $value) {
|
||||
if (substr($key, 0, 4) === "lib_") {
|
||||
if($key == 'lib_language') continue;
|
||||
if($lib->hasProperty($key) && $value != '') {
|
||||
$lib->{$key} = $value;
|
||||
}
|
||||
}
|
||||
}
|
||||
$lib->object_id = $object->id;
|
||||
|
||||
if(!empty($data->language) && $data->language->language_ids) {
|
||||
$lib->lib_language = $data->language->language_ids;
|
||||
}
|
||||
|
||||
$lib->save();
|
||||
}
|
||||
return ['success' => true, 'message' => 'Record done!'];
|
||||
}
|
||||
|
||||
public function unsetObject($data)
|
||||
{
|
||||
$lib_log = new LibraryLog();
|
||||
$lib_log->log = json_encode($data);
|
||||
$lib_log->save();
|
||||
|
||||
if (!empty($data->ref_num)) {
|
||||
$object = RegisterObjects::find()->where(['ref_num' => $data->ref_num])->one();
|
||||
$object->is_active = null;
|
||||
$object->save();
|
||||
return ['success' => true, 'message' => 'The object is already deactivated'];
|
||||
} else {
|
||||
return ['error' => 1, 'message' => 'Missing ref_num parameter'];
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user