예제 #1
0
파일: cp.php 프로젝트: Acsac/CMS-RuDi
function cpListTable($table, $_fields, $_actions, $where='', $orderby='title', $perpage=60) {
    global $_LANG;
    
    $page = cmsCore::request('page', 'int', 1);

    $sql = 'SELECT *';
    $is_actions = sizeof($_actions);

    foreach($_fields as $key => $value) {
        if (isset($_fields[$key]['fdate'])) {
            $sql .= ", DATE_FORMAT(".$_fields[$key]['field'].", '".$_fields[$key]['fdate']."') as `".$_fields[$key]['field']."`" ;
        }
    }

    $sql .= ' FROM '. $table;

    if (isset($_SESSION['filter_table']) && $_SESSION['filter_table']!=$table) {
        unset($_SESSION['filter']);
    }

    if (cmsCore::inRequest('nofilter')) {
        unset($_SESSION['filter']);
        cmsCore::redirect('/admin/index.php?'. cpAddParam($_SERVER['QUERY_STRING'], 'page', 1));
    }

    $filter = false;
    
    if (cmsCore::inRequest('filter')) {
        $filter = cmsCore::request('filter', 'array_str', '');
        
        if (isset($_SESSION['filter']) && $_SESSION['filter'] != $filter) {
            $page = 1;
        }
        
        $_SESSION['filter'] = $filter;
    } else if (isset($_SESSION['filter'])) {
        $filter = $_SESSION['filter'];
    }

    if ($filter) {
        $f = 0;
        foreach ($filter as $key => $value) {
            if (!empty($filter[$key]) && $filter[$key] != -100) {
                if (!is_numeric($filter[$key])) {
                    cmsCore::c('db')->where($key ." LIKE '%" . $filter[$key] ."%'");
                } else {
                    cmsCore::c('db')->where($key ." = '". $filter[$key] ."'");
                }
                $f++;
            }
        }
        if (!isset($_SESSION['filter'])) { $_SESSION['filter'] = $filter; }
    }
    
    if (mb_strlen($where) <= 3) {
        $where = '1=1';
    }

    //Выставляем сортировку выборки данных в БД
    $sort = cmsCore::request('sort', 'str', '');
    $so = 'asc';
    if (empty($sort)) {
        if (!empty($orderby)) {
            $orderby = trim($orderby);
            
            if (mb_strstr($orderby, ' ')) {
                $sorta = explode(' ', $orderby);
                $sort = $sorta[0];
                
                foreach ($sorta as $s) {
                    $s = mb_strtolower(trim($s,' ,'));
                    if ($s == 'asc' || $s == 'desc') {
                        $so = $s;
                        break;
                    }
                }
            } else {
                $sort = $orderby;
            }
            
            cmsCore::c('db')->order_by = 'ORDER BY '. $orderby;
        } else {
            foreach($_fields as $key => $value) {
                if ($_fields[$key]['field'] == 'ordering' && $sort != 'NSLeft') {
                    $sort = 'ordering';
                    cmsCore::c('db')->orderBy($sort, $so);
                }
            }
        }
    } else {
        $so = cmsCore::request('so', array('asc', 'desc'), $so);
        cmsCore::c('db')->orderBy($sort, $so);
    }

    $total = cmsCore::c('db')->rows_count($table, $where .' '. cmsCore::c('db')->where);
    cmsCore::c('db')->limitPage($page, $perpage);

    $result = cmsCore::c('db')->query(
        $sql . "\n" .'WHERE '. $where .' '. cmsCore::c('db')->where . "\n" . (empty(cmsCore::c('db')->order_by) ? '' : cmsCore::c('db')->order_by) .' LIMIT '. cmsCore::c('db')->limit
    );

    $_SESSION['filter_table'] = $table;

    if (cmsCore::c('db')->error()) {
        unset($_SESSION['filter']);
        cmsCore::redirect('/admin/index.php?'. $_SERVER['QUERY_STRING']);
    }

    $filters = 0; $f_html = '';
    //Find and render filters
    foreach($_fields as $key => $value) {
        if (isset($_fields[$key]['filter'])) {
            $f_html .= '<div class="form-group"><label>'. $_fields[$key]['title'] .': </label>';
            
            $initval = false;
            if (isset($filter[$_fields[$key]['field']])) {
                $initval =  $filter[$_fields[$key]['field']];
            }

            $inputname = 'filter['.$_fields[$key]['field'].']';
            if (!isset($_fields[$key]['filterlist'])) {
                $f_html .= '<input type="text" class="form-control" style="margin-right:10px;margin-left:5px;" name="'. $inputname .'" size="'. $_fields[$key]['filter'] .'" value="'. ($initval === false ? '' : $initval) .'" />';
            } else {
                $f_html .= cpBuildList($inputname, $_fields[$key]['filterlist'], $initval);
            }
            
            $f_html .= '</div>';

            $filters += 1;
            $_SERVER['QUERY_STRING'] = str_replace('filter['.$_fields[$key]['field'].']=', '', $_SERVER['QUERY_STRING']);
        }
    }
    
    //draw filters
    if ($filters > 0) {
        echo '<div class="panel panel-default"><div class="panel-body" style="padding:0;">';
        echo '<form class="form-inline navbar-form navbar-left" name="filterform" action="index.php?'. $_SERVER['QUERY_STRING'] .'" method="POST" role="search">';
        echo '';
        echo $f_html;
        echo '<button type="submit" class="btn btn-default">'. $_LANG['AD_FILTER'] .'</button>';
        if (!empty($f)) {
            echo '<button onclick="window.location.href=\'index.php?'. $_SERVER['QUERY_STRING'] .'&nofilter\'; return false;" class="btn btn-default" style="margin-left:10px;">'. $_LANG['AD_ALL'] .'</button>';
        }
        echo '</form>';
        echo '</div></div>';
    }

    if (cmsCore::c('db')->num_rows($result)) {
        //DRAW LIST TABLE
        echo '<form name="selform" action="index.php?view='.cmsAdmin::getApplet().'&do=saveorder" method="post">';
            echo '<table class="table table-striped tablesorter">';
                //TABLE HEADING
                echo '<thead>'."\n";
                    echo '<tr>'."\n";
                        echo '<th width="20" class="lt_header" style="vertical-align:middle;"><a class="lt_header_link" href="javascript:invert();" title="'.$_LANG['AD_INVERT_SELECTION'].'">#</a></th>'. "\n";
                        foreach($_fields as $key => $value) {
                            echo '<th width="'.$_fields[$key]['width'].'" class="lt_header'. (is_array($_fields[$key]['field']) ? '' : ' header') .' '. ($_fields[$key]['field'] == $sort ? ( $so == 'asc' ? 'headerSortDown' : 'headerSortUp' ) : '') .'" style="vertical-align:middle;">';
                                if (!is_array($_fields[$key]['field'])) {
                                    echo '<a href="index.php?'. cpAddParam($_SERVER['QUERY_STRING'], array( 0 => 'sort', 1 => 'so'), array( 0 => $_fields[$key]['field'], 1 => ($so == 'asc' ? 'desc' : 'asc'))) .'">'. $_fields[$key]['title'] .'</a>';
                                } else {
                                    echo $_fields[$key]['title'];
                                }
                            echo '</th>'. "\n";
                        }
                        if ($is_actions) {
                            echo '<th width="80" class="lt_header" style="vertical-align:middle;">'.$_LANG['AD_ACTIONS'].'</th>'. "\n";
                        }
                    echo '</tr>'."\n";
                echo '</thead><tbody>'."\n";
                //TABLE BODY
                    $r = 0;
                    while ($item = cmsCore::c('db')->fetch_assoc($result)) {
                        $r++;
                        if ($r % 2) { $row_class = 'lt_row1'; } else { $row_class = 'lt_row2'; }
                        echo '<tr id="'. $row_class .'">'."\n";
                        echo '<td class="'.$row_class.'" align="center" valign="middle"><input type="checkbox" name="item[]" value="'.$item['id'].'" /></td>'. "\n";
                        foreach($_fields as $key => $value) {
                            if (isset($_fields[$key]['link'])) {
                                $link = str_replace('%id%', $item['id'], $_fields[$key]['link']);
                                if (isset($_fields[$key]['prc'])) {
                                    // функция обработки под названием $_fields[$key]['prc']
                                    // какие параметры передать функции - один ключ или произвольный массив ключей
                                    if (is_array($_fields[$key]['field'])) {
                                        foreach ($_fields[$key]['field'] as $func_field) {
                                            $in_func_array[$func_field] = $item[$func_field];
                                        }
                                        
                                        $data = call_user_func($_fields[$key]['prc'], $in_func_array);
                                    } else {
                                        $data = call_user_func($_fields[$key]['prc'], $item[$_fields[$key]['field']]);
                                    }
                                } else {
                                    $data = $item[$_fields[$key]['field']];
                                    if (isset($_fields[$key]['maxlen'])) {
                                        if (mb_strlen($data) > $_fields[$key]['maxlen']) {
                                            $data = mb_substr($data, 0, $_fields[$key]['maxlen']).'...';
                                        }
                                    }
                                }

                                //nested sets otstup
                                if (isset($item['NSLevel']) && ($_fields[$key]['field']=='title' || (is_array($_fields[$key]['field']) && in_array('title', $_fields[$key]['field'])))) {
                                    $otstup = str_repeat('&nbsp;&nbsp;&nbsp;&nbsp;', ($item['NSLevel']-1));
                                    if ($item['NSLevel']-1 > 0) { $otstup .=  ' &raquo; '; }
                                } else { $otstup = ''; }

                                if ($table != 'cms_components') {
                                    echo '<td class="'.$row_class.'" valign="middle">'.$otstup.'<a class="lt_link" href="'.$link.'">'.$data.'</a></td>'. "\n";
                                } else {
                                    $data = function_exists('cpComponentHasConfig') && cpComponentHasConfig($item['link']) ?
                                    '<a class="lt_link" href="'.$link.'">'.$data.'</a>' :
                                    $data;

                                    echo '<td class="'.$row_class.'" valign="middle">
                                        <span class="lt_link" style="padding:1px; padding-left:24px; background:url(/admin/images/components/'.$item['link'].'.png) no-repeat">'.$data.'</span>
                                  </td>'. "\n";
                                }
                            } else {
                                if ($_fields[$key]['field'] != 'ordering') {
                                    if ($_fields[$key]['field'] == 'published') {
                                        if (isset($_fields[$key]['do'])) { $do = $_fields[$key]['do']; } else { $do = 'do'; }
                                        if (isset($_fields[$key]['do_suffix'])) { $dos = $_fields[$key]['do_suffix']; $ids = 'item_id'; } else { $dos = ''; $ids = 'id'; }
                                        if ($item['published']) {
                                            $qs = cpAddParam($_SERVER['QUERY_STRING'], $do, 'hide'.$dos);
                                            $qs = cpAddParam($qs, $ids, $item['id']);
                                            $qs2 = cpAddParam($_SERVER['QUERY_STRING'], $do, 'show'.$dos);
                                            $qs2 = cpAddParam($qs2, $ids, $item['id']);
                                            $qs = "pub(".$item['id'].", '".$qs."', '".$qs2."', 'off', 'on');";
                                            echo '<td class="'.$row_class.'" valign="middle">
                                                    <a title="'.$_LANG['HIDE'].'" class="uittip" id="publink'.$item['id'].'" href="javascript:'.$qs.'"><img id="pub'.$item['id'].'" src="images/actions/on.gif" border="0"/></a>
                                                  </td>'. "\n";
                                        } else {
                                            $qs = cpAddParam($_SERVER['QUERY_STRING'], $do, 'show'.$dos);
                                            $qs = cpAddParam($qs, $ids, $item['id']);
                                            $qs2 = cpAddParam($_SERVER['QUERY_STRING'], $do, 'hide'.$dos);
                                            $qs2 = cpAddParam($qs2, $ids, $item['id']);
                                            $qs = "pub(".$item['id'].", '".$qs."', '".$qs2."', 'on', 'off');";
                                            echo '<td class="'.$row_class.'" valign="middle">
                                                    <a title="'.$_LANG['SHOW'].'" class="uittip" id="publink'.$item['id'].'" href="javascript:'.$qs.'"><img id="pub'.$item['id'].'" src="images/actions/off.gif" border="0"/></a>
                                                  </td>'. "\n";
                                        }
                                    } else {
                                        if (isset($_fields[$key]['prc'])) {
                                            // функция обработки под названием $_fields[$key]['prc']
                                            // какие параметры передать функции - один ключ или произвольный массив ключей
                                            if (is_array($_fields[$key]['field'])) {
                                                foreach ($_fields[$key]['field'] as $func_field) {
                                                    $in_func_array[$func_field] = $item[$func_field];
                                                }
                                                $data = call_user_func($_fields[$key]['prc'], $in_func_array);
                                            } else {
                                                $data = call_user_func($_fields[$key]['prc'], $item[$_fields[$key]['field']]);
                                            }
                                            if (is_array($data) && isset($data['link'])) {
                                                $data = str_replace('%id%', $item['id'], $data['link']);
                                            }
                                        } else {
                                            $data = $item[$_fields[$key]['field']];
                                            if (isset($_fields[$key]['maxlen'])) {
                                                if (mb_strlen($data)>$_fields[$key]['maxlen']) {
                                                    $data = mb_substr($data, 0, $_fields[$key]['maxlen']).'...';
                                                }
                                            }
                                        }

                                        //nested sets otstup
                                        if (isset($item['NSLevel']) && ($_fields[$key]['field'] == 'title' || (is_array($_fields[$key]['field']) && in_array('title', $_fields[$key]['field'])))) {
                                            $otstup = str_repeat('&nbsp;&nbsp;&nbsp;&nbsp;', ($item['NSLevel']-1));
                                            if ($item['NSLevel']-1 > 0) { $otstup .=  ' &raquo; '; }
                                        } else { $otstup = ''; }

                                        echo '<td class="'.$row_class.'" valign="middle">'.$otstup.$data.'</td>'. "\n";
                                    }
                                } else {
                                    if (isset($_fields[$key]['do'])) { $do = 'do=config&id='.(int)$_REQUEST['id'].'&'.$_fields[$key]['do']; } else { $do = 'do'; }
                                    if (isset($_fields[$key]['do_suffix'])) { $dos = $_fields[$key]['do_suffix']; $ids = 'item_id'; } else { $dos = ''; $ids = 'id'; }
                                    echo '<td class="'.$row_class.'" valign="middle">
                                            <a title="'.$_LANG['AD_DOWN'].'" href="?view='.cmsAdmin::getApplet().'&'.$do.'=move_down&co='.$item[$_fields[$key]['field']].'&'.$ids.'='.$item['id'].'"><img src="images/actions/down.gif" border="0"/></a>';
                                    if ($table != 'cms_menu' && $table != 'cms_category'){
                                        echo '<input class="lt_input" type="text" size="4" name="ordering[]" value="'.$item['ordering'].'" />';
                                        echo '<input name="ids[]" type="hidden" value="'.$item['id'].'" />';
                                    } else {
                                        echo '<input class="lt_input" type="text" size="4" name="ordering[]" value="'.$item['ordering'].'" disabled/>';
                                    }

                                    echo '<a title="'.$_LANG['AD_UP'].'" href="?view='.cmsAdmin::getApplet().'&'.$do.'=move_up&co='.$item[$_fields[$key]['field']].'&'.$ids.'='.$item['id'].'"><img src="images/actions/top.gif" border="0"/></a>'.
                                                        '</td>'. "\n";
                                }
                            }
                        }

                        if ($is_actions) {
                            echo '<td width="110" class="'.$row_class.'" align="right" valign="middle"><div style="padding-right:8px">';
                            
                            foreach($_actions as $action) {
                                if (isset($action['condition'])) {
                                    $print = $action['condition']($item);
                                } else {
                                    $print = true;
                                }

                                if ($print) {
                                    $icon   = $action['icon'];
                                    $title  = $action['title'];
                                    $link   = $action['link'];

                                    foreach($item as $f=>$v) {
                                        $link = str_replace('%'.$f.'%', $v, $link);
                                    }
                                    
                                    if (!isset($action['confirm'])) {
                                        echo '<a href="'. $link .'" class="uittip" title="'. $title .'"'. (isset($action['target']) ? ' target="'. $action['target'] .'"' : '') .'><img hspace="2" src="images/actions/'. $icon .'" border="0" alt="'. $title .'"/></a>';
                                    } else {
                                        echo '<a href="#" class="uittip" onclick="jsmsg(\''. $action['confirm'] .'\', \''. $link .'\')"  title="'. $title .'"><img hspace="2" src="images/actions/'. $icon .'" border="0" alt="'. $title .'"/></a>';
                                    }
                                }
                            }

                            echo '</div></td>'. "\n";
                        }

                        echo '</tr>'."\n";
                    }
                    
        echo '</tbody></table></form>';
        echo '<style>tr#lt_row2{ background:#eeeeee !important; } tr#lt_row1:hover td,tr#lt_row2:hover{ background:#cccccc !important; }</style>';
        echo '<script type="text/javascript">trClickChecked();</script>';

        $link = '?view='. cmsAdmin::getApplet();
        
        if ($sort) {
            $link .= '&sort='.$sort;
            if (cmsCore::inRequest('so')) { $link .= '&so='.cmsCore::request('so'); }
        }

        echo cmsPage::getPagebar($total, $page, $perpage, $_SERVER['PHP_SELF'] .'?'. cpAddParam($_SERVER['QUERY_STRING'], 'page', '%page%'));
    } else {
        echo '<p class="cp_message">'.$_LANG['OBJECTS_NOT_FOUND'].'</p>';
    }
}
예제 #2
0
파일: cp.php 프로젝트: deltas1/icms1
function cpListTable($table, $_fields, $_actions, $where = '', $orderby = 'title')
{
    $event = 'ADMIN_CPLISTTABLE_' . strtoupper($table) . '_' . strtoupper($GLOBALS['applet']) . (!empty($GLOBALS['component']) ? '_' . strtoupper($GLOBALS['component']) : '');
    list($table, $_fields, $_actions, $where, $orderby) = cmsCore::callEvent($event, array($table, $_fields, $_actions, $where, $orderby));
    global $_LANG;
    $inDB = cmsDatabase::getInstance();
    $perpage = 60;
    $sql = 'SELECT *';
    $is_actions = sizeof($_actions);
    foreach ($_fields as $key => $value) {
        if (isset($_fields[$key]['fdate'])) {
            $sql .= ", DATE_FORMAT(" . $_fields[$key]['field'] . ", '" . $_fields[$key]['fdate'] . "') as `" . $_fields[$key]['field'] . "`";
        }
    }
    $sql .= ' FROM ' . $table;
    if (isset($_SESSION['filter_table']) && $_SESSION['filter_table'] != $table) {
        unset($_SESSION['filter']);
    }
    if (cmsCore::inRequest('nofilter')) {
        unset($_SESSION['filter']);
        cmsCore::redirect('/admin/index.php?' . str_replace('&nofilter', '', $_SERVER['QUERY_STRING']));
    }
    $filter = false;
    if (cmsCore::inRequest('filter')) {
        $filter = cmsCore::request('filter', 'array_str', '');
        $_SESSION['filter'] = $filter;
    } elseif (isset($_SESSION['filter'])) {
        $filter = $_SESSION['filter'];
    }
    if ($filter) {
        $f = 0;
        $sql .= ' WHERE 1=1';
        foreach ($filter as $key => $value) {
            if ($filter[$key] && $filter[$key] != -100) {
                $sql .= ' AND ';
                if (!is_numeric($filter[$key])) {
                    $sql .= $key . " LIKE '%" . $filter[$key] . "%'";
                } else {
                    $sql .= $key . " = '" . $filter[$key] . "'";
                }
                $f++;
            }
        }
        if (!isset($_SESSION['filter'])) {
            $_SESSION['filter'] = $filter;
        }
    }
    if (mb_strlen($where) > 3) {
        if (mb_strstr($sql, 'WHERE')) {
            $sql .= ' AND ' . $where;
        } else {
            $sql .= ' WHERE ' . $where;
        }
    }
    $sort = cmsCore::request('sort', 'str', '');
    if ($sort == false) {
        if ($orderby) {
            $sort = $orderby;
        } else {
            foreach ($_fields as $key => $value) {
                if ($_fields[$key]['field'] == 'ordering' && $sort != 'NSLeft') {
                    $sort = 'ordering';
                    $so = 'asc';
                }
            }
        }
    }
    if ($sort) {
        $sql .= ' ORDER BY ' . $sort;
        if (cmsCore::inRequest('so')) {
            $sql .= ' ' . cmsCore::request('so', 'str', '');
        }
    }
    $page = cmsCore::request('page', 'int', 1);
    $total_rs = $inDB->query($sql);
    $total = $inDB->num_rows($total_rs);
    $sql .= " LIMIT " . ($page - 1) * $perpage . ", {$perpage}";
    $result = $inDB->query($sql);
    $_SESSION['filter_table'] = $table;
    if ($inDB->error()) {
        unset($_SESSION['filter']);
        cmsCore::redirect('/admin/index.php?' . $_SERVER['QUERY_STRING']);
    }
    $filters = 0;
    $f_html = '';
    //Find and render filters
    foreach ($_fields as $key => $value) {
        if (isset($_fields[$key]['filter'])) {
            $f_html .= '<td width="">' . $_fields[$key]['title'] . ': </td>';
            if (!isset($filter[$_fields[$key]['field']])) {
                $initval = '';
            } else {
                $initval = $filter[$_fields[$key]['field']];
            }
            $f_html .= '<td width="">';
            $inputname = 'filter[' . $_fields[$key]['field'] . ']';
            if (!isset($_fields[$key]['filterlist'])) {
                $f_html .= '<input name="' . $inputname . '" type="text" size="' . $_fields[$key]['filter'] . '" class="filter_input" value="' . $initval . '"/></td>';
            } else {
                $f_html .= cpBuildList($inputname, $_fields[$key]['filterlist'], $initval);
            }
            $f_html .= '</td>';
            $filters += 1;
            $_SERVER['QUERY_STRING'] = str_replace('filter[' . $_fields[$key]['field'] . ']=', '', $_SERVER['QUERY_STRING']);
        }
    }
    //draw filters
    if ($filters > 0) {
        echo '<div class="filter">';
        echo '<form name="filterform" action="index.php?' . $_SERVER['QUERY_STRING'] . '" method="POST">';
        echo '<table width="250"><tr>';
        echo $f_html;
        echo '<td width="80"><input type="submit" class="filter_submit" value="' . $_LANG['AD_FILTER'] . '" /></td>';
        if (@$f > 0) {
            echo '<td width="80"><input type="button" onclick="window.location.href=\'index.php?' . $_SERVER['QUERY_STRING'] . '&nofilter\'" class="filter_submit" value="' . $_LANG['AD_ALL'] . '" /></td>';
        }
        echo '</tr></table>';
        echo '</form>';
        echo '</div>';
    }
    if ($inDB->num_rows($result)) {
        //DRAW LIST TABLE
        echo '<form name="selform" action="index.php?view=' . $GLOBALS['applet'] . '&do=saveorder" method="post">';
        echo '<table id="listTable" border="0" class="tablesorter" width="100%" cellpadding="0" cellspacing="0">';
        //TABLE HEADING
        echo '<thead>' . "\n";
        echo '<tr>' . "\n";
        echo '<th width="20" class="lt_header" align="center"><a class="lt_header_link" href="javascript:invert();" title="' . $_LANG['AD_INVERT_SELECTION'] . '">#</a></th>' . "\n";
        foreach ($_fields as $key => $value) {
            echo '<th width="' . $_fields[$key]['width'] . '" class="lt_header">';
            echo $_fields[$key]['title'];
            echo '</th>' . "\n";
        }
        if ($is_actions) {
            echo '<th width="80" class="lt_header" align="center">' . $_LANG['AD_ACTIONS'] . '</th>' . "\n";
        }
        echo '</tr>' . "\n";
        echo '</thead><tbody>' . "\n";
        //TABLE BODY
        $r = 0;
        while ($item = $inDB->fetch_assoc($result)) {
            $r++;
            if ($r % 2) {
                $row_class = 'lt_row1';
            } else {
                $row_class = 'lt_row2';
            }
            echo '<tr id="lt_row2">' . "\n";
            echo '<td class="' . $row_class . '" align="center" valign="middle"><input type="checkbox" name="item[]" value="' . $item['id'] . '" /></td>' . "\n";
            foreach ($_fields as $key => $value) {
                if (isset($_fields[$key]['link'])) {
                    $link = str_replace('%id%', $item['id'], $_fields[$key]['link']);
                    if (isset($_fields[$key]['prc'])) {
                        // функция обработки под названием $_fields[$key]['prc']
                        // какие параметры передать функции - один ключ или произвольный массив ключей
                        if (is_array($_fields[$key]['field'])) {
                            foreach ($_fields[$key]['field'] as $func_field) {
                                $in_func_array[$func_field] = $item[$func_field];
                            }
                            $data = call_user_func($_fields[$key]['prc'], $in_func_array);
                        } else {
                            $data = call_user_func($_fields[$key]['prc'], $item[$_fields[$key]['field']]);
                        }
                    } else {
                        $data = $item[$_fields[$key]['field']];
                        if (isset($_fields[$key]['maxlen'])) {
                            if (mb_strlen($data) > $_fields[$key]['maxlen']) {
                                $data = mb_substr($data, 0, $_fields[$key]['maxlen']) . '...';
                            }
                        }
                    }
                    //nested sets otstup
                    if (isset($item['NSLevel']) && ($_fields[$key]['field'] == 'title' || is_array($_fields[$key]['field']) && in_array('title', $_fields[$key]['field']))) {
                        $otstup = str_repeat('&nbsp;&nbsp;&nbsp;&nbsp;', $item['NSLevel'] - 1);
                        if ($item['NSLevel'] - 1 > 0) {
                            $otstup .= ' &raquo; ';
                        }
                    } else {
                        $otstup = '';
                    }
                    if ($table != 'cms_components') {
                        echo '<td class="' . $row_class . '" valign="middle">' . $otstup . '<a class="lt_link" href="' . $link . '">' . $data . '</a></td>' . "\n";
                    } else {
                        $data = function_exists('cpComponentHasConfig') && cpComponentHasConfig($item['link']) ? '<a class="lt_link" href="' . $link . '">' . $data . '</a>' : $data;
                        echo '<td class="' . $row_class . '" valign="middle">
                                            <span class="lt_link" style="padding:1px; padding-left:24px; background:url(/admin/images/components/' . $item['link'] . '.png) no-repeat">' . $data . '</span>
                                      </td>' . "\n";
                    }
                } else {
                    if ($_fields[$key]['field'] != 'ordering') {
                        if ($_fields[$key]['field'] == 'published') {
                            if (isset($_fields[$key]['do'])) {
                                $do = $_fields[$key]['do'];
                            } else {
                                $do = 'do';
                            }
                            if (isset($_fields[$key]['do_suffix'])) {
                                $dos = $_fields[$key]['do_suffix'];
                                $ids = 'item_id';
                            } else {
                                $dos = '';
                                $ids = 'id';
                            }
                            if ($item['published']) {
                                $qs = cpAddParam($_SERVER['QUERY_STRING'], $do, 'hide' . $dos);
                                $qs = cpAddParam($qs, $ids, $item['id']);
                                $qs2 = cpAddParam($_SERVER['QUERY_STRING'], $do, 'show' . $dos);
                                $qs2 = cpAddParam($qs2, $ids, $item['id']);
                                $qs = "pub(" . $item['id'] . ", '" . $qs . "', '" . $qs2 . "', 'off', 'on');";
                                echo '<td class="' . $row_class . '" valign="middle">
												<a title="' . $_LANG['HIDE'] . '" class="uittip" id="publink' . $item['id'] . '" href="javascript:' . $qs . '"><img id="pub' . $item['id'] . '" src="images/actions/on.gif" border="0"/></a>
											 </td>' . "\n";
                            } else {
                                $qs = cpAddParam($_SERVER['QUERY_STRING'], $do, 'show' . $dos);
                                $qs = cpAddParam($qs, $ids, $item['id']);
                                $qs2 = cpAddParam($_SERVER['QUERY_STRING'], $do, 'hide' . $dos);
                                $qs2 = cpAddParam($qs2, $ids, $item['id']);
                                $qs = "pub(" . $item['id'] . ", '" . $qs . "', '" . $qs2 . "', 'on', 'off');";
                                echo '<td class="' . $row_class . '" valign="middle">
												<a title="' . $_LANG['SHOW'] . '" class="uittip" id="publink' . $item['id'] . '" href="javascript:' . $qs . '"><img id="pub' . $item['id'] . '" src="images/actions/off.gif" border="0"/></a>
											 </td>' . "\n";
                            }
                        } else {
                            if (isset($_fields[$key]['prc'])) {
                                // функция обработки под названием $_fields[$key]['prc']
                                // какие параметры передать функции - один ключ или произвольный массив ключей
                                if (is_array($_fields[$key]['field'])) {
                                    foreach ($_fields[$key]['field'] as $func_field) {
                                        $in_func_array[$func_field] = $item[$func_field];
                                    }
                                    $data = call_user_func($_fields[$key]['prc'], $in_func_array);
                                } else {
                                    $data = call_user_func($_fields[$key]['prc'], $item[$_fields[$key]['field']]);
                                }
                            } else {
                                $data = $item[$_fields[$key]['field']];
                                if (isset($_fields[$key]['maxlen'])) {
                                    if (mb_strlen($data) > $_fields[$key]['maxlen']) {
                                        $data = mb_substr($data, 0, $_fields[$key]['maxlen']) . '...';
                                    }
                                }
                            }
                            //nested sets otstup
                            if (isset($item['NSLevel']) && ($_fields[$key]['field'] == 'title' || is_array($_fields[$key]['field']) && in_array('title', $_fields[$key]['field']))) {
                                $otstup = str_repeat('&nbsp;&nbsp;&nbsp;&nbsp;', $item['NSLevel'] - 1);
                                if ($item['NSLevel'] - 1 > 0) {
                                    $otstup .= ' &raquo; ';
                                }
                            } else {
                                $otstup = '';
                            }
                            echo '<td class="' . $row_class . '" valign="middle">' . $otstup . $data . '</td>' . "\n";
                        }
                    } else {
                        if (isset($_fields[$key]['do'])) {
                            $do = 'do=config&id=' . (int) $_REQUEST['id'] . '&' . $_fields[$key]['do'];
                        } else {
                            $do = 'do';
                        }
                        if (isset($_fields[$key]['do_suffix'])) {
                            $dos = $_fields[$key]['do_suffix'];
                            $ids = 'item_id';
                        } else {
                            $dos = '';
                            $ids = 'id';
                        }
                        echo '<td class="' . $row_class . '" valign="middle">
									<a title="' . $_LANG['AD_DOWN'] . '" href="?view=' . $GLOBALS['applet'] . '&' . $do . '=move_down&co=' . $item[$_fields[$key]['field']] . '&' . $ids . '=' . $item['id'] . '"><img src="images/actions/down.gif" border="0"/></a>';
                        if ($table != 'cms_menu' && $table != 'cms_category') {
                            echo '<input class="lt_input" type="text" size="4" name="ordering[]" value="' . $item['ordering'] . '" />';
                            echo '<input name="ids[]" type="hidden" value="' . $item['id'] . '" />';
                        } else {
                            echo '<input class="lt_input" type="text" size="4" name="ordering[]" value="' . $item['ordering'] . '" disabled/>';
                        }
                        echo '<a title="' . $_LANG['AD_UP'] . '" href="?view=' . $GLOBALS['applet'] . '&' . $do . '=move_up&co=' . $item[$_fields[$key]['field']] . '&' . $ids . '=' . $item['id'] . '"><img src="images/actions/top.gif" border="0"/></a>' . '</td>' . "\n";
                    }
                }
            }
            if ($is_actions) {
                echo '<td width="110" class="' . $row_class . '" align="right" valign="middle"><div style="padding-right:8px">';
                foreach ($_actions as $key => $value) {
                    if (isset($_actions[$key]['condition'])) {
                        $print = $_actions[$key]['condition']($item);
                    } else {
                        $print = true;
                    }
                    if ($print) {
                        $icon = $_actions[$key]['icon'];
                        $title = $_actions[$key]['title'];
                        $link = $_actions[$key]['link'];
                        foreach ($item as $f => $v) {
                            $link = str_replace('%' . $f . '%', $v, $link);
                        }
                        if (!isset($_actions[$key]['confirm'])) {
                            echo '<a href="' . $link . '" class="uittip" title="' . $title . '"><img hspace="2" src="images/actions/' . $icon . '" border="0" alt="' . $title . '"/></a>';
                        } else {
                            echo '<a href="#" class="uittip" onclick="jsmsg(\'' . $_actions[$key]['confirm'] . '\', \'' . $link . '\')" title="' . $title . '"><img hspace="2" src="images/actions/' . $icon . '" border="0" alt="' . $title . '"/></a>';
                        }
                    }
                }
                echo '</div></td>' . "\n";
            }
            echo '</tr>' . "\n";
        }
        echo '</tbody></table></form>';
        echo '<script type="text/javascript">highlightTableRows("listTable","hoverRow","clickedRow");</script>';
        echo '<script type="text/javascript">activateListTable("listTable");</script>';
        $link = '?view=' . $GLOBALS['applet'];
        if ($sort) {
            $link .= '&sort=' . $sort;
            if (cmsCore::inRequest('so')) {
                $link .= '&so=' . cmsCore::request('so');
            }
        }
        echo cmsPage::getPagebar($total, $page, $perpage, $_SERVER['PHP_SELF'] . '?' . cpAddParam($_SERVER['QUERY_STRING'], 'page', '%page%'));
    } else {
        echo '<p class="cp_message">' . $_LANG['OBJECTS_NOT_FOUND'] . '</p>';
    }
}
예제 #3
0
파일: cp.php 프로젝트: CMS-RuDi/CMS-RuDi
function cpListTable($table, $_fields, $_actions, $where = '', $orderby = 'title', $perpage = 60)
{
    global $_LANG;
    $page = cmsCore::request('page', 'int', 1);
    $sql = 'SELECT *';
    $is_actions = sizeof($_actions);
    foreach ($_fields as $key => $value) {
        if (isset($_fields[$key]['fdate'])) {
            $sql .= ", DATE_FORMAT(" . $_fields[$key]['field'] . ", '" . $_fields[$key]['fdate'] . "') as `" . $_fields[$key]['field'] . "`";
        }
    }
    $sql .= ' FROM ' . $table;
    if (isset($_SESSION['filter_table']) && $_SESSION['filter_table'] != $table) {
        unset($_SESSION['filter']);
    }
    if (cmsCore::inRequest('nofilter')) {
        unset($_SESSION['filter']);
        cmsCore::redirect('/admin/index.php?' . cpAddParam($_SERVER['QUERY_STRING'], 'page', 1));
    }
    $filter = false;
    if (cmsCore::inRequest('filter')) {
        $filter = cmsCore::request('filter', 'array_str', '');
        if (isset($_SESSION['filter']) && $_SESSION['filter'] != $filter) {
            $page = 1;
        }
        $_SESSION['filter'] = $filter;
    } else {
        if (isset($_SESSION['filter'])) {
            $filter = $_SESSION['filter'];
        }
    }
    if ($filter) {
        $f = 0;
        foreach ($filter as $key => $value) {
            if (!empty($filter[$key]) && $filter[$key] != -100) {
                if (!is_numeric($filter[$key])) {
                    cmsCore::c('db')->where($key . " LIKE '%" . $filter[$key] . "%'");
                } else {
                    cmsCore::c('db')->where($key . " = '" . $filter[$key] . "'");
                }
                $f++;
            }
        }
        if (!isset($_SESSION['filter'])) {
            $_SESSION['filter'] = $filter;
        }
    }
    if (mb_strlen($where) <= 3) {
        $where = '1=1';
    }
    //Выставляем сортировку выборки данных в БД
    $sort = cmsCore::request('sort', 'str', '');
    $so = 'asc';
    if (empty($sort)) {
        if (!empty($orderby)) {
            $orderby = trim($orderby);
            if (mb_strstr($orderby, ' ')) {
                $sorta = explode(' ', $orderby);
                $sort = $sorta[0];
                foreach ($sorta as $s) {
                    $s = mb_strtolower(trim($s, ' ,'));
                    if ($s == 'asc' || $s == 'desc') {
                        $so = $s;
                        break;
                    }
                }
            } else {
                $sort = $orderby;
            }
            cmsCore::c('db')->order_by = 'ORDER BY ' . $orderby;
        } else {
            foreach ($_fields as $key => $value) {
                if ($_fields[$key]['field'] == 'ordering' && $sort != 'NSLeft') {
                    $sort = 'ordering';
                    cmsCore::c('db')->orderBy($sort, $so);
                }
            }
        }
    } else {
        $so = cmsCore::request('so', array('asc', 'desc'), $so);
        cmsCore::c('db')->orderBy($sort, $so);
    }
    $total = cmsCore::c('db')->rows_count($table, $where . ' ' . cmsCore::c('db')->where);
    cmsCore::c('db')->limitPage($page, $perpage);
    $result = cmsCore::c('db')->query($sql . "\n" . 'WHERE ' . $where . ' ' . cmsCore::c('db')->where . "\n" . (empty(cmsCore::c('db')->order_by) ? '' : cmsCore::c('db')->order_by) . ' LIMIT ' . cmsCore::c('db')->limit);
    $_SESSION['filter_table'] = $table;
    if (cmsCore::c('db')->error()) {
        unset($_SESSION['filter']);
        cmsCore::redirect('/admin/index.php?' . $_SERVER['QUERY_STRING']);
    }
    $lt_form_field = cmsCore::c('page')->initTemplate('special', 'list_table_filter')->fetch();
    $filters = 0;
    $f_html = '';
    //Find and render filters
    foreach ($_fields as $key => $value) {
        if (isset($_fields[$key]['filter'])) {
            $f_html .= str_replace('%title%', $_fields[$key]['title'], $lt_form_field);
            $initval = false;
            if (isset($filter[$_fields[$key]['field']])) {
                $initval = $filter[$_fields[$key]['field']];
            }
            $inputname = 'filter[' . $_fields[$key]['field'] . ']';
            if (!isset($_fields[$key]['filterlist'])) {
                $f_html .= str_replace('%field%', '<input type="text" class="form-control" name="' . $inputname . '" size="' . $_fields[$key]['filter'] . '" value="' . ($initval === false ? '' : $initval) . '" />', $f_html);
            } else {
                $f_html .= str_replace('%field%', cpBuildList($inputname, $_fields[$key]['filterlist'], $initval), $f_html);
            }
            $filters += 1;
            $_SERVER['QUERY_STRING'] = str_replace('filter[' . $_fields[$key]['field'] . ']=', '', $_SERVER['QUERY_STRING']);
        }
    }
    foreach ($_fields as $key => $value) {
        if (!is_array($_fields[$key]['field'])) {
            $_fields[$key]['sort_link'] = 'index.php?' . cpAddParam($_SERVER['QUERY_STRING'], array(0 => 'sort', 1 => 'so'), array(0 => $_fields[$key]['field'], 1 => $so == 'asc' ? 'desc' : 'asc'));
        }
    }
    $items = $actions = array();
    if (cmsCore::c('db')->num_rows($result)) {
        while ($item = cmsCore::c('db')->fetch_assoc($result)) {
            $itms = array();
            foreach ($_fields as $key => $value) {
                $it = array();
                if (isset($_fields[$key]['link'])) {
                    $it['type'] = 'link';
                    $it['link'] = str_replace('%id%', $item['id'], $_fields[$key]['link']);
                    if (isset($_fields[$key]['prc'])) {
                        // функция обработки под названием $_fields[$key]['prc']
                        // какие параметры передать функции - один ключ или произвольный массив ключей
                        if (is_array($_fields[$key]['field'])) {
                            foreach ($_fields[$key]['field'] as $func_field) {
                                $in_func_array[$func_field] = $item[$func_field];
                            }
                            $it['title'] = call_user_func($_fields[$key]['prc'], $in_func_array);
                        } else {
                            $it['title'] = call_user_func($_fields[$key]['prc'], $item[$_fields[$key]['field']]);
                        }
                    } else {
                        $it['title'] = $item[$_fields[$key]['field']];
                        if (isset($_fields[$key]['maxlen'])) {
                            if (mb_strlen($it['title']) > $_fields[$key]['maxlen']) {
                                $it['title'] = mb_substr($it['title'], 0, $_fields[$key]['maxlen']) . '...';
                            }
                        }
                    }
                    //nested sets otstup
                    if (isset($item['NSLevel']) && ($_fields[$key]['field'] == 'title' || is_array($_fields[$key]['field']) && in_array('title', $_fields[$key]['field']))) {
                        $otstup = str_repeat('&nbsp;&nbsp;&nbsp;&nbsp;', $item['NSLevel'] - 1);
                        if ($item['NSLevel'] - 1 > 0) {
                            $otstup .= ' &raquo; ';
                        }
                    } else {
                        $otstup = '';
                    }
                    $it['otstup'] = $otstup;
                    if ($table == 'cms_components') {
                        if (!function_exists('cpComponentHasConfig') || !cpComponentHasConfig($item['link'])) {
                            unset($it['link']);
                        }
                        $it['icon'] = '/admin/images/components/' . $item['link'] . '.png';
                    }
                } else {
                    if ($_fields[$key]['field'] != 'ordering') {
                        if ($_fields[$key]['field'] == 'published' || isset($_fields[$key]['published'])) {
                            $it['type'] = 'published';
                            if (isset($_fields[$key]['do'])) {
                                $do = $_fields[$key]['do'];
                            } else {
                                $do = 'do';
                            }
                            if (isset($_fields[$key]['do_suffix'])) {
                                $dos = $_fields[$key]['do_suffix'];
                                $ids = 'item_id';
                            } else {
                                $dos = '';
                                $ids = 'id';
                            }
                            if ($item[$_fields[$key]['field']]) {
                                $qs = cpAddParam($_SERVER['QUERY_STRING'], $do, 'hide' . $dos);
                                $qs = cpAddParam($qs, $ids, $item['id']);
                                $qs2 = cpAddParam($_SERVER['QUERY_STRING'], $do, 'show' . $dos);
                                $qs2 = cpAddParam($qs2, $ids, $item['id']);
                                $it['link'] = "javascript:pub(" . $item['id'] . ", '" . $qs . "', '" . $qs2 . "', 'off', 'on');";
                                $it['icon'] = 'images/actions/on.gif';
                                $it['title'] = $_LANG['HIDE'];
                            } else {
                                $qs = cpAddParam($_SERVER['QUERY_STRING'], $do, 'show' . $dos);
                                $qs = cpAddParam($qs, $ids, $item['id']);
                                $qs2 = cpAddParam($_SERVER['QUERY_STRING'], $do, 'hide' . $dos);
                                $qs2 = cpAddParam($qs2, $ids, $item['id']);
                                $it['link'] = "javascript:pub(" . $item['id'] . ", '" . $qs . "', '" . $qs2 . "', 'on', 'off');";
                                $it['icon'] = 'images/actions/off.gif';
                                $it['title'] = $_LANG['SHOW'];
                            }
                        } else {
                            $it['type'] = 'default';
                            if (isset($_fields[$key]['prc'])) {
                                // функция обработки под названием $_fields[$key]['prc']
                                // какие параметры передать функции - один ключ или произвольный массив ключей
                                if (is_array($_fields[$key]['field'])) {
                                    foreach ($_fields[$key]['field'] as $func_field) {
                                        $in_func_array[$func_field] = $item[$func_field];
                                    }
                                    $it['title'] = call_user_func($_fields[$key]['prc'], $in_func_array);
                                } else {
                                    $it['title'] = call_user_func($_fields[$key]['prc'], $item[$_fields[$key]['field']]);
                                }
                                if (is_array($it['title']) && isset($it['title']['link'])) {
                                    $it['title'] = str_replace('%id%', $item['id'], $it['title']['link']);
                                }
                            } else {
                                $it['title'] = $item[$_fields[$key]['field']];
                                if (isset($_fields[$key]['maxlen'])) {
                                    if (mb_strlen($it['title']) > $_fields[$key]['maxlen']) {
                                        $it['title'] = mb_substr($it['title'], 0, $_fields[$key]['maxlen']) . '...';
                                    }
                                }
                            }
                            //nested sets otstup
                            if (isset($item['NSLevel']) && ($_fields[$key]['field'] == 'title' || is_array($_fields[$key]['field']) && in_array('title', $_fields[$key]['field']))) {
                                $otstup = str_repeat('&nbsp;&nbsp;&nbsp;&nbsp;', $item['NSLevel'] - 1);
                                if ($item['NSLevel'] - 1 > 0) {
                                    $otstup .= ' &raquo; ';
                                }
                            } else {
                                $otstup = '';
                            }
                            $it['otstup'] = $otstup;
                        }
                    } else {
                        $it['type'] = 'ordering';
                        if (isset($_fields[$key]['do'])) {
                            $do = 'do=config&id=' . (int) $_REQUEST['id'] . '&' . $_fields[$key]['do'];
                        } else {
                            $do = 'do';
                        }
                        if (isset($_fields[$key]['do_suffix'])) {
                            $dos = $_fields[$key]['do_suffix'];
                            $ids = 'item_id';
                        } else {
                            $dos = '';
                            $ids = 'id';
                        }
                        $it['link_down'] = '?view=' . cmsAdmin::getApplet() . '&' . $do . '=move_down&co=' . $item[$_fields[$key]['field']] . '&' . $ids . '=' . $item['id'];
                        $it['link_up'] = '?view=' . cmsAdmin::getApplet() . '&' . $do . '=move_up&co=' . $item[$_fields[$key]['field']] . '&' . $ids . '=' . $item['id'];
                        $it['value'] = $item['ordering'];
                    }
                }
                $itms[] = $it;
            }
            if ($is_actions) {
                $actions[$item['id']] = array();
                foreach ($_actions as $action) {
                    if (isset($action['condition'])) {
                        $print = $action['condition']($item);
                    } else {
                        $print = true;
                    }
                    if ($print) {
                        $icon = $action['icon'];
                        $title = $action['title'];
                        $link = $action['link'];
                        foreach ($item as $f => $v) {
                            $link = str_replace('%' . $f . '%', $v, $link);
                            if (isset($action['confirm'])) {
                                $action['confirm'] = str_replace('%' . $f . '%', $v, $action['confirm']);
                            }
                        }
                        $acts = array('link' => $link, 'title' => $title, 'icon' => 'images/actions/' . $icon, 'target' => isset($action['target']) ? $action['target'] : false);
                        if (isset($action['confirm'])) {
                            $acts['link'] = "javascript:jsmsg('" . $action['confirm'] . "', '" . $link . "');";
                        }
                        $actions[$item['id']][] = $acts;
                    }
                }
            }
            $items[$item['id']] = $itms;
        }
        $link = '?view=' . cmsAdmin::getApplet();
        if ($sort) {
            $link .= '&sort=' . $sort;
            if (cmsCore::inRequest('so')) {
                $link .= '&so=' . cmsCore::request('so');
            }
        }
    }
    cmsCore::c('page')->initTemplate('special', 'list_table')->assign('applet', cmsAdmin::getApplet())->assign('filters', $filters)->assign('f_html', $f_html)->assign('f', $f)->assign('query_str', $_SERVER['QUERY_STRING'])->assign('sort', $sort)->assign('so', $so)->assign('actions', $actions)->assign('table', $table)->assign('fields', $_fields)->assign('items', $items)->display();
    echo cmsPage::getPagebar($total, $page, $perpage, $_SERVER['PHP_SELF'] . '?' . cpAddParam($_SERVER['QUERY_STRING'], 'page', '%page%'));
}