1
|
|
|
<?php
|
2
|
|
|
/**
|
3
|
|
|
* @author Sergey Glagolev <[email protected]>
|
4
|
|
|
* @link https://github.com/shogodev/argilla/
|
5
|
|
|
* @copyright Copyright © 2003-2014 Shogo
|
6
|
|
|
* @license http://argilla.ru/LICENSE
|
7
|
|
|
* @package backend.components.actions
|
8
|
|
|
*/
|
9
|
|
|
class BRelatedActionDelete extends CAction
|
10
|
|
|
{
|
11
|
2 |
|
public function run()
|
12
|
|
|
{
|
13
|
2 |
|
if( Yii::app()->request->isAjaxRequest )
|
14
|
2 |
|
{
|
15
|
1 |
|
$result = $this->tryDelete();
|
16
|
|
|
|
17
|
1 |
|
if( !$result )
|
18
|
1 |
|
throw new CHttpException(500, 'Не могу удалить запись.');
|
19
|
1 |
|
}
|
20
|
|
|
else
|
21
|
1 |
|
throw new CHttpException(500, 'Некорректный запрос.');
|
22
|
1 |
|
}
|
23
|
|
|
|
24
|
1 |
|
private function tryDelete()
|
25
|
|
|
{
|
26
|
1 |
|
$id = Yii::app()->request->getPost('id');
|
27
|
1 |
|
$relation = Yii::app()->request->getPost('relation');
|
28
|
|
|
|
29
|
|
|
/**
|
30
|
|
|
* @var BActiveRecord $model
|
31
|
|
|
* @var BActiveRecord $relatedModel
|
32
|
|
|
*/
|
33
|
1 |
|
$model = new $this->controller->modelClass;
|
34
|
1 |
|
$className = $model->getActiveRelation($relation)->className;
|
35
|
1 |
|
$relatedModel = $className::model()->findByPk($id);
|
36
|
|
|
|
37
|
|
|
try
|
38
|
|
|
{
|
39
|
1 |
|
$result = $relatedModel->delete();
|
40
|
|
|
}
|
41
|
1 |
|
catch(CDbException $e)
|
42
|
|
|
{
|
43
|
|
|
if( strpos($e->getMessage(), 'update a parent row: a foreign key constraint fails') )
|
44
|
|
|
{
|
45
|
|
|
throw $e;
|
46
|
|
|
/* if( !$message = $this->parseError($e->getMessage(), $id) )
|
47
|
|
|
throw $e;
|
48
|
|
|
|
49
|
|
|
echo $message;
|
50
|
|
|
Yii::app()->end();*/
|
51
|
|
|
}
|
52
|
|
|
}
|
53
|
|
|
|
54
|
1 |
|
return $result;
|
55
|
|
|
}
|
56
|
|
|
|
57
|
|
|
/* private function parseError($error, $pk)
|
58
|
|
|
{
|
59
|
|
|
if( !preg_match('/a foreign key constraint fails \((.+)\). The SQL/', $error, $matchesQuery) )
|
60
|
|
|
return null;
|
61
|
|
|
|
62
|
|
|
if( !preg_match('/`(\w+)`.`(\w+)`.+FOREIGN KEY.+`(\w+)`.+REFERENCES+.`(\w+)`/', $matchesQuery[1], $matches) )
|
63
|
|
|
return null;
|
64
|
|
|
|
65
|
|
|
$db = $matches[1];
|
66
|
|
|
$table = $matches[2];
|
67
|
|
|
$field = $matches[3];
|
68
|
|
|
|
69
|
|
|
if( !$records = $this->getRelatedRecords($table, $field, $pk) )
|
70
|
|
|
return null;
|
71
|
|
|
|
72
|
|
|
return $this->createResponse($table, $records);
|
73
|
|
|
}
|
74
|
|
|
|
75
|
|
|
private function getRelatedRecords($table, $field, $pk)
|
76
|
|
|
{
|
77
|
|
|
$criteria = new CDbCriteria();
|
78
|
|
|
$criteria->compare($field, $pk);
|
79
|
|
|
|
80
|
|
|
$command = Yii::app()->db->getSchema()->commandBuilder->createFindCommand($table, $criteria);
|
81
|
|
|
return $command->queryAll();
|
82
|
|
|
}
|
83
|
|
|
|
84
|
|
|
private function createResponse($table, $records)
|
85
|
|
|
{
|
86
|
|
|
switch($table)
|
87
|
|
|
{
|
88
|
|
|
case $this->getFullTableName(BProductParam::model()->tableName()):
|
89
|
|
|
$urls = array();
|
90
|
|
|
foreach(CHtml::listData($records, 'product_id', 'product_id') as $productId)
|
91
|
|
|
{
|
92
|
|
|
$urls[$productId] = CHtml::link('Товар id='.$productId, '/backend/product/product/update/'.$productId, array('target' => '_blank'));
|
93
|
|
|
}
|
94
|
|
|
$out = 'Товары имеющие сввязанные параметры:<br/>';
|
95
|
|
|
$out .= implode('<br/>', $urls);
|
96
|
|
|
break;
|
97
|
|
|
}
|
98
|
|
|
|
99
|
|
|
return $out;
|
100
|
|
|
}
|
101
|
|
|
|
102
|
|
|
private function getFullTableName($shortName)
|
103
|
|
|
{
|
104
|
|
|
$normalShortName = trim($shortName, '{}');
|
105
|
|
|
return Yii::app()->db->tablePrefix.$normalShortName;
|
106
|
|
|
}*/
|
107
|
|
|
} |