/var/www/(Del)gepgroup.hk/webadmin/news_update_sort.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
<?php
require_once( realpath(dirname(__FILE__)) . '/../common/config.php');
require_once( 
realpath(dirname(__FILE__)) . '/function_is_login.php');
if (!
is_login())
{
    
header("Location: index.php");
    exit;
}

// Update sort
$newss News::all(array(
    
'conditions' => array('deleted = ?'0),
));
$news_map = array();
foreach (
$newss as $news)
    
$news_map[$news->id] = $news;

$msg NULL;
$is_valid News::transaction(function() use (&$news_map, &$msg)
{
    
$commit true;
    foreach (
$_POST['sort'] as $id => $sort)
    {
        if (isset(
$news_map[$id]))
        {
            
$model $news_map[$id];
            if (
$model->sort != $sort)
            {
                
$model->sort $sort;
                if (!
$model->save())
                {
                    
$commit false;
                    
$msg $model->errors->full_messages();
                    break;
                }
            }
        }
    }
    return 
$commit;
});

if (
$is_valid)
{
    
// Redirect
    
header("Location: news_index.php?msg=Update Success");
    exit;
}
else
{
    echo 
$msg;
}