Initial import
This commit is contained in:
@@ -0,0 +1,165 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace app\models;
|
||||
|
||||
use yii\base\BaseObject;
|
||||
|
||||
/**
|
||||
* Class Objects
|
||||
* @package app\models
|
||||
* @property int $sc_id
|
||||
* @property int $ot_id
|
||||
* @property int $partner_id
|
||||
* @property string $name
|
||||
* @property string $annotation
|
||||
* @property string $description
|
||||
* [RELATIONS]
|
||||
* @property ObjectTemplate $objectTemplate
|
||||
* @property Categories $subCategory
|
||||
* @property ObjectsField[] $objectFields
|
||||
* @property Partner $partner
|
||||
* @property \app\models\History $history
|
||||
* @property \app\models\CollectionsObjects[] $collections
|
||||
* @property string $collectionConcatList
|
||||
*/
|
||||
class Objects extends _Base
|
||||
{
|
||||
public function getHistory()
|
||||
{
|
||||
return $this->hasMany(History::class, ['history_id' => 'id'])->where(['table_name' => 'objects', 'action' => 1])->one();
|
||||
}
|
||||
|
||||
public function getObjectTemplate()
|
||||
{
|
||||
return $this->hasOne(ObjectTemplate::class, ['id' => 'ot_id']);
|
||||
}
|
||||
|
||||
public function getPartner()
|
||||
{
|
||||
return $this->hasOne(Partner::class, ['id' => 'partner_id']);
|
||||
}
|
||||
|
||||
public function getSubCategory()
|
||||
{
|
||||
return $this->hasOne(Categories::class, ['id' => 'sc_id']);
|
||||
}
|
||||
|
||||
public function getObjectFields()
|
||||
{
|
||||
return $this->hasMany(ObjectsField::class, ['object_id' => 'id'])
|
||||
->joinWith('objectTemplateField')
|
||||
->orderBy(['order_index' => SORT_ASC])
|
||||
->all();
|
||||
}
|
||||
|
||||
public function setObjectFields($p)
|
||||
{
|
||||
if ($this->ot_id) {
|
||||
if (empty($_GET['id'])) {
|
||||
foreach ($this->objectTemplate->objectTemplateFields as $templateField) {
|
||||
$objectField = new ObjectsField();
|
||||
$objectField->object_id = $this->id;
|
||||
$objectField->object_tf_id = $templateField->id;
|
||||
$objectField->save();
|
||||
}
|
||||
} else {
|
||||
if (empty($p->{'new_object_fields'})) {
|
||||
$obf = $p->{'obf'};
|
||||
$idsToUpdate = (array_keys($obf));
|
||||
$objectFields = ObjectsField::find()->where(['IN', 'id', $idsToUpdate])->all();
|
||||
foreach ($objectFields as $objectField) {
|
||||
if (!empty($obf{$objectField->id}{'option_tf_id'})) $objectField->option_tf_id = $obf{$objectField->id}{'option_tf_id'};
|
||||
if (!empty($obf{$objectField->id}{'value'})) $objectField->value = $obf{$objectField->id}{'value'};
|
||||
if (!empty($obf{$objectField->id}{'value_en'})) $objectField->value_en = $obf{$objectField->id}{'value_en'};
|
||||
if (!empty($obf{$objectField->id}{'date'})) $objectField->date = $obf{$objectField->id}{'date'};
|
||||
if (!empty($obf{$objectField->id}{'geo_lon'})) $objectField->geo_lon = $obf{$objectField->id}{'geo_lon'};
|
||||
if (!empty($obf{$objectField->id}{'geo_lat'})) $objectField->geo_lat = $obf{$objectField->id}{'geo_lat'};
|
||||
if (!empty($obf{$objectField->id}{'settlement_id'})) $objectField->settlement_id = $obf{$objectField->id}{'settlement_id'};
|
||||
if (!empty($obf{$objectField->id}{'common_field_id'})) $objectField->common_field_id = $obf{$objectField->id}{'common_field_id'};
|
||||
|
||||
$objectField->save();
|
||||
}
|
||||
} else {
|
||||
foreach ($this->newTemplateFields() as $id => $field) {
|
||||
$newObjectField = new ObjectsField();
|
||||
$newObjectField->object_tf_id = $id;
|
||||
$newObjectField->object_id = $this->id;
|
||||
$newObjectField->save();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function newTemplateFields()
|
||||
{
|
||||
$objectFieldIds = [];
|
||||
$missing = [];
|
||||
foreach ($this->objectFields as $field) $objectFieldIds[] = $field->object_tf_id;
|
||||
foreach ($this->objectTemplate->objectTemplateFields as $templateField) {
|
||||
if (!in_array($templateField->id, $objectFieldIds)) {
|
||||
$userInterface = ObjectTemplateField::userInterfaceType($templateField->user_interface_type, false);
|
||||
$missing[$templateField->id] = [
|
||||
'name' => $templateField->name,
|
||||
'type' => $userInterface
|
||||
];
|
||||
}
|
||||
}
|
||||
return $missing;
|
||||
}
|
||||
|
||||
public function setCollections($p)
|
||||
{
|
||||
$ids = $p->{'collections'} ?? [];
|
||||
|
||||
$related = CollectionsObjects::find()->where(['object_id' => $this->id])->all();
|
||||
$remain = [];
|
||||
foreach ($related as $cto) {
|
||||
if(in_array($cto->id, $ids)) {
|
||||
$remain[] = $cto->id;
|
||||
} else {
|
||||
$cto->delete();
|
||||
}
|
||||
}
|
||||
foreach ($ids as $id) {
|
||||
if(!in_array($id, $remain)) {
|
||||
$newCto = new CollectionsObjects();
|
||||
$newCto->object_id = $this->id;
|
||||
$newCto->collection_id = $id;
|
||||
$newCto->category_id = $this->subCategory->id;
|
||||
$newCto->save();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function getCollections()
|
||||
{
|
||||
return $this->hasMany(CollectionsObjects::class, ['object_id' => 'id']);
|
||||
}
|
||||
|
||||
public function getCollectionsSelectList()
|
||||
{
|
||||
$ids = [];
|
||||
foreach ($this->collections as $collection) {
|
||||
$ids[] = $collection->collection_id;
|
||||
}
|
||||
$list = [];
|
||||
foreach (Collections::find()->all() as $item) {
|
||||
$list[$item->id] = [
|
||||
'name' => $item->name,
|
||||
'selected' => in_array($item->id, $ids)
|
||||
];
|
||||
}
|
||||
return $list;
|
||||
}
|
||||
|
||||
public function getCollectionConcatList() {
|
||||
$collections = [];
|
||||
foreach ($this->collections as $collection) {
|
||||
$collections[] = $collection->collection->name;
|
||||
}
|
||||
return implode(', ', $collections);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user