2019-04-03 11:51:29 +02:00
|
|
|
<?php
|
|
|
|
App::uses('AppModel', 'Model');
|
|
|
|
|
|
|
|
class DecayingModelMapping extends AppModel
|
|
|
|
{
|
|
|
|
public $actsAs = array('Containable');
|
|
|
|
|
|
|
|
public $validate = array(
|
|
|
|
'attribute_type' => array(
|
|
|
|
'valueNotEmpty' => array(
|
|
|
|
'rule' => array('valueNotEmpty'),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
'model_id' => array(
|
|
|
|
'valueNotEmpty' => array(
|
|
|
|
'rule' => array('valueNotEmpty'),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
public $belongsTo = array(
|
2019-04-04 09:37:51 +02:00
|
|
|
'DecayingModel' => array(
|
|
|
|
'className' => 'DecayingModel',
|
|
|
|
'foreignKey' => 'id'
|
|
|
|
)
|
2019-04-03 11:51:29 +02:00
|
|
|
);
|
|
|
|
|
2024-03-12 11:32:10 +01:00
|
|
|
private $modelCache = [];
|
|
|
|
|
2019-08-22 11:31:50 +02:00
|
|
|
public function resetMappingForModel($new_model, $user) {
|
2019-09-12 11:28:32 +02:00
|
|
|
if (empty($new_model['model_id'])) {
|
|
|
|
throw new NotFoundException(__('No Decaying Model with the provided ID exists'));
|
|
|
|
}
|
2019-04-04 09:37:51 +02:00
|
|
|
$this->deleteAll(array(
|
2019-04-04 11:17:11 +02:00
|
|
|
'model_id' => $new_model['model_id']
|
2019-04-04 09:37:51 +02:00
|
|
|
));
|
|
|
|
|
2019-07-23 11:07:38 +02:00
|
|
|
$data = array();
|
2019-04-04 09:37:51 +02:00
|
|
|
foreach ($new_model['attribute_types'] as $type) {
|
|
|
|
$to_save = array(
|
|
|
|
'attribute_type' => $type,
|
2019-04-04 11:17:11 +02:00
|
|
|
'model_id' => $new_model['model_id']
|
2019-04-04 09:37:51 +02:00
|
|
|
);
|
2019-04-04 11:17:11 +02:00
|
|
|
$data[] = $to_save;
|
2019-04-04 09:37:51 +02:00
|
|
|
}
|
2019-04-04 11:17:11 +02:00
|
|
|
|
2019-07-23 11:07:38 +02:00
|
|
|
$result = $this->saveMany($data, array(
|
2019-04-04 11:17:11 +02:00
|
|
|
'atomic' => true
|
|
|
|
));
|
2019-07-23 11:07:38 +02:00
|
|
|
if ($result) {
|
|
|
|
return $new_model['attribute_types'];
|
|
|
|
} else {
|
|
|
|
return array();
|
|
|
|
}
|
2019-04-03 11:51:29 +02:00
|
|
|
}
|
|
|
|
|
2019-08-22 11:31:50 +02:00
|
|
|
public function getAssociatedTypes($user, $model) {
|
|
|
|
if (is_numeric($model)) {
|
|
|
|
$model = $this->DecayingModel->fetchModel($user, $model, false);
|
2019-09-12 11:02:39 +02:00
|
|
|
if (empty($model)) {
|
|
|
|
throw new NotFoundException(__('No Decaying Model with the provided ID exists'));
|
|
|
|
}
|
2019-08-22 11:31:50 +02:00
|
|
|
}
|
|
|
|
$decaying_model = isset($model['DecayingModel']) ? $model['DecayingModel'] : $model;
|
2019-08-30 09:18:00 +02:00
|
|
|
if ($decaying_model['default']) {
|
2019-04-04 09:37:51 +02:00
|
|
|
$associated_types = $decaying_model['attribute_types'];
|
2019-08-22 11:31:50 +02:00
|
|
|
} else {
|
2019-04-04 09:37:51 +02:00
|
|
|
$temp = $this->find('list', array(
|
|
|
|
'conditions' => array(
|
2019-08-22 11:31:50 +02:00
|
|
|
'model_id' => $decaying_model['id']
|
2019-04-04 09:37:51 +02:00
|
|
|
),
|
2019-04-03 11:51:29 +02:00
|
|
|
'recursive' => -1,
|
|
|
|
'fields' => array('attribute_type')
|
|
|
|
));
|
2019-08-22 11:31:50 +02:00
|
|
|
$associated_types = array_values($temp);
|
2019-04-03 11:51:29 +02:00
|
|
|
}
|
|
|
|
return $associated_types;
|
|
|
|
}
|
|
|
|
|
2019-08-29 12:23:18 +02:00
|
|
|
public function getAssociatedModels($user, $attribute_type = false) {
|
2024-03-12 11:32:10 +01:00
|
|
|
$cacheKey = sprintf('%s', $attribute_type);
|
|
|
|
if (isset($this->modelCache[$cacheKey])) {
|
|
|
|
return $this->modelCache[$cacheKey];
|
|
|
|
}
|
2019-08-23 09:16:51 +02:00
|
|
|
$conditions = array(
|
|
|
|
'OR' => array(
|
|
|
|
'DecayingModel.org_id' => $user['org_id'],
|
|
|
|
'DecayingModel.all_orgs' => true
|
|
|
|
)
|
|
|
|
);
|
2019-08-29 12:23:18 +02:00
|
|
|
if ($attribute_type !== false) {
|
2019-08-23 09:16:51 +02:00
|
|
|
$conditions['attribute_type'] = $attribute_type;
|
|
|
|
}
|
2019-04-04 09:37:51 +02:00
|
|
|
$associated_models = $this->find('all', array(
|
2019-08-23 09:16:51 +02:00
|
|
|
'conditions' => $conditions,
|
2019-04-04 09:37:51 +02:00
|
|
|
'recursive' => -1,
|
2019-08-22 11:31:50 +02:00
|
|
|
'fields' => array('attribute_type', 'model_id'),
|
|
|
|
'joins' => array( // joins has to be done to enforce ACL
|
|
|
|
array(
|
|
|
|
'table' => 'decaying_models',
|
|
|
|
'alias' => 'DecayingModel',
|
|
|
|
'type' => 'INNER',
|
|
|
|
'conditions' => array(
|
|
|
|
'DecayingModel.id = DecayingModelMapping.model_id'
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
2019-04-04 09:37:51 +02:00
|
|
|
));
|
2019-08-29 12:23:18 +02:00
|
|
|
// Also add default models to selection
|
|
|
|
$default_models = $this->DecayingModel->fetchAllDefaultModel($user);
|
|
|
|
$associated_default_models = array();
|
|
|
|
foreach ($default_models as $i => $model) {
|
|
|
|
$intersection = array_intersect($model['DecayingModel']['attribute_types'], array($attribute_type));
|
|
|
|
if (count($intersection) > 0) {
|
|
|
|
$associated_default_models[$attribute_type][] = $model['DecayingModel']['id'];
|
|
|
|
}
|
|
|
|
}
|
2019-04-04 15:22:31 +02:00
|
|
|
$associated_models = Hash::combine($associated_models, '{n}.DecayingModelMapping.model_id', '{n}.DecayingModelMapping.model_id', '{n}.DecayingModelMapping.attribute_type');
|
2019-08-29 12:23:18 +02:00
|
|
|
$models = array_merge_recursive($associated_default_models, $associated_models);
|
2024-03-12 11:32:10 +01:00
|
|
|
$this->modelCache[$cacheKey] = $models;
|
2019-08-29 12:23:18 +02:00
|
|
|
return $models;
|
2019-04-04 09:37:51 +02:00
|
|
|
}
|
|
|
|
|
2019-04-03 11:51:29 +02:00
|
|
|
}
|