2019-04-03 11:51:29 +02:00
|
|
|
<?php
|
|
|
|
App::uses('AppModel', 'Model');
|
|
|
|
|
|
|
|
class DecayingModelMapping extends AppModel
|
|
|
|
{
|
|
|
|
public $actsAs = array('Containable');
|
|
|
|
|
|
|
|
public $validate = array(
|
|
|
|
'org_id' => array(
|
|
|
|
'valueNotEmpty' => array(
|
|
|
|
'rule' => array('valueNotEmpty'),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
'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
|
|
|
);
|
|
|
|
|
|
|
|
private $__default_type_mapping_reverse = array();
|
|
|
|
|
2019-04-04 11:17:11 +02:00
|
|
|
public function resetMappingForModel($new_model) {
|
|
|
|
if (!isset($new_model['org_id'])) {
|
|
|
|
$new_model['org_id'] = null;
|
|
|
|
}
|
2019-04-04 09:37:51 +02:00
|
|
|
$this->deleteAll(array(
|
2019-04-04 11:17:11 +02:00
|
|
|
'DecayingModelMapping.org_id' => $new_model['org_id'],
|
|
|
|
'model_id' => $new_model['model_id']
|
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
|
|
|
if (!is_null($new_model['org_id'])) {
|
|
|
|
$to_save['org_id'] = $new_model['org_id'];
|
|
|
|
}
|
|
|
|
$data[] = $to_save;
|
2019-04-04 09:37:51 +02:00
|
|
|
}
|
2019-04-04 11:17:11 +02:00
|
|
|
|
|
|
|
$this->saveMany($data, array(
|
|
|
|
'atomic' => true
|
|
|
|
));
|
2019-04-03 11:51:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getAssociatedTypes($user, $model_id) {
|
2019-04-04 09:37:51 +02:00
|
|
|
$decaying_model = $this->DecayingModel->find('first', array(
|
|
|
|
'conditions' => array('id' => $model_id),
|
|
|
|
'recursive' => -1,
|
|
|
|
));
|
|
|
|
if (empty($decaying_model)) {
|
2019-04-03 11:51:29 +02:00
|
|
|
$associated_types = array();
|
|
|
|
} else {
|
2019-04-04 09:37:51 +02:00
|
|
|
$decaying_model = $decaying_model['DecayingModel'];
|
|
|
|
$associated_types = $decaying_model['attribute_types'];
|
|
|
|
$temp = $this->find('list', array(
|
|
|
|
'conditions' => array(
|
2019-04-04 15:22:31 +02:00
|
|
|
'OR' => array(
|
|
|
|
array('org_id' => $user['Organisation']['id']),
|
|
|
|
array('org_id' => null),
|
|
|
|
),
|
2019-04-04 09:37:51 +02:00
|
|
|
'model_id' => $model_id
|
|
|
|
),
|
2019-04-03 11:51:29 +02:00
|
|
|
'recursive' => -1,
|
|
|
|
'fields' => array('attribute_type')
|
|
|
|
));
|
2019-04-05 11:24:40 +02:00
|
|
|
$associated_types = array_values(array_unique(array_merge($associated_types, $temp)));
|
2019-04-03 11:51:29 +02:00
|
|
|
}
|
|
|
|
return $associated_types;
|
|
|
|
}
|
|
|
|
|
2019-04-04 15:22:31 +02:00
|
|
|
public function getAssociatedModels($user, $attribute_type = array()) {
|
|
|
|
$conditions = array(
|
|
|
|
'OR' => array(
|
|
|
|
array('org_id' => $user['Organisation']['id']),
|
|
|
|
array('org_id' => null),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
if (!empty($attribute_type)) {
|
|
|
|
$conditions['attribute_type'] = $attribute_type;
|
|
|
|
}
|
2019-04-04 09:37:51 +02:00
|
|
|
$associated_models = $this->find('all', array(
|
2019-04-04 15:22:31 +02:00
|
|
|
'conditions' => $conditions,
|
2019-04-04 09:37:51 +02:00
|
|
|
'recursive' => -1,
|
2019-04-04 15:22:31 +02:00
|
|
|
// 'group' => 'attribute_type',
|
|
|
|
'fields' => array('attribute_type', 'model_id')
|
2019-04-04 09:37:51 +02:00
|
|
|
));
|
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-04-04 09:37:51 +02:00
|
|
|
return $associated_models;
|
|
|
|
}
|
|
|
|
|
2019-04-03 11:51:29 +02:00
|
|
|
}
|