[ Index ] |
|
Code source de b2evolution 2.1.0-beta |
1 <?php 2 /** 3 * This file implements the Chapter class. 4 * 5 * This file is part of the evoCore framework - {@link http://evocore.net/} 6 * See also {@link http://sourceforge.net/projects/evocms/}. 7 * 8 * @copyright (c)2003-2007 by Francois PLANQUE - {@link http://fplanque.net/} 9 * Parts of this file are copyright (c)2005-2006 by PROGIDISTRI - {@link http://progidistri.com/}. 10 * 11 * {@internal License choice 12 * - If you have received this file as part of a package, please find the license.txt file in 13 * the same folder or the closest folder above for complete license terms. 14 * - If you have received this file individually (e-g: from http://evocms.cvs.sourceforge.net/) 15 * then you must choose one of the following licenses before using the file: 16 * - GNU General Public License 2 (GPL) - http://www.opensource.org/licenses/gpl-license.php 17 * - Mozilla Public License 1.1 (MPL) - http://www.opensource.org/licenses/mozilla1.1.php 18 * }} 19 * 20 * @package evocore 21 * 22 * {@internal Below is a list of authors who have contributed to design/coding of this file: }} 23 * @author fplanque: Francois PLANQUE. 24 * 25 * @version $Id: _chapter.class.php,v 1.5 2007/10/06 21:17:25 fplanque Exp $ 26 */ 27 if( !defined('EVO_MAIN_INIT') ) die( 'Please, do not access this page directly.' ); 28 29 load_class('generic/model/_genericcategory.class.php'); 30 31 32 /** 33 * Chapter Class 34 * 35 * @package evocore 36 */ 37 class Chapter extends GenericCategory 38 { 39 /** 40 * @var integer 41 */ 42 var $blog_ID; 43 /** 44 * The Blog of the Item (lazy filled, use {@link get_Blog()} to access it. 45 * @access protected 46 * @var Blog 47 */ 48 var $Blog; 49 50 var $urlname; 51 52 /** 53 * Lazy filled 54 * @var Chapter 55 */ 56 var $parent_Chapter; 57 58 /** 59 * Constructor 60 * 61 * @param table Database row 62 * @param integer|NULL subset to use for new object 63 */ 64 function Chapter( $db_row = NULL, $subset_ID = NULL ) 65 { 66 // Call parent constructor: 67 parent::GenericCategory( 'T_categories', 'cat_', 'cat_ID', $db_row ); 68 69 if( is_null($db_row) ) 70 { // We are creating an object here: 71 $this->set( 'blog_ID', $subset_ID ); 72 } 73 else 74 { // Wa are loading an object: 75 $this->blog_ID = $db_row->cat_blog_ID; 76 $this->urlname = $db_row->cat_urlname; 77 } 78 } 79 80 81 /** 82 * Load data from Request form fields. 83 * 84 * @return boolean true if loaded data seems valid. 85 */ 86 function load_from_request() 87 { 88 global $DB; 89 90 parent::load_from_Request(); 91 92 // Check url name 93 if( param_string_not_empty( 'cat_urlname', T_('Please enter an urlname.') ) ) 94 { 95 $this->set_from_Request( 'urlname' ); 96 if( ! preg_match( '|^[A-Za-z0-9\-]+$|', $this->urlname ) 97 || preg_match( '|^[A-Za-z][0-9]*$|', $this->urlname ) ) // This is to prevent conflict with things such as week number 98 { 99 param_error( 'cat_urlname', T_('The url name is invalid.') ); 100 } 101 else 102 { 103 if( Chapter::urlname_exists( $this->urlname, $this->ID ) ) 104 { // urlname is already in use 105 param_error( 'cat_urlname', T_('This URL name is already in use by another category. Please choose another name.') ); 106 } 107 } 108 } 109 110 return ! param_errors_detected(); 111 } 112 113 114 /** 115 * @static 116 */ 117 function urlname_exists( $urlname, $ignore_ID = 0 ) 118 { 119 global $DB; 120 121 return $DB->get_var( 'SELECT COUNT(*) 122 FROM T_categories 123 WHERE cat_urlname = '.$DB->quote($urlname).' 124 AND cat_ID <> '.$ignore_ID 125 ); 126 } 127 128 129 function & get_parent_Chapter() 130 { 131 if( ! isset( $this->parent_Chapter ) ) 132 { // Not resoleved yet! 133 if( empty( $this->parent_ID ) ) 134 { 135 $this->parent_Chapter = NULL; 136 } 137 else 138 { 139 $ChapterCache = & get_Cache( 'ChapterCache' ); 140 $this->parent_Chapter = & $ChapterCache->get_by_ID( $this->parent_ID ); 141 } 142 } 143 144 return $this->parent_Chapter; 145 } 146 147 148 /** 149 * Get URL path (made of URL names) back to the root 150 */ 151 function get_url_path() 152 { 153 $r = $this->urlname.'/'; 154 155 $parent_Chapter = & $this->get_parent_Chapter(); 156 if( !is_null( $parent_Chapter ) ) 157 { // Recurse: 158 $r = $parent_Chapter->get_url_path().$r; 159 } 160 161 return $r; 162 } 163 164 165 /** 166 * Generate the URL to access the category. 167 * 168 * @param string|NULL 'param_num', 'subchap', 'chapters' 169 * @param string|NULL url to use 170 * @param string glue between url params 171 */ 172 function get_permanent_url( $link_type = NULL, $blogurl = NULL, $glue = '&' ) 173 { 174 global $DB, $cacheweekly, $Settings; 175 176 if( empty( $link_type ) ) 177 { // Use default from settings: 178 $this->get_Blog(); 179 $link_type = $this->Blog->get_setting( 'chapter_links' ); 180 } 181 182 if( empty( $blogurl ) ) 183 { 184 $this->get_Blog(); 185 $blogurl = $this->Blog->gen_blogurl(); 186 } 187 188 switch( $link_type ) 189 { 190 case 'param_num': 191 return url_add_param( $blogurl, 'cat='.$this->ID, $glue ); 192 /* break; */ 193 194 case 'subchap': 195 $this->get_Blog(); 196 $category_prefix = $this->Blog->get_setting('category_prefix'); 197 if( !empty( $category_prefix ) ) 198 { 199 return url_add_tail( $blogurl, '/' . $category_prefix . '/' . $this->urlname.'/' ); 200 } 201 else 202 { 203 return url_add_tail( $blogurl, '/'.$this->urlname.'/' ); 204 } 205 /* break; */ 206 207 case 'chapters': 208 default: 209 $this->get_Blog(); 210 $category_prefix = $this->Blog->get_setting('category_prefix'); 211 if( !empty( $category_prefix ) ) 212 { 213 return url_add_tail( $blogurl, '/'. $category_prefix . '/' . $this->get_url_path() ); 214 } 215 else 216 { 217 return url_add_tail( $blogurl, '/'.$this->get_url_path() ); 218 } 219 /* break; */ 220 } 221 } 222 223 224 /** 225 * Get the Blog object for the Chapter. 226 * 227 * @return Blog 228 */ 229 function & get_Blog() 230 { 231 if( is_null($this->Blog) ) 232 { 233 $this->load_Blog(); 234 } 235 236 return $this->Blog; 237 } 238 239 240 /** 241 * Load the Blog object for the Chapter, without returning it. 242 */ 243 function load_Blog() 244 { 245 if( is_null($this->Blog) ) 246 { 247 $BlogCache = & get_Cache( 'BlogCache' ); 248 $this->Blog = & $BlogCache->get_by_ID( $this->blog_ID ); 249 } 250 } 251 252 253 /** 254 * Insert object into DB based on previously recorded changes. 255 * 256 * @return boolean true on success 257 */ 258 function dbinsert() 259 { 260 global $DB; 261 262 if( $this->ID != 0 ) die( 'Existing object cannot be inserted!' ); 263 264 $DB->begin(); 265 266 if( Chapter::urlname_exists( $this->urlname ) ) 267 { // We gotta find another name: 268 $sql = 'SELECT cat_urlname 269 FROM T_categories 270 WHERE cat_urlname REGEXP "^'.$this->urlname.'(-[0-9]+)?$"'; 271 $highest_number = 0; 272 foreach( $DB->get_results( $sql ) as $row ) 273 { 274 if( preg_match( '/-([0-9]+)$/', $row->cat_urlname, $matches ) ) 275 { // This one has a number, we extract it: 276 $existing_number = (integer) $matches[1]; 277 if( $existing_number > $highest_number ) 278 { // This is the new high 279 $highest_number = $existing_number; 280 } 281 } 282 } 283 $this->set( 'urlname', $this->urlname.'-'.($highest_number+1) ); 284 } 285 286 $r = parent::dbinsert(); 287 288 $DB->commit(); 289 290 return $r; 291 } 292 } 293 294 295 /* 296 * $Log: _chapter.class.php,v $ 297 * Revision 1.5 2007/10/06 21:17:25 fplanque 298 * cleanup 299 * 300 * Revision 1.4 2007/10/01 13:41:06 waltercruz 301 * Category prefix, trying to make the code more b2evo style 302 * 303 * Revision 1.3 2007/09/29 01:50:49 fplanque 304 * temporary rollback; waiting for new version 305 * 306 * Revision 1.1 2007/06/25 10:59:25 fplanque 307 * MODULES (refactored MVC) 308 * 309 * Revision 1.10 2007/05/09 00:54:45 fplanque 310 * Attempt to normalize all URLs before adding params 311 * 312 * Revision 1.9 2007/04/26 00:11:06 fplanque 313 * (c) 2007 314 * 315 * Revision 1.8 2007/03/02 00:44:43 fplanque 316 * various small fixes 317 * 318 * Revision 1.7 2007/01/15 00:38:06 fplanque 319 * pepped up "new blog" creation a little. To be continued. 320 * 321 * Revision 1.6 2006/12/11 00:32:26 fplanque 322 * allow_moving_chapters stting moved to UI 323 * chapters are now called categories in the UI 324 * 325 * Revision 1.5 2006/11/24 18:27:23 blueyed 326 * Fixed link to b2evo CVS browsing interface in file docblocks 327 */ 328 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Thu Nov 29 23:58:50 2007 | par Balluche grâce à PHPXref 0.7 |
![]() |