163 lines
6.8 KiB
PHP
163 lines
6.8 KiB
PHP
<?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'];
|
|
}
|
|
}
|
|
}
|