1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
<?php
namespace BEdita\Core\Model\Action;
use Cake\Datasource\Exception\InvalidPrimaryKeyException;
use Cake\Utility\Hash;
class GetObjectAction extends BaseAction
{
protected $Table;
protected $objectType;
protected function initialize(array $data)
{
$this->Table = $this->getConfig('table');
$this->objectType = $this->getConfig('objectType');
}
public function execute(array $data = [])
{
$conditions = $this->getPrimaryKeyConditions($data);
$conditions += [
'deleted' => (int)!empty($data['deleted']),
];
if (isset($data['locked'])) {
$conditions += [
'locked' => (int)!empty($data['locked']),
];
}
$contain = array_merge(['ObjectTypes'], (array)Hash::get($data, 'contain'));
$query = $this->Table->find('publishable')
->contain($contain)
->where($conditions);
if (isset($this->objectType)) {
$assoc = $this->objectType->associations;
if (!empty($assoc)) {
$query = $query->contain($assoc);
}
$query = $query->find('type', (array)$this->objectType->id);
}
if (!empty($data['lang'])) {
$query = $query->find('translations', ['lang' => $data['lang']]);
}
return $query->firstOrFail();
}
protected function getPrimaryKeyConditions(array $data)
{
$key = array_map([$this->Table, 'aliasField'], (array)$this->Table->getPrimaryKey());
$primaryKey = (array)$data['primaryKey'];
if (count($key) !== count($primaryKey)) {
$primaryKey = $primaryKey ?: [null];
$primaryKey = array_map(function ($key) {
return var_export($key, true);
}, $primaryKey);
throw new InvalidPrimaryKeyException(sprintf(
'Record not found in table "%s" with primary key [%s]',
$this->Table->getTable(),
implode(', ', $primaryKey)
));
}
$conditions = array_combine($key, $primaryKey);
return $conditions;
}
}