-
Notifications
You must be signed in to change notification settings - Fork 3
/
NestedSortableAction.php
62 lines (49 loc) · 1.44 KB
/
NestedSortableAction.php
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
<?php
namespace mcms\nested;
use yii\base\Action;
use yii\web\NotFoundHttpException;
class NestedSortableAction extends Action
{
public $modelclass;
public $scenario='';
public function run()
{
// Get the JSON string
$jsonstring = $_GET['jsonstring'];
// Decode it into an array
$jsonDecoded = json_decode($jsonstring, true, 64);
// Run the function above
$readbleArray = $this->parseJsonArray($jsonDecoded);
// Loop through the "readable" array and save changes to DB
foreach ($readbleArray as $key => $value) {
// $value should always be an array, but we do a check
if (is_array($value)) {
$modelclass=$this->modelclass;
$model= $modelclass::find()->where([
'id' => $value['id']
])->one();
if($this->scenario){
$model->setScenario($this->scenario);
}
$model->order = $key;
$model->parent = $value['parentID'];
$model->save(false);
}
}
// Echo status message for the update
echo \Yii::t('app',"The list was updated ").date("y-m-d H:i:s")."!";
}
public function parseJsonArray($jsonArray, $parentID = 0)
{
$return = array();
foreach ($jsonArray as $subArray) {
$returnSubSubArray = array();
if (isset($subArray['children'])) {
$returnSubSubArray = $this->parseJsonArray($subArray['children'], $subArray['id']);
}
$return[] = array('id' => $subArray['id'], 'parentID' => $parentID);
$return = array_merge($return, $returnSubSubArray);
}
return $return;
}
}