source: temp/test-xoops.ec-cube.net/html/kernel/block.php @ 405

Revision 405, 13.2 KB checked in by root, 20 years ago (diff)
Line 
1<?php
2// $Id: block.php,v 1.4 2005/08/03 12:39:12 onokazu Exp $
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
32if (!defined('XOOPS_ROOT_PATH')) {
33    exit();
34}
35
36/**
37 * @author  Kazumi Ono <[email protected]>
38 * @copyright copyright (c) 2000 XOOPS.org
39 **/
40
41/**
42 * A block
43 *
44 * @author Kazumi Ono <[email protected]>
45 * @copyright copyright (c) 2000 XOOPS.org
46 *
47 * @package kernel
48 **/
49class XoopsBlock extends XoopsObject
50{
51
52    /**
53     * constructor
54     *
55     * @param mixed $id
56     **/
57    function XoopsBlock($id = null)
58    {
59        $this->initVar('bid', XOBJ_DTYPE_INT, null, false);
60        $this->initVar('mid', XOBJ_DTYPE_INT, 0, false);
61        $this->initVar('func_num', XOBJ_DTYPE_INT, 0, false);
62        $this->initVar('options', XOBJ_DTYPE_TXTBOX, null, false, 255);
63        $this->initVar('name', XOBJ_DTYPE_TXTBOX, null, true, 150);
64        //$this->initVar('position', XOBJ_DTYPE_INT, 0, false);
65        $this->initVar('title', XOBJ_DTYPE_TXTBOX, null, false, 150);
66        $this->initVar('content', XOBJ_DTYPE_TXTAREA, null, false);
67        $this->initVar('side', XOBJ_DTYPE_INT, 0, false);
68        $this->initVar('weight', XOBJ_DTYPE_INT, 0, false);
69        $this->initVar('visible', XOBJ_DTYPE_INT, 0, false);
70        $this->initVar('block_type', XOBJ_DTYPE_OTHER, null, false);
71        $this->initVar('c_type', XOBJ_DTYPE_OTHER, null, false);
72        $this->initVar('isactive', XOBJ_DTYPE_INT, null, false);
73        $this->initVar('dirname', XOBJ_DTYPE_TXTBOX, null, false, 50);
74        $this->initVar('func_file', XOBJ_DTYPE_TXTBOX, null, false, 50);
75        $this->initVar('show_func', XOBJ_DTYPE_TXTBOX, null, false, 50);
76        $this->initVar('edit_func', XOBJ_DTYPE_TXTBOX, null, false, 50);
77        $this->initVar('template', XOBJ_DTYPE_OTHER, null, false);
78        $this->initVar('bcachetime', XOBJ_DTYPE_INT, 0, false);
79        $this->initVar('last_modified', XOBJ_DTYPE_INT, 0, false);
80
81        // for backward compatibility
82        if (isset($id)) {
83            if (is_array($id)) {
84                $this->assignVars($id);
85            } else {
86                $blkhandler =& xoops_gethandler('block');
87                $obj =& $blkhandler->get($id);
88                $this->assignVars($obj->getVars());
89            }
90        }
91    }
92
93    /**
94     * return the content of the block for output
95     *
96     * @param string $format
97     * @param string $c_type type of content<br>
98     * Legal value for the type of content<br>
99     * <ul><li>H : custom HTML block
100     * <li>P : custom PHP block
101     * <li>S : use text sanitizater (smilies enabled)
102     * <li>T : use text sanitizater (smilies disabled)</ul>
103     * @return string content for output
104     **/
105    function &getContent($format = 'S', $c_type = 'T')
106    {
107        switch ( $format ) {
108        case 'S':
109            if ( $c_type == 'H' ) {
110                return str_replace('{X_SITEURL}', XOOPS_URL.'/', $this->getVar('content', 'N'));
111            } elseif ( $c_type == 'P' ) {
112                ob_start();
113                echo eval($this->getVar('content', 'N'));
114                $content = ob_get_contents();
115                ob_end_clean();
116                return str_replace('{X_SITEURL}', XOOPS_URL.'/', $content);
117            } elseif ( $c_type == 'S' ) {
118                $myts =& MyTextSanitizer::getInstance();
119                return str_replace('{X_SITEURL}', XOOPS_URL.'/', $myts->displayTarea($this->getVar('content', 'N'), 0, 1));
120            } else {
121                $myts =& MyTextSanitizer::getInstance();
122                return str_replace('{X_SITEURL}', XOOPS_URL.'/', $myts->displayTarea($this->getVar('content', 'N'), 0, 0));
123            }
124            break;
125        case 'E':
126            return $this->getVar('content', 'E');
127            break;
128        default:
129            return $this->getVar('content', 'N');
130            break;
131        }
132    }
133
134    /**
135     * (HTML-) form for setting the options of the block
136     *
137     * @return string HTML for the form, FALSE if not defined for this block
138     **/
139    function getOptions()
140    {
141        if ($this->getVar('block_type') != 'C') {
142            $edit_func = $this->getVar('edit_func');
143            if (!$edit_func) {
144                return false;
145            }
146            if (file_exists(XOOPS_ROOT_PATH.'/modules/'.$this->getVar('dirname').'/blocks/'.$this->getVar('func_file'))) {
147                if (file_exists(XOOPS_ROOT_PATH.'/modules/'.$this->getVar('dirname').'/language/'.$GLOBALS['xoopsConfig']['language'].'/blocks.php')) {
148                    include_once XOOPS_ROOT_PATH.'/modules/'.$this->getVar('dirname').'/language/'.$GLOBALS['xoopsConfig']['language'].'/blocks.php';
149                } elseif (file_exists(XOOPS_ROOT_PATH.'/modules/'.$this->getVar('dirname').'/language/english/blocks.php')) {
150                    include_once XOOPS_ROOT_PATH.'/modules/'.$this->getVar('dirname').'/language/english/blocks.php';
151                }
152                include_once XOOPS_ROOT_PATH.'/modules/'.$this->getVar('dirname').'/blocks/'.$this->getVar('func_file');
153                $options = explode('|', $this->getVar('options'));
154                $edit_form = $edit_func($options);
155                if (!$edit_form) {
156                    return false;
157                }
158                return $edit_form;
159            } else {
160                return false;
161            }
162        } else {
163            return false;
164        }
165    }
166}
167
168
169/**
170 * XOOPS block handler class. (Singelton)
171 *
172 * This class is responsible for providing data access mechanisms to the data source
173 * of XOOPS block class objects.
174 *
175 * @author  Kazumi Ono <[email protected]>
176 * @copyright copyright (c) 2000 XOOPS.org
177 * @package kernel
178 * @subpackage block
179*/
180class XoopsBlockHandler extends XoopsObjectHandler
181{
182
183    /**
184     * create a new block
185     *
186     * @see XoopsBlock
187     * @param bool $isNew is the new block new??
188     * @return object XoopsBlock reference to the new block
189     **/
190    function &create($isNew = true)
191    {
192        $block = new XoopsBlock();
193        if ($isNew) {
194            $block->setNew();
195        }
196        return $block;
197    }
198
199    /**
200     * retrieve a specific {@link XoopsBlock}
201     *
202     * @see XoopsBlock
203     * @param int $id bid of the block to retrieve
204     * @return object XoopsBlock reference to the block
205     **/
206    function &get($id)
207    {
208        $id = intval($id);
209        if ($id > 0) {
210            $sql = 'SELECT * FROM '.$this->db->prefix('newblocks').' WHERE bid='.$id;
211            if (!$result = $this->db->query($sql)) {
212                return false;
213            }
214            $numrows = $this->db->getRowsNum($result);
215            if ($numrows == 1) {
216                $block = new XoopsBlock();
217                $block->assignVars($this->db->fetchArray($result));
218                return $block;
219            }
220        }
221        return false;
222    }
223
224    /**
225     * write a new block into the database
226     *
227     * @param object XoopsBlock $block reference to the block to insert
228     * @return bool TRUE if succesful
229     **/
230    function insert(&$block)
231    {
232        if (strtolower(get_class($block)) != 'xoopsblock') {
233            return false;
234        }
235        if (!$block->isDirty()) {
236            return true;
237        }
238        if (!$block->cleanVars()) {
239            return false;
240        }
241        foreach ($block->cleanVars as $k => $v) {
242            ${$k} = $v;
243        }
244        if ($block->isNew()) {
245            $bid = $this->db->genId('newblocks_bid_seq');
246            $sql = sprintf("INSERT INTO %s (bid, mid, func_num, options, name, title, content, side, weight, visible, block_type, c_type, isactive, dirname, func_file, show_func, edit_func, template, bcachetime, last_modified) VALUES (%u, %u, %u, '%s', '%s', '%s', '%s', %u, %u, %u, '%s', '%s', %u, '%s', '%s', '%s', '%s', '%s', %u, %u)", $this->db->prefix('newblocks'), $bid, $mid, $func_num, $options, $name, $title, $content, $side, $weight, $visible, $block_type, $c_type, 1, $dirname, $func_file, $show_func, $edit_func, $template, $bcachetime, time());
247        } else {
248            $sql = sprintf("UPDATE %s SET func_num = %u, options = '%s', name = '%s', title = '%s', content = '%s', side = %u, weight = %u, visible = %u, c_type = '%s', isactive = %u, func_file = '%s', show_func = '%s', edit_func = '%s', template = '%s', bcachetime = %u, last_modified = %u WHERE bid = %u", $this->db->prefix('newblocks'), $func_num, $options, $name, $title, $content, $side, $weight, $visible, $c_type, $isactive, $func_file, $show_func, $edit_func, $template, $bcachetime, time(), $bid);
249        }
250        if (!$result = $this->db->query($sql)) {
251            return false;
252        }
253        if (empty($bid)) {
254            $bid = $this->db->getInsertId();
255        }
256        $block->assignVar('bid', $bid);
257        return true;
258    }
259
260    /**
261     * delete a block from the database
262     *
263     * @param object XoopsBlock $block reference to the block to delete
264     * @return bool TRUE if succesful
265     **/
266    function delete(&$block)
267    {
268        if (strtolower(get_class($block)) != 'xoopsblock') {
269            return false;
270        }
271        $id = $block->getVar('bid');
272        $sql = sprintf("DELETE FROM %s WHERE bid = %u", $this->db->prefix('newblocks'), $id);
273        if (!$result = $this->db->query($sql)) {
274            return false;
275        }
276        $sql = sprintf("DELETE FROM %s WHERE block_id = %u", $this->db->prefix('block_module_link'), $id);
277        $this->db->query($sql);
278        return true;
279    }
280
281    /**
282     * retrieve array of {@link XoopsBlock}s meeting certain conditions
283     * @param object $criteria {@link CriteriaElement} with conditions for the blocks
284     * @param bool $id_as_key should the blocks' bid be the key for the returned array?
285     * @return array {@link XoopsBlock}s matching the conditions
286     **/
287    function &getObjects($criteria = null, $id_as_key = false)
288    {
289        $ret = array();
290        $limit = $start = 0;
291        $sql = 'SELECT DISTINCT(b.*) FROM '.$this->db->prefix('newblocks').' b LEFT JOIN '.$this->db->prefix('block_module_link').' l ON b.bid=l.block_id';
292        if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
293            $sql .= ' '.$criteria->renderWhere();
294            $limit = $criteria->getLimit();
295            $start = $criteria->getStart();
296        }
297        $result = $this->db->query($sql, $limit, $start);
298        if (!$result) {
299            return $ret;
300        }
301        while ($myrow = $this->db->fetchArray($result)) {
302            $block = new XoopsBlock();
303            $block->assignVars($myrow);
304            if (!$id_as_key) {
305                $ret[] =& $block;
306            } else {
307                $ret[$myrow['bid']] =& $block;
308            }
309            unset($block);
310        }
311        return $ret;
312    }
313
314    /**
315     * get a list of blocks matchich certain conditions
316     *
317     * @param string $criteria conditions to match
318     * @return array array of blocks matching the conditions
319     **/
320    function &getList($criteria = null)
321    {
322        $blocks =& $this->getObjects($criteria, true);
323        $ret = array();
324        foreach (array_keys($blocks) as $i) {
325            $name = ($blocks[$i]->getVar('block_type') != 'C') ? $blocks[$i]->getVar('name') : $blocks[$i]->getVar('title');
326            $ret[$i] = $name;
327        }
328        return $ret;
329    }
330}
331?>
Note: See TracBrowser for help on using the repository browser.