home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / CMS / xoops-2.0.18.1.exe / xoops-2.0.18.1 / htdocs / kernel / tplfile.php < prev    next >
Encoding:
PHP Script  |  2007-10-19  |  13.3 KB  |  326 lines

  1. <?php
  2. // $Id: tplfile.php 1102 2007-10-19 02:55:52Z dugris $
  3. //  ------------------------------------------------------------------------ //
  4. //                XOOPS - PHP Content Management System                      //
  5. //                    Copyright (c) 2000 XOOPS.org                           //
  6. //                       <http://www.xoops.org/>                             //
  7. //  ------------------------------------------------------------------------ //
  8. //  This program is free software; you can redistribute it and/or modify     //
  9. //  it under the terms of the GNU General Public License as published by     //
  10. //  the Free Software Foundation; either version 2 of the License, or        //
  11. //  (at your option) any later version.                                      //
  12. //                                                                           //
  13. //  You may not change or alter any portion of this comment or credits       //
  14. //  of supporting developers from this source code or any supporting         //
  15. //  source code which is considered copyrighted (c) material of the          //
  16. //  original comment or credit authors.                                      //
  17. //                                                                           //
  18. //  This program is distributed in the hope that it will be useful,          //
  19. //  but WITHOUT ANY WARRANTY; without even the implied warranty of           //
  20. //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
  21. //  GNU General Public License for more details.                             //
  22. //                                                                           //
  23. //  You should have received a copy of the GNU General Public License        //
  24. //  along with this program; if not, write to the Free Software              //
  25. //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //
  26. //  ------------------------------------------------------------------------ //
  27. // Author: Kazumi Ono (AKA onokazu)                                          //
  28. // URL: http://www.myweb.ne.jp/, http://www.xoops.org/, http://jp.xoops.org/ //
  29. // Project: The XOOPS Project                                                //
  30. // ------------------------------------------------------------------------- //
  31. if (!defined('XOOPS_ROOT_PATH')) {
  32.     exit();
  33. }
  34. class XoopsTplfile extends XoopsObject
  35. {
  36.  
  37.     function XoopsTplfile()
  38.     {
  39.         $this->XoopsObject();
  40.         $this->initVar('tpl_id', XOBJ_DTYPE_INT, null, false);
  41.         $this->initVar('tpl_refid', XOBJ_DTYPE_INT, 0, false);
  42.         $this->initVar('tpl_tplset', XOBJ_DTYPE_OTHER, null, false);
  43.         $this->initVar('tpl_file', XOBJ_DTYPE_TXTBOX, null, true, 100);
  44.         $this->initVar('tpl_desc', XOBJ_DTYPE_TXTBOX, null, false, 100);
  45.         $this->initVar('tpl_lastmodified', XOBJ_DTYPE_INT, 0, false);
  46.         $this->initVar('tpl_lastimported', XOBJ_DTYPE_INT, 0, false);
  47.         $this->initVar('tpl_module', XOBJ_DTYPE_OTHER, null, false);
  48.         $this->initVar('tpl_type', XOBJ_DTYPE_OTHER, null, false);
  49.         $this->initVar('tpl_source', XOBJ_DTYPE_SOURCE, null, false);
  50.     }
  51.  
  52.     function getSource()
  53.     {
  54.         return $this->getVar('tpl_source');
  55.     }
  56.  
  57.     function getLastModified()
  58.     {
  59.         return $this->getVar('tpl_lastmodified');
  60.     }
  61. }
  62.  
  63. /**
  64. * XOOPS template file handler class.
  65. * This class is responsible for providing data access mechanisms to the data source
  66. * of XOOPS template file class objects.
  67. *
  68. *
  69. * @author  Kazumi Ono <onokazu@xoops.org>
  70. */
  71.  
  72. class XoopsTplfileHandler extends XoopsObjectHandler
  73. {
  74.  
  75.     function &create($isNew = true)
  76.     {
  77.         $tplfile = new XoopsTplfile();
  78.         if ($isNew) {
  79.             $tplfile->setNew();
  80.         }
  81.         return $tplfile;
  82.     }
  83.  
  84.     function &get($id, $getsource = false)
  85.     {
  86.         $tplfile = false;
  87.         $id = intval($id);
  88.         if ($id > 0) {
  89.             if (!$getsource) {
  90.                 $sql = 'SELECT * FROM '.$this->db->prefix('tplfile').' WHERE tpl_id='.$id;
  91.             } else {
  92.                 $sql = 'SELECT f.*, s.tpl_source FROM '.$this->db->prefix('tplfile').' f LEFT JOIN '.$this->db->prefix('tplsource').' s  ON s.tpl_id=f.tpl_id WHERE f.tpl_id='.$id;
  93.             }
  94.             if (!$result = $this->db->query($sql)) {
  95.                 return $tplfile;
  96.             }
  97.             $numrows = $this->db->getRowsNum($result);
  98.             if ($numrows == 1) {
  99.                 $tplfile = new XoopsTplfile();
  100.                 $tplfile->assignVars($this->db->fetchArray($result));
  101.             }
  102.         }
  103.         return $tplfile;
  104.     }
  105.  
  106.     function loadSource(&$tplfile)
  107.     {
  108.         /**
  109.         * @TODO: Change to if (!(class_exists($this->className) && $obj instanceof $this->className)) when going fully PHP5
  110.         */
  111.         if (!is_a($tplfile, 'xoopstplfile')) {
  112.             return false;
  113.         }
  114.  
  115.         if (!$tplfile->getVar('tpl_source')) {
  116.             $sql = 'SELECT tpl_source FROM '.$this->db->prefix('tplsource').' WHERE tpl_id='.$tplfile->getVar('tpl_id');
  117.             if (!$result = $this->db->query($sql)) {
  118.                 return false;
  119.             }
  120.             $myrow = $this->db->fetchArray($result);
  121.             $tplfile->assignVar('tpl_source', $myrow['tpl_source']);
  122.         }
  123.         return true;
  124.     }
  125.  
  126.     function insert(&$tplfile)
  127.     {
  128.         /**
  129.         * @TODO: Change to if (!(class_exists($this->className) && $obj instanceof $this->className)) when going fully PHP5
  130.         */
  131.         if (!is_a($tplfile, 'xoopstplfile')) {
  132.             return false;
  133.         }
  134.         if (!$tplfile->isDirty()) {
  135.             return true;
  136.         }
  137.         if (!$tplfile->cleanVars()) {
  138.             return false;
  139.         }
  140.         foreach ($tplfile->cleanVars as $k => $v) {
  141.             ${$k} = $v;
  142.         }
  143.         if ($tplfile->isNew()) {
  144.             $tpl_id = $this->db->genId('tpltpl_file_id_seq');
  145.             $sql = sprintf("INSERT INTO %s (tpl_id, tpl_module, tpl_refid, tpl_tplset, tpl_file, tpl_desc, tpl_lastmodified, tpl_lastimported, tpl_type) VALUES (%u, %s, %u, %s, %s, %s, %u, %u, %s)", $this->db->prefix('tplfile'), $tpl_id, $this->db->quoteString($tpl_module), $tpl_refid, $this->db->quoteString($tpl_tplset), $this->db->quoteString($tpl_file), $this->db->quoteString($tpl_desc), $tpl_lastmodified, $tpl_lastimported, $this->db->quoteString($tpl_type));
  146.             if (!$result = $this->db->query($sql)) {
  147.                 return false;
  148.             }
  149.             if (empty($tpl_id)) {
  150.                 $tpl_id = $this->db->getInsertId();
  151.             }
  152.             if (isset($tpl_source) && $tpl_source != '') {
  153.                 $sql = sprintf("INSERT INTO %s (tpl_id, tpl_source) VALUES (%u, %s)", $this->db->prefix('tplsource'), $tpl_id, $this->db->quoteString($tpl_source));
  154.                 if (!$result = $this->db->query($sql)) {
  155.                     $this->db->query(sprintf("DELETE FROM %s WHERE tpl_id = %u", $this->db->prefix('tplfile'), $tpl_id));
  156.                     return false;
  157.                 }
  158.             }
  159.             $tplfile->assignVar('tpl_id', $tpl_id);
  160.         } else {
  161.             $sql = sprintf("UPDATE %s SET tpl_tplset = %s, tpl_file = %s, tpl_desc = %s, tpl_lastimported = %u, tpl_lastmodified = %u WHERE tpl_id = %u", $this->db->prefix('tplfile'), $this->db->quoteString($tpl_tplset), $this->db->quoteString($tpl_file), $this->db->quoteString($tpl_desc), $tpl_lastimported, $tpl_lastmodified, $tpl_id);
  162.             if (!$result = $this->db->query($sql)) {
  163.                 return false;
  164.             }
  165.             if (isset($tpl_source) && $tpl_source != '') {
  166.                 $sql = sprintf("UPDATE %s SET tpl_source = %s WHERE tpl_id = %u", $this->db->prefix('tplsource'), $this->db->quoteString($tpl_source), $tpl_id);
  167.                 if (!$result = $this->db->query($sql)) {
  168.                     return false;
  169.                 }
  170.             }
  171.         }
  172.         return true;
  173.     }
  174.  
  175.     function forceUpdate(&$tplfile)
  176.     {
  177.         /**
  178.         * @TODO: Change to if (!(class_exists($this->className) && $obj instanceof $this->className)) when going fully PHP5
  179.         */
  180.         if (!is_a($tplfile, 'xoopstplfile')) {
  181.             return false;
  182.         }
  183.         if (!$tplfile->isDirty()) {
  184.             return true;
  185.         }
  186.         if (!$tplfile->cleanVars()) {
  187.             return false;
  188.         }
  189.         foreach ($tplfile->cleanVars as $k => $v) {
  190.             ${$k} = $v;
  191.         }
  192.         if (!$tplfile->isNew()) {
  193.             $sql = sprintf("UPDATE %s SET tpl_tplset = %s, tpl_file = %s, tpl_desc = %s, tpl_lastimported = %u, tpl_lastmodified = %u WHERE tpl_id = %u", $this->db->prefix('tplfile'), $this->db->quoteString($tpl_tplset), $this->db->quoteString($tpl_file), $this->db->quoteString($tpl_desc), $tpl_lastimported, $tpl_lastmodified, $tpl_id);
  194.             if (!$result = $this->db->queryF($sql)) {
  195.                 return false;
  196.             }
  197.             if (isset($tpl_source) && $tpl_source != '') {
  198.                 $sql = sprintf("UPDATE %s SET tpl_source = %s WHERE tpl_id = %u", $this->db->prefix('tplsource'), $this->db->quoteString($tpl_source), $tpl_id);
  199.                 if (!$result = $this->db->queryF($sql)) {
  200.                     return false;
  201.                 }
  202.             }
  203.             return true;
  204.         } else {
  205.             return false;
  206.         }
  207.     }
  208.  
  209.     function delete(&$tplfile)
  210.     {
  211.         /**
  212.         * @TODO: Change to if (!(class_exists($this->className) && $obj instanceof $this->className)) when going fully PHP5
  213.         */
  214.         if (!is_a($tplfile, 'xoopstplfile')) {
  215.             return false;
  216.         }
  217.         $id = $tplfile->getVar('tpl_id');
  218.         $sql = sprintf("DELETE FROM %s WHERE tpl_id = %u", $this->db->prefix('tplfile'), $id);
  219.         if (!$result = $this->db->query($sql)) {
  220.             return false;
  221.         }
  222.         $sql = sprintf("DELETE FROM %s WHERE tpl_id = %u", $this->db->prefix('tplsource'), $id);
  223.         $this->db->query($sql);
  224.         return true;
  225.     }
  226.  
  227.     function getObjects($criteria = null, $getsource = false, $id_as_key = false)
  228.     {
  229.         $ret = array();
  230.         $limit = $start = 0;
  231.         if ($getsource) {
  232.             $sql = 'SELECT f.*, s.tpl_source FROM '.$this->db->prefix('tplfile').' f LEFT JOIN '.$this->db->prefix('tplsource').' s ON s.tpl_id=f.tpl_id';
  233.         } else {
  234.             $sql = 'SELECT * FROM '.$this->db->prefix('tplfile');
  235.         }
  236.         if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
  237.             $sql .= ' '.$criteria->renderWhere().' ORDER BY tpl_refid';
  238.             $limit = $criteria->getLimit();
  239.             $start = $criteria->getStart();
  240.         }
  241.         $result = $this->db->query($sql, $limit, $start);
  242.         if (!$result) {
  243.             return $ret;
  244.         }
  245.         while ($myrow = $this->db->fetchArray($result)) {
  246.             $tplfile = new XoopsTplfile();
  247.             $tplfile->assignVars($myrow);
  248.             if (!$id_as_key) {
  249.                 $ret[] =& $tplfile;
  250.             } else {
  251.                 $ret[$myrow['tpl_id']] =& $tplfile;
  252.             }
  253.             unset($tplfile);
  254.         }
  255.         return $ret;
  256.     }
  257.  
  258.     function getCount($criteria = null)
  259.     {
  260.         $sql = 'SELECT COUNT(*) FROM '.$this->db->prefix('tplfile');
  261.         if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
  262.             $sql .= ' '.$criteria->renderWhere();
  263.         }
  264.         if (!$result =& $this->db->query($sql)) {
  265.             return 0;
  266.         }
  267.         list($count) = $this->db->fetchRow($result);
  268.         return $count;
  269.     }
  270.  
  271.     function getModuleTplCount($tplset)
  272.     {
  273.         $ret = array();
  274.         $sql = "SELECT tpl_module, COUNT(tpl_id) AS count FROM ".$this->db->prefix('tplfile')." WHERE tpl_tplset='".$tplset."' GROUP BY tpl_module";
  275.         $result = $this->db->query($sql);
  276.         if (!$result) {
  277.             return $ret;
  278.         }
  279.         while ($myrow = $this->db->fetchArray($result)) {
  280.             if ($myrow['tpl_module'] != '') {
  281.                 $ret[$myrow['tpl_module']] = $myrow['count'];
  282.             }
  283.         }
  284.         return $ret;
  285.     }
  286.  
  287.     function find($tplset = null, $type = null, $refid = null, $module = null, $file = null, $getsource = false)
  288.     {
  289.         $criteria = new CriteriaCompo();
  290.         if (isset($tplset)) {
  291.             $criteria->add(new Criteria('tpl_tplset', $tplset));
  292.         }
  293.         if (isset($module)) {
  294.             $criteria->add(new Criteria('tpl_module', $module));
  295.         }
  296.         if (isset($refid)) {
  297.             $criteria->add(new Criteria('tpl_refid', $refid));
  298.         }
  299.         if (isset($file)) {
  300.             $criteria->add(new Criteria('tpl_file', $file));
  301.         }
  302.         if (isset($type)) {
  303.             if (is_array($type)) {
  304.                 $criteria2 = new CriteriaCompo();
  305.                 foreach ($type as $t) {
  306.                     $criteria2->add(new Criteria('tpl_type', $t), 'OR');
  307.                 }
  308.                 $criteria->add($criteria2);
  309.             } else {
  310.                 $criteria->add(new Criteria('tpl_type', $type));
  311.             }
  312.         }
  313.         return $this->getObjects($criteria, $getsource, false);
  314.     }
  315.  
  316.     function templateExists($tplname, $tplset_name)
  317.     {
  318.         $criteria = new CriteriaCompo(new Criteria('tpl_file', trim($tplname)));
  319.         $criteria->add(new Criteria('tpl_tplset', trim($tplset_name)));
  320.         if ($this->getCount($criteria) > 0) {
  321.             return true;
  322.         }
  323.         return false;
  324.     }
  325. }
  326. ?>