⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 categories.php

📁 这是一个joomla系统的插件, 这是一个joomla系统的插件
💻 PHP
字号:
<?php
/**
* @package EasyGallery
* @copyright (C) 2006 Joomla-addons.org
* @author  Adam van Dongen
* @version $Id: categories.php 4 2007-06-16 14:22:19Z websmurf $
* 
* --------------------------------------------------------------------------------
* All rights reserved. Easy Gallery Component for Joomla!
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the Joomla-addons Free Software License 
* See LICENSE.php for more information.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
* --------------------------------------------------------------------------------
**/

defined( '_VALID_MOS' ) or die( 'Restricted access' );


class categories {
  
  /**
   * Show all categories
   *
   */
  function show(){
    global $database, $mainframe, $option;
    
  	$limit = intval( $mainframe->getUserStateFromRequest( "viewlistlimit", 'limit', $mainframe->getCfg('list_limit') ) );
  	$limitstart	= intval( $mainframe->getUserStateFromRequest( "view{$option}limitstart", 'limitstart', 0 ) );
  	$cid	= intval( $mainframe->getUserStateFromRequest( "view{$option}cid", 'cid', 0 ) );
  	$search	= $mainframe->getUserStateFromRequest( "search{$option}", 'search', '' );
  	$search	= $database->getEscaped( trim( strtolower( $search ) ) );
  	
  	$where = array();
  	$where[] = "section = 'com_easygallery'";
  	if(!empty($search)){
  	  $where[] = "c.title LIKE '%" . $search . "%'";
  	}
  	if($cid > 0){
  	  $where[] = 'c.parent_id = ' . $cid;
  	}
  	
  	$basequery = "SELECT %s FROM #__categories c ";
  	if(sizeof($where) > 0){
  	  $basequery .= "WHERE " . implode(" AND ", $where);
  	}
  	$basequery .= " ORDER BY parent_id, ordering";
  	
  	$query = str_replace('%s', 'COUNT(1)', $basequery);
  	$database->setQuery($query);
  	$total = $database->loadResult();
  	
  	require_once($mainframe->getCfg('absolute_path') . '/administrator/includes/pageNavigation.php');
  	$pageNav = new mosPageNav($total, $limitstart, $limit);
  	
  	$query = str_replace("%s", "*", $basequery);
  	$database->setQuery($query);
  	$rows = $database->loadObjectList();
  	echo $database->getErrorMsg();
  	
  	/** sort accoording to level **/
  	$level = 0;
    $tree = array();
    //sort by parent
    for($i=0,$n=count($rows);$i<$n;$i++){
      $row = $rows[$i];
      $tree[$row->parent_id][] = $row;
    }
    
    $final = array();
    $level = 0;
    //loop through all items
    if(isset($tree[$cid])){
      for($i=0,$n=count($tree[$cid]);$i<$n;$i++){
        $row = $tree[$cid][$i];
        categories::recurseCategorie($tree, $final, $row, $level);
      }
    }
    $final = array_slice($final, $limitstart, $limit);
    
  	$lists['search'] = $search;
  	
  	$categories = categories::getCategorieTree();
  	
  	$obj = new stdClass();
  	$obj->id = 0;
  	$obj->name = ' - all - ';
  	
  	array_unshift($categories, $obj);
  	
  	$lists = array();
  	$lists['cid'] = mosHTML::selectList($categories, 'cid', 'onchange="document.adminForm.submit();"', 'id', 'name', $cid);
  	
  	HTML_easygallery::showCategories($final, $pageNav, $lists);
  }
  
  /**
   * Edit or add a category
   *
   * @param int $id
   */
  function edit($id){
    global $database, $my;
    
    $row = new mosCategory($database);
    $row->load($id);
    
    if(!$row->id){
      $row->published = 1;
    }
    
    $categories = categories::getCategorieTree();
    
    $obj = new stdClass();
    $obj->id = 0;
    $obj->name = 'Top';
    
    array_unshift($categories, $obj);
    
    $lists = array();
  	$lists['parent_id'] = mosHTML::selectList( $categories, 'parent_id', 'class="inputbox" size="1"', 'id', 'name', $row->parent_id );
  	$lists['published'] = mosHTML::yesnoRadioList('published', '', intval($row->published));
  	$lists['access'] = mosAdminMenus::Access( $row );
    
    HTML_easygallery::editCategory($row, $lists);
  }
  
  /**
   * Save changes to category
   *
   */
  function save($redirect = true){
    global $database, $task, $option, $my;
    
    $row = new mosCategory($database);
    $row->bind($_POST);
    
    if(!$row->id){
      $row->ordering = 999999999;
    }
    
    // avoid linking category to itself
    if($row->parent_id == $row->id){
      $row->parent_id = 0;
    }
    
    $row->section = 'com_easygallery';
    
    $row->published = intval(mosGetParam($_REQUEST, 'published', 0));
    
    if (!$row->check()) {
			echo "<script> alert('".$row->getError()."'); window.history.go(-1); </script>\n";
			exit();
		}
		if (!$row->store()) {
			echo "<script> alert('".$row->getError()."'); window.history.go(-1); </script>\n";
			exit();
		}
		
		$row->updateOrder('section = \'com_easygallery\'');
		
		if($redirect){
  		switch ($task){
  		  case 'save':
  		    mosRedirect('index2.php?option=' . $option . '&act=categories', 'Changes to category saved');
  		    break;
  		  case 'apply':
  		    mosRedirect('index2.php?option=' . $option . '&act=categories&task=edit&cid=' . $row->id . '&hidemainmenu=1', 'Changes to category saved');
  		    break;
  		}
		} else {
		  return $row->id;
		}
  }
  
  /**
   * Remove Categorie(s)
   *
   * @param array ids
   */
  function remove($cid, $redirect = true){
    global $database, $option;
    
    $row = new mosCategory($database);
    
    for($i=0,$n=count($cid);$i<$n;$i++){
      $id = $cid[$i];
      
      //check for photos
      $query = "SELECT COUNT(1) FROM #__easygallery WHERE cid = " . (int) $id;
      $database->setQuery($query);
      $total = $database->loadResult();
      
      if($total > 0){
        echo "<script> alert('" . addslashes(EG_CAT_DELETE_STILL_PHOTOS) . "'); window.history.go(-1); </script>\n";
  			exit();
      }
      
      //check for subcategories
      $query = "SELECT COUNT(1) FROM #__categories WHERE parent_id = $id";
      $database->setQuery($query);
      $total = $database->loadResult();
      
      if($total > 0){
        echo "<script> alert('" . addslashes(EG_CAT_DELETE_STILL_SUBCATS) . "'); window.history.go(-1); </script>\n";
  			exit();
      }
      
      $row->delete($id);
    }
    
    if($redirect){
      mosRedirect('index2.php?option=' . $option . '&act=categories', 'Selected categories removed');
    }
  }
  
  /**
   * Set a Category state
   *
   * @param int id
   * @param int published
   */
  function setState($id, $published){
    global $database, $option;
    
    $row = new mosCategory($database);
    $row->load($id);
    
    $row->published = $published;
    
    if (!$row->store()) {
			echo "<script> alert('".$row->getError()."'); window.history.go(-1); </script>\n";
			exit();
		}
    
    mosRedirect('index2.php?option=' . $option . '&act=categories');
  }
  
  /**
   * Move an item up or down
   *
   * @param int $id
   * @param int $direction
   */
  function reorder($id, $direction){
    global $database, $option;
    
    $row = new mosCategory($database);
    $row->load($id);
    
    $row->move($direction, 'section = \'com_easygallery\'');
    
    mosRedirect('index2.php?option=' . $option . '&act=categories');
  }
  
  /**
   * Change ordering of a lot of items
   *
   * @param array $cid
   */
  function saveOrder($cid){
    global $database, $option;
    
    $order = mosGetParam( $_POST, 'order', array(0) );
    $row = new mosCategory($database);
    $conditions = array();
    
    for($i=0,$n=count($cid);$i<$n;$i++){
			$row->load($cid[$i]);
			
			if ($row->ordering != $order[$i]) {
				$row->ordering = $order[$i];
				
				if (!$row->store()) {
				  echo "<script> alert('".$database->getErrorMsg()."'); window.history.go(-1); </script>\n";
  				exit();
				}
				
				$condition = "section = 'com_easygallery'";
				$found = false;
				
				for($j=0,$k=count($conditions);$j<$k;$j++){
				  $cond = $conditions[$j];

				  if ($cond[1] == $condition) {
						$found = true;
						break;
					}
				}
			  if (!$found){
				  $conditions[] = array ($row->id, $condition);
				}
			}
		}

		for($i=0,$n=count($conditions);$i<$n;$i++){
		  $condition = $conditions[$i];
		  
			$row->load($condition[0]);
			$row->updateOrder($condition[1]);
		}
		
		mosRedirect('index2.php?option=' . $option . '&act=categories');
  }
  
  /**
   * Get the categorie tree
   *
   * @return array
   */
  function getCategorieTree($published_only = false){
    global $mainframe, $database;
    
    $query = "SELECT * FROM #__categories 
      WHERE section = 'com_easygallery' ";
    if($published_only){
      $query .= "AND published = 1";
    }
    $query .= "ORDER BY parent_id, ordering";
    $database->setQuery($query);
    $rows = $database->loadObjectList();
    
    $level = 0;
    $tree = array();
    //sort by parent
    for($i=0,$n=count($rows);$i<$n;$i++){
      $row = $rows[$i];
      
      $tree[$row->parent_id][] = $row;
    }
    
    $final = array();
    $level = 0;
    //loop through all items
    for($i=0,$n=count($tree[0]);$i<$n;$i++){
      $row = $tree[0][$i];
      categories::recurseCategorie($tree, $final, $row, $level);
    }
    
    return $final;
  }
  
  /**
   * Recurse categories
   *
   * @param array all categories
   * @param array categorie tree
   * @param <var>dbDisplayCategorie</var> $row
   * @param int current level
   */
  function recurseCategorie($rows, &$tree, $row, $level){

    $name = '';
    for($i=0;$i<$level;$i++){
      $name .= '--';
    }
    if($level != 0){
      $name = '|' . $name;
    }
    $row->name = $name . ' ' . $row->name;
    
    $tree[] = $row;
    if(@sizeof($rows[$row->id])){
      for($i=0,$n=count($rows[$row->id]);$i<$n;$i++){
        
        $child = $rows[$row->id][$i];
        
        categories::recurseCategorie($rows, $tree, $child, $level + 1);  
      }
    }
  }
  
  /**
   * Show all images and subcategories from a certain category
   */
  function viewFront($cid, $limitstart, $limit, $params){
    global $database, $option, $my, $mainframe;
    
    $cid = intval($cid);
    
    $row = new mosCategory($database);
    $row->load($cid);
    
    if( (($row->id > 0) && ($row->access > $my->gid)) || ($row->published < 1 && $my->gid != 2 && $row->id) ){
  		mosNotAuth();
  		return;
    }
    
    require_once($mainframe->getCfg('absolute_path') . '/includes/pageNavigation.php');
    
    //get categories
    $query = 'SELECT c.*, e.path AS photo_path, e.name AS photo_name FROM #__categories AS c
      LEFT JOIN #__easygallery AS e ON c.id = e.cid AND e.default = 1
      WHERE section = \'com_easygallery\' 
      AND parent_id = ' . (int) $cid . ' ';
    if($my->gid != 2){
      $query .= 'AND published = 1 ';
    }
    $query .= 'AND access <= ' . (int) $my->gid . '
      GROUP BY c.id
      ORDER BY ordering';
    $database->setQuery($query);
    $subcats = $database->loadObjectList();
    echo $database->getErrorMsg();
    
    //get photo's
    $query = 'SELECT * FROM #__easygallery 
      WHERE cid = ' . (int) $cid . ' ';
    if($my->gid != 2){
      $query .= 'AND state = 1 ';
    }
    $query .= 'ORDER BY ordering';
    $database->setQuery(str_replace('*', 'COUNT(1)', $query));
    $total = $database->loadResult();
    
    $database->setQuery($query . ' LIMIT ' . $limitstart . ', ' . $limit);
    $photos = $database->loadObjectList();
    echo $database->getErrorMsg();
    
    $pageNav = new mosPageNav($total, $limitstart, $limit);
    
    HTML_easygallery::showCategory($row, $subcats, $photos, $pageNav, $params);
  }
    
  /**
   * Edit a category in the frontend
   *
   * @param int $id
   */
  function editFront($id){
    global $database, $my;
    
    if(!$my->id && $my->gid != 2){
      mosNotAuth();
      return;
    }
    
    $row = new mosCategory($database);
    $row->load($id);
    
    if(!$row->id){
      $row->published = 1;
      $row->parent_id = intval(mosGetParam($_REQUEST, 'cid', 0));
    }
    
    $categories = categories::getCategorieTree();
    
    $obj = new stdClass();
    $obj->id = 0;
    $obj->name = 'Top';
    
    array_unshift($categories, $obj);
    
    $lists = array();
  	$lists['parent_id'] = mosHTML::selectList( $categories, 'parent_id', 'class="inputbox" size="1"', 'id', 'name', $row->parent_id );
  	$lists['published'] = mosHTML::yesnoRadioList('published', '', intval($row->published));
  	$lists['access'] = mosAdminMenus::Access( $row );
    
    HTML_easygallery::editCategory($row, $lists);
  }
  
  /**
   * Save changes in the frontend
   *
   * @return int ID
   */
  function saveFront(){
    global $database, $task, $option, $my, $Itemid;
    
    if(!$my->id && $my->gid != 2){
      mosNotAuth();
      return;
    }
    
    $row = new mosCategory($database);
    $row->bind($_POST);
    
    if(!$row->id){
      $row->ordering = 999999999;
    }
    
    // avoid linking category to itself
    if($row->parent_id == $row->id){
      $row->parent_id = 0;
    }
    
    $row->section = 'com_easygallery';
    
    $row->published = intval(mosGetParam($_REQUEST, 'published', 0));
    
    if (!$row->check()) {
			echo "<script> alert('".$row->getError()."'); window.history.go(-1); </script>\n";
			exit();
		}
		if (!$row->store()) {
			echo "<script> alert('".$row->getError()."'); window.history.go(-1); </script>\n";
			exit();
		}
		
		$row->updateOrder('section = \'com_easygallery\'');
		
		switch ($task){
		  case 'apply':
		    mosRedirect('index.php?option=' . $option . '&task=editCategory&cid=' . $row->id . '&Itemid=' .$Itemid);
		    break;
		  case 'save':
		    mosRedirect('index.php?option=' . $option . '&act=categories&cid=' . $row->id . '&Itemid=' .$Itemid);
		    break;
		}
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -