Project

General

Profile

1
<?php
2
/**
3
 *
4
 * @category        modules
5
 * @package         JsAdmin
6
 * @author          WebsiteBaker Project, modified by Swen Uth for Website Baker 2.7
7
 * @copyright       (C) 2006, Stepan Riha
8
 * @copyright       WebsiteBaker Org. e.V.
9
 * @link            http://websitebaker.org/
10
 * @license         http://www.gnu.org/licenses/gpl.html
11
 * @platform        WebsiteBaker 2.8.3
12
 * @requirements    PHP 5.3.6 and higher
13
 * @version         $Id: move_to.php 2 2017-07-02 15:14:29Z Manuela $
14
 * @filesource      $HeadURL: svn://isteam.dynxs.de/wb/2.10.x/branches/main/modules/jsadmin/move_to.php $
15
 * @lastmodified    $Date: 2017-07-02 17:14:29 +0200 (Sun, 02 Jul 2017) $
16
 *
17
*/
18

    
19
// Include the configuration file
20
if (!defined('WB_PATH')) {
21
    $sStartupFile = dirname(dirname(__DIR__)).'/config.php';
22
    if (is_readable($sStartupFile)) {
23
        require($sStartupFile);
24
    } else {
25
        die(
26
            'tried to read a nonexisting or not readable startup file ['
27
          . basename(dirname($sStartupFile)).'/'.basename($sStartupFile).']!!'
28
        );
29
    }
30
}
31

    
32
    $aJsonRespond = array();
33
    $aJsonRespond['jsadmin'] = array();
34
    $aJsonRespond['modules'] = '';
35
    $aJsonRespond['modules_dir'] = '';
36
    $aJsonRespond['message'] = 'ajax operation failed';
37
    $aJsonRespond['success'] = false;
38
    // Include WB admin wrapper script
39
    $update_when_modified = false;
40
// Tells script to update when this page was last updated
41
    $admin_header = false;
42
    require(WB_PATH.'/modules/admin.php');
43

    
44
if (isset($aRequestVars['page_id']) && is_numeric($aRequestVars['page_id']) && is_numeric(@$aRequestVars['newposition']))
45
{
46

    
47
// Include the ordering class
48
    if (!class_exists('order', false)){require(WB_PATH.'/framework/class.order.php');}
49

    
50
  $cleanOrder = (function($common_id) use ($database){
51
        global $table,$sFieldOrderName,$common_field;
52
// Loop through all records and give new order
53
        $sql  = 'SET @c:=0';
54
        $database->query($sql);
55
        $sql  = 'UPDATE `'.$table.'` SET `'.$sFieldOrderName.'`=(SELECT @c:=@c+1) '
56
              . 'WHERE `'.$common_field.'`=\''.$common_id.'\' '
57
              . 'ORDER BY `'.$sFieldOrderName.'` ASC;';
58
        if ($database->query($sql)){
59
            echo "$sql".PHP_EOL;
60
        } else {
61
          $aJsonRespond['message'] = $sFieldOrderName.PHP-EOL.$database->get_error();
62
          $aJsonRespond['success'] = false;
63
          exit (json_encode($aJsonRespond));
64
        }
65
  });
66

    
67
    $position = (int)$aRequestVars['newposition'];
68
    // Interface move_to.php from modules
69
    if (isset($aRequestVars['module'])) {
70
        $aJsonRespond['jsadmin'] = $aRequestVars;
71
        $sParameterFileName = WB_PATH.'/modules/'.$aRequestVars['module'].'/move_to.php';
72
        if (is_readable($sParameterFileName)){require $sParameterFileName;}
73
//        exit(json_encode($aJsonRespond));
74
    } else {
75
    // default Interface move_to.php from core
76
        if( isset($aRequestVars['page_id']) || (isset($aRequestVars['section_id'])) ) {
77
            // Get common fields
78
            if(isset($aRequestVars['section_id']) && is_numeric($aRequestVars['section_id'])) {
79
//                $page_id = (int)$aRequestVars['page_id'];
80
                $id = (int)$aRequestVars['section_id'];
81
                $id_field = 'section_id';
82
//                $group = (int)$aRequestVars['section_id'];
83
                $sFieldOrderName = 'position';
84
                $common_field = 'page_id';
85
                $table = TABLE_PREFIX.'sections';
86
                $aJsonRespond['modules'] = '/'.ADMIN_DIRECTORY.'(pages/sections.php';
87
            } else {
88
                $id = (int)$aRequestVars['page_id'];
89
                $id_field = 'page_id';
90
//                $group = (int)$aRequestVars['page_id'];
91
                $sFieldOrderName = 'position';
92
                $common_field = 'parent';
93
                $table = TABLE_PREFIX.'pages';
94
                $aJsonRespond['modules'] = '/'.ADMIN_DIRECTORY.'(pages/index.php';
95
            }
96
        }
97
    }
98

    
99
    // Get current index
100
    $sql = <<<EOT
101
SELECT `$common_field`, `$sFieldOrderName` FROM `$table` WHERE `$id_field` = $id
102
EOT;
103
    echo "$sql".PHP_EOL;
104
    if ($oRes = $database->query($sql)){
105
        if( $row = $oRes->fetchRow(MYSQLI_ASSOC)) {
106
            $common_id = $row[$common_field];
107
            $old_position = $row['position'];
108
        }
109
    } else {
110
      $aJsonRespond['message'] = $sFieldOrderName.PHP-EOL.$database->get_error();
111
      $aJsonRespond['success'] = false;
112
      exit (json_encode($aJsonRespond));
113
    }
114
    echo "Old Position: $old_position".PHP_EOL;
115
    echo "New Position: $position".PHP_EOL;
116
    if($old_position == $position){
117
      $cleanOrder($common_id);
118
      return;
119
    }
120

    
121
    // Build query to update affected rows
122
    if($old_position < $position)
123
        $sql = <<<EOT
124
UPDATE `$table` SET `$sFieldOrderName` = `$sFieldOrderName` - 1
125
    WHERE `$sFieldOrderName` > $old_position AND `$sFieldOrderName` <= $position
126
        AND `$common_field` = $common_id
127
EOT;
128
    else
129
        $sql = <<<EOT
130
UPDATE `$table` SET `position` = `position` + 1
131
    WHERE `$sFieldOrderName` >= $position AND `$sFieldOrderName` < $old_position
132
        AND `$common_field` = $common_id
133
EOT;
134
    if ($database->query($sql)){
135
        echo "$sql".PHP_EOL;
136
    }
137
    // Build query to update specified row
138
    $sql = <<<EOT
139
UPDATE `$table` SET `$sFieldOrderName` = $position
140
    WHERE `$id_field` = $id
141
EOT;
142
    if ($database->query($sql))
143
    {
144
        echo "$sql".PHP_EOL;
145
        $cleanOrder($common_id);
146
        $aJsonRespond['success'] = true;
147
        echo (json_encode($aJsonRespond));
148
    }
149
} else {
150
    $aJsonRespond['message'] = "Missing parameters";
151
    $aJsonRespond['success'] = false;
152
    exit (json_encode($aJsonRespond));
153
}
(11-11/14)