| [ Index ] |
|
Code source de LifeType 1.2.4 |
1 <?php 2 3 4 lt_include( PLOG_CLASS_PATH."class/config/config.class.php" ); 5 lt_include( PLOG_CLASS_PATH."class/misc/glob.class.php" ); 6 lt_include( PLOG_CLASS_PATH."class/file/file.class.php" ); 7 lt_include( PLOG_CLASS_PATH."class/template/templatesets/templatesets.class.php" ); 8 9 define( "BLOG_BASE_TEMPLATE_FOLDER", "blog_" ); 10 11 /** 12 * \ingroup Template 13 * 14 * deals with template sets files in disk. The relation between this class, 15 * TemplateService and TemplateSets is that TemplateService takes care of generating 16 * the right object (CachedTemplate, PluginTemplate, Template) when needed for rendering 17 * template pages. TemplateSets deals with the logical information (does it have an 18 * screenshot? Is it a local template?) and TemplateStorage deals with adding 19 * templates, removing them from disk, etc. 20 * 21 * Class and API users should not need to use this class directly. 22 */ 23 class TemplateSetStorage 24 { 25 26 /** 27 * Constructor 28 */ 29 function TemplateSetStorage() 30 { 31 32 } 33 34 /** 35 * Returns the path to where the given template should be. If blogId == 0, 36 * then the template will be considered global or else it will be considered 37 * as a custom template for the given blog. 38 * 39 * @return Returns a string with the path to the folder. 40 */ 41 function getTemplateFolder( $templateName, $blogId = 0 ) 42 { 43 $config =& Config::getConfig(); 44 $baseFolder = $config->getValue( "template_folder" ); 45 if( $baseFolder[strlen($baseFolder)-1] != "/" ) 46 $baseFolder = $baseFolder."/"; 47 48 if( $blogId == 0 ) { 49 $templateFolder = $baseFolder.$templateName."/"; 50 } 51 else { 52 $templateFolder = $baseFolder.BLOG_BASE_TEMPLATE_FOLDER."$blogId/".$templateName."/"; 53 } 54 55 return $templateFolder; 56 } 57 58 /** 59 * returns the base folder where templates are stored (templates/) by default 60 * 61 * @return The folder where templates are stored, according to our configuration 62 * @static 63 */ 64 function getBaseTemplateFolder() 65 { 66 $config =& Config::getConfig(); 67 68 $basePath = $config->getValue( "template_folder" ); 69 70 return $basePath; 71 } 72 73 /** 74 * returns the path where the given blog is storing its local templates 75 * 76 * @param blogId the id of the blog 77 * @return The template in disk where templates are being stored 78 * @static 79 */ 80 function getBlogBaseTemplateFolder( $blogId ) 81 { 82 $config =& Config::getConfig(); 83 $baseFolder = $config->getValue( "template_folder" ); 84 if( $baseFolder[strlen($baseFolder)-1] != "/" ) 85 $baseFolder = $baseFolder."/"; 86 87 $templateFolder = $baseFolder.BLOG_BASE_TEMPLATE_FOLDER."$blogId/"; 88 89 return $templateFolder; 90 } 91 92 /** 93 * Recursively removes a folder from disk. 94 * 95 * @return True if successful or false otherwise. 96 */ 97 function _removeFolder( $folderName ) 98 { 99 // if the folder does not even exist, let's not even bother trying... It 100 // could be that it was manually removed by the user or something! 101 if( File::exists( $folderName )) 102 $result = File::deleteDir( $folderName, true ); 103 else 104 $result = true; 105 106 return( $result ); 107 } 108 109 /** 110 * @private 111 */ 112 function _removeTemplateFromArray( $array, $key ) 113 { 114 $resultArray = Array(); 115 foreach( $array as $elem ) { 116 if( $elem != $key ) 117 array_push( $resultArray, $elem ); 118 } 119 120 return $resultArray; 121 } 122 123 /** 124 * Removes a global template 125 * 126 * @param templateName The name of the template that we'd like to remove 127 */ 128 function removeGlobalTemplate( $templateName ) 129 { 130 $config =& Config::getConfig(); 131 132 // check if the template really exists 133 $availableTemplates = $config->getValue( "templates" ); 134 if( !in_array( $templateName, $availableTemplates )) 135 return false; 136 137 // remove the folder from disk 138 $templateFolder = $this->getTemplateFolder( $templateName ); 139 $result = $this->_removeFolder( $templateFolder ); 140 if( !$result ) 141 return false; 142 143 // remove the entry for that template from the configuration 144 $newTemplateList = $this->_removeTemplateFromArray( $availableTemplates, $templateName ); 145 146 // update the list of templates 147 $config->saveValue( "templates", $newTemplateList ); 148 149 return true; 150 } 151 152 /** 153 * Removes a blog specific template. 154 * 155 * @param templateName The name of the blog-specific template that we'd like to remove 156 * @param blogId The id of the blog whose blog we'd like to remove 157 * @return true if sucessful or false otherwise 158 */ 159 function removeBlogTemplate( $templateName, $blogId ) 160 { 161 $config =& Config::getConfig(); 162 163 // get the settings of this blog 164 $blogs = new Blogs(); 165 $blog = $blogs->getBlogInfo( $blogId ); 166 $blogSettings = $blog->getSettings(); 167 168 // looks like the blog doesn't exist, so let's not bother... 169 if( empty($blogSettings) ) 170 return false; 171 172 // check if the template really exists 173 $blogTemplates = $blogSettings->getValue( "blog_templates" ); 174 if( !in_array($templateName, $blogTemplates )) 175 return false; 176 177 // remove the folder where the template is 178 $templateFolder = $this->getTemplateFolder( $templateName, $blogId ); 179 $result = $this->_removeFolder( $templateFolder ); 180 if( !$result ) 181 return false; 182 183 // remove the entry for that template from the configuration 184 $newTemplateList = $this->_removeTemplateFromArray( $blogTemplates, $templateName ); 185 186 // finally, save the settings 187 $blogSettings->setValue( "blog_templates", $newTemplateList ); 188 $blog->setSettings( $blogSettings ); 189 $blogs->updateBlog( $blog ); 190 191 return true; 192 } 193 194 /** 195 * Removes a template from the templates folder. If blogId == 0, then we will 196 * look for it as a global template, or else we will look for the template 197 * in the folder of the blog. 198 * 199 * @param templateName 200 * @param blogId 201 * @see TemplateSetStorage::removeGlobalTemplate 202 * @see TemplateSetStorage::removeBlogTemplate 203 * 204 * @return Returns true if removed successfully or false otherwise. 205 */ 206 function removeTemplate( $templateName, $blogId = 0 ) 207 { 208 if( $blogId == 0 ) { 209 $result = $this->removeGlobalTemplate( $templateName ); 210 } 211 else { 212 $result = $this->removeBlogTemplate( $templateName, $blogId ); 213 } 214 215 return $result; 216 } 217 218 /** 219 * Adds a new template. If the template already exists, it is not 220 * added again. 221 * 222 * @param templateName The name of the template we'd like to add 223 * @param blogId The identifier of the blog to which we'd like to add 224 * the template. If blogId == 0, then the template will be added as global. 225 * 226 * @return Returns true if ok or false otherwise. 227 */ 228 function addTemplate( $templateName, $blogId = 0 ) 229 { 230 if( $blogId == 0 ) { 231 // the template is global 232 $result = $this->addGlobalTemplate( $templateName ); 233 } 234 else { 235 // the template is local 236 $result = $this->addLocalTemplate( $templateName, $blogId ); 237 } 238 239 return $result; 240 } 241 242 /** 243 * Adds a global template to the site. 244 * 245 * @param templateName Name of the template we'd like to add 246 * @return Returns true if successful or false otherwise. 247 */ 248 function addGlobalTemplate( $templateName ) 249 { 250 $config =& Config::getConfig(); 251 252 // check if the value is there and if so, do nothing 253 $templateSets = new TemplateSets(); 254 if( $templateSets->isTemplate( $templateName )) 255 return true; 256 257 // let's make sure that we actually got an array... if not, then 258 // let's recreate the array to avoid further problems down the road! 259 $templates = $templateSets->getGlobalTemplates(); 260 if( !is_array($templates)) { 261 print("recreating the array!"); 262 $templates = Array(); 263 } 264 265 // if not there, we can add it 266 array_push( $templates, $templateName ); 267 268 // and store the values 269 $config->saveValue( "templates", $templates ); 270 271 return true; 272 } 273 274 /** 275 * Adds a local template to the given blog. 276 * 277 * @param templateName Name that the template will use 278 * @param blogId the identifier of the blog to which we're going to install 279 * the new template. 280 * @return True if successful or false otherwise. 281 */ 282 function addLocalTemplate( $templateName, $blogId ) 283 { 284 $blogs = new Blogs(); 285 $blog = $blogs->getBlogInfo( $blogId ); 286 $blogSettings = $blog->getSettings(); 287 if( empty($blogSettings) ) 288 return false; 289 290 // get the array with the templates 291 $blogTemplates = $blogSettings->getValue( "blog_templates" ); 292 293 // add a new one unless it's already there 294 if( $blogTemplates == "" || $blogTemplates == null ) 295 $blogTemplates = Array(); 296 297 if( in_array( $templateName, $blogTemplates )) 298 return true; 299 300 array_push( $blogTemplates, $templateName ); 301 $blogSettings->setValue( "blog_templates", $blogTemplates ); 302 $blog->setSettings( $blogSettings ); 303 $blogs->updateBlog( $blog ); 304 305 return true; 306 } 307 308 /** 309 * Creates the blog-specific folder where templates are stored. 310 * If the folder is not there, it will be created. If it is already there, 311 * then nothing will happen. 312 * 313 * @param blogId The identifier of the blog. 314 */ 315 function createBlogTemplateFolder( $blogId ) 316 { 317 // make sure that the blog-specific folder exists 318 $templateFolder = $this->getBaseTemplateFolder(); 319 $blogTemplateFolder = "$templateFolder/blog_".$blogId; 320 if( !File::isDir( $blogTemplateFolder )) { 321 File::createDir( $blogTemplateFolder, 0755 ); 322 } 323 324 return $blogTemplateFolder; 325 } 326 327 /** 328 * returns true if the given template file exists in the given template set, or false 329 * if not. 330 * 331 * @param fileName The name of the file, without the .template extension 332 * @param templateSet name of the template set. 333 * @return Returns true if file exists and is readable, or false otherwise. 334 */ 335 function templateExists( $fileName, $templateSet ) 336 { 337 $filePath = $this->getBaseTemplateFolder()."/".$templateSet."/".$fileName.".template"; 338 339 // first check if it exists at all 340 if( !File::exists( $filePath, $templateSet )) 341 return false; 342 343 // and if it does, check if it can be read 344 if( !File::isReadable( $filePath, $templateSet )) 345 return false; 346 347 return true; 348 } 349 350 /** 351 * returns the path where the admin templates are stored 352 * 353 * @return a path 354 */ 355 function getAdminTemplateFolder() 356 { 357 $templatePath = TemplateSetStorage::getBaseTemplateFolder()."/admin"; 358 359 return $templatePath; 360 } 361 362 /** 363 * returns the folder where the templates used by a plugin 364 * are stored 365 * 366 * @para pluginId 367 * @return At path 368 */ 369 function getPluginTemplateFolder( $pluginId ) 370 { 371 $templateFolder = "./plugins/".$pluginId."/templates/"; 372 373 return $templateFolder; 374 } 375 376 /** 377 * returns true if the template set has a screenshot available in disk 378 * 379 * @param templateName The name of the template 380 * @param blogId If the template is blog specific, then the blog id and if it is global, then 381 * '0' or no parameeter 382 */ 383 function isScreenshotAvailable( $templateName, $blogId = 0 ) 384 { 385 // build up the path to the screenshot file 386 $templatePath = $this->getTemplateFolder( $templateName, $blogId ); 387 // and return whether it is available or not 388 $screenshotPath = $templatePath."/screenshot.jpg"; 389 return( File::isReadable( $screenshotPath )); 390 } 391 392 /** 393 * returns the path to the blog screenshot 394 * 395 * @param templateName 396 * @param blogId 397 * @return The http path to the screenshot file or empty if not available 398 */ 399 function getScreenshotUrl( $templateName, $blogId = 0) 400 { 401 // check if available at all... 402 if( !$this->isScreenshotAvailable( $templateName, $blogId )) 403 return $this->getNoScreenshotUrl(); 404 405 $config =& Config::getConfig(); 406 $templateFolder = $this->getBaseTemplateFolder(); 407 $baseUrl = $config->getValue( "base_url" ); 408 if( $blogId == 0 ) 409 $screenshotPath = "$baseUrl/$templateFolder/$templateName/screenshot.jpg"; 410 else 411 $screenshotPath = "$baseUrl/$templateFolder/blog_{$blogId}/$templateName/screenshot.jpg"; 412 413 return $screenshotPath; 414 } 415 416 /** 417 * returns a default picture for those occasions when there is no screenshot 418 * available for the template 419 * 420 * @return a url 421 */ 422 function getNoScreenshotUrl() 423 { 424 $config =& Config::getConfig(); 425 $baseUrl = $config->getValue( "base_url" ); 426 $url = $baseUrl."/imgs/no-template-screenshot.jpg"; 427 428 return $url; 429 } 430 431 /** 432 * returns the path to the 'templates/misc/' folder 433 * 434 * @return path 435 * @static 436 */ 437 function getMiscTemplateFolder() 438 { 439 $templatePath = TemplateSetStorage::getBaseTemplateFolder()."/misc"; 440 return $templatePath; 441 } 442 } 443 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
| Généré le : Mon Nov 26 21:04:15 2007 | par Balluche grâce à PHPXref 0.7 |
|