Overview

Packages

  • Core
    • Authentication
    • Backend
    • Cache
    • CEC
    • Chain
    • ContentType
    • Database
    • Datatype
    • Debug
    • Exception
    • Frontend
      • Search
      • URI
      • Util
    • GenericDB
      • Model
    • GUI
      • HTML
    • I18N
    • LayoutHandler
    • Log
    • Security
    • Session
    • Util
    • Validation
    • Versioning
    • XML
  • Module
    • ContentSitemapHtml
    • ContentSitemapXml
    • ContentUserForum
    • NavigationMain
    • NavigationTop
  • mpAutoloaderClassMap
  • None
  • Plugin
    • ContentAllocation
    • CronjobOverview
    • FormAssistant
    • FrontendLogic
    • FrontendUsers
    • Linkchecker
    • ModRewrite
    • Newsletter
    • Repository
      • FrontendNavigation
      • KeywordDensity
    • SearchSolr
    • SmartyWrapper
    • UrlShortener
    • UserForum
    • Workflow
  • PluginManager
  • Setup
    • Form
    • GUI
    • Helper
      • Environment
      • Filesystem
      • MySQL
      • PHP
    • UpgradeJob

Classes

  • cLayoutHandler
  • cLayoutSynchronizer
  • Overview
  • Package
  • Class
  • Tree
  • Deprecated
  • Todo
  1: <?php
  2: /**
  3:  * This file contains the layout handler class.
  4:  *
  5:  * @package    Core
  6:  * @subpackage LayoutHandler
  7:  * @version    SVN Revision $Rev:$
  8:  *
  9:  * @author     Rusmir Jusufovic
 10:  * @copyright  four for business AG <www.4fb.de>
 11:  * @license    http://www.contenido.org/license/LIZENZ.txt
 12:  * @link       http://www.4fb.de
 13:  * @link       http://www.contenido.org
 14:  */
 15: 
 16: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
 17: 
 18: /**
 19:  * This class controls all layouts in filesystem.
 20:  *
 21:  * @package    Core
 22:  * @subpackage LayoutHandler
 23:  */
 24: class cLayoutHandler {
 25: 
 26:     /**
 27:      * The ID of the layout
 28:      * @var int
 29:      */
 30:     protected $_layoutId = 0;
 31: 
 32:     /**
 33:      * The code of the layout
 34:      * @var string
 35:      */
 36:     protected $_layoutCode = "";
 37:     protected $_db = null;
 38: 
 39:     /**
 40:      * Layout name
 41:      * @var string
 42:      */
 43:     protected $_layoutName = "";
 44: 
 45:     /**
 46:      * The contenido cfg
 47:      * @var array
 48:      */
 49:     protected $_cfg = array();
 50: 
 51:     /**
 52:      * Encoding of the page
 53:      * @var string
 54:      */
 55:     protected $_encoding;
 56: 
 57:     /**
 58:      * Layout path
 59:      * [layout_path].layoutName/
 60:      * @var string
 61:      */
 62:     protected $_layoutPath = "";
 63: 
 64:     /**
 65:      * Main path of layouts.
 66:      * [layout_path].layouts
 67:      * @var string
 68:      */
 69:     protected $_layoutMainPath = "";
 70: 
 71:     /**
 72:      * File name of the layout ([layoutname].html
 73:      * @var string
 74:      */
 75:     protected $_fileName = "";
 76: 
 77:     /**
 78:      * Construct of the class
 79:      */
 80:     public function __construct($layoutId = 0, $layoutCode = "", $cfg = array(), $lang = 0, $db = null) {
 81:         if ($db === null) {
 82:             $db = cRegistry::getDb();
 83:         }
 84: 
 85:         $this->_layoutId = $layoutId;
 86:         $this->_db = $db;
 87:         $this->init($layoutId, $layoutCode, $cfg, $lang);
 88:     }
 89: 
 90:     /**
 91:      * Get method for Layout path
 92:      * @return string
 93:      */
 94:     public function _getLayoutPath() {
 95:         return $this->_layoutPath;
 96:     }
 97: 
 98:     /**
 99:      * Get method for Filename
100:      * @return string
101:      */
102:     public function _getFileName() {
103:         return $this->_fileName;
104:     }
105: 
106:     /**
107:      * Look in layout directory if layout [$layoutAlias] directory exists
108:      *
109:      * @param string $layoutAlias
110:      * @return boolean if file exist true
111:      */
112:     static function existLayout($layoutAlias, $cfgClient, $client) {
113:         $file = $cfgClient[$client]['layout']['path'] . $layoutAlias . '/';
114:         return cFileHandler::exists($file);
115:     }
116: 
117:     /**
118:      * Init all vars for the class
119:      *
120:      * @param int $layoutId
121:      * @param string $layoutCode
122:      * @param array $cfg
123:      * @param int $language
124:      */
125:     public function init($layoutId, $layoutCode, $cfg, $language) {
126:         $this->_layoutCode = $layoutCode;
127:         $this->_cfg = $cfg;
128: 
129:         // set encoding
130:         $this->_setEncoding($language);
131: 
132:         if ((int) $layoutId == 0) {
133:             return;
134:         }
135: 
136:         global $cfgClient, $client;
137: 
138:         $cApiLayout = new cApiLayout($layoutId);
139: 
140:         if ($cApiLayout->virgin == false && is_array($cfgClient) && (int) $client > 0) {
141:             $this->_layoutName = $cApiLayout->get('alias');
142:             $this->_layoutMainPath = $cfgClient[$client]['layout']['path'];
143:             $this->_layoutPath = $this->_layoutMainPath . $this->_layoutName . "/";
144:             $this->_fileName = $this->_layoutName . ".html";
145: 
146:             // make directoryies for layout
147:             $this->_makeDirectories();
148:         }
149:     }
150: 
151:     /**
152:      * Get the layout name
153:      * @return string layoutname
154:      */
155:     public function getLayoutName() {
156:         return $this->_layoutName;
157:     }
158: 
159:     /**
160:      * Init class vars with values, only use for setup or upgrade
161:      *
162:      * @param cDb $dbObject
163:      */
164:     public function initWithDbObject($dbObject) {
165:         global $cfgClient, $client;
166: 
167:         $this->_layoutCode = $dbObject->f("code");
168:         $this->_layoutName = $dbObject->f("alias");
169:         $this->_layoutMainPath = $cfgClient[$dbObject->f("idclient")]['layout']['path'];
170:         $this->_layoutPath = $this->_layoutMainPath . $this->_layoutName . "/";
171:         $this->_fileName = $this->_layoutName . ".html";
172: 
173:         // make directories for layout
174:         $this->_makeDirectories();
175:     }
176: 
177:     /**
178:      * Make all directories for layout. Main directory and Layout directory
179:      * @return boolean true if successfully
180:      */
181:     private function _makeDirectories() {
182:         if ($this->_makeDirectory($this->_layoutMainPath)) {
183:             if ($this->_makeDirectory($this->_layoutPath)) {
184:                 return true;
185:             }
186:         }
187: 
188:         return false;
189:     }
190: 
191:     /**
192:      * Make directory
193:      * @param string $directory
194:      * @return boolean true if succssesfully
195:      */
196:     private function _makeDirectory($directory) {
197:         if (is_dir($directory)) {
198:             $success = true;
199:         } else {
200:             $success = mkdir($directory);
201:             if ($success) {
202:                 cFileHandler::setDefaultDirPerms($directory);
203:             }
204:         }
205: 
206:         return $success;
207:     }
208: 
209:     /**
210:      * Save encoding from language.
211:      * @param int $lang
212:      */
213:     private function _setEncoding($lang) {
214:         if ((int) $lang == 0) {
215:             $clientId = cRegistry::getClientId();
216: 
217:             $clientsLangColl = new cApiClientLanguageCollection();
218:             $clientLanguages = $clientsLangColl->getLanguagesByClient($clientId);
219:             sort($clientLanguages);
220: 
221:             if (isset($clientLanguages[0]) && (int) $clientLanguages[0] != 0) {
222:                 $languageId = $clientLanguages[0];
223:             }
224:         } else {
225:             $languageId = $lang;
226:         }
227: 
228:         $cApiLanguage = new cApiLanguage($languageId);
229:         $encoding = $cApiLanguage->get('encoding');
230: 
231:         $this->_encoding = $encoding;
232:     }
233: 
234:     /**
235:      * Can write/create a file
236:      *
237:      * @param string $fileName file name
238:      * @param string $directory directory where is the file
239:      * @return boolean, success true else false
240:      */
241:     public function isWritable($fileName, $directory) {
242:         if (cFileHandler::exists($fileName)) {
243:             if (!is_writable($fileName)) {
244:                 return false;
245:             }
246:         } else {
247:             if (!is_writable($directory)) {
248:                 return false;
249:             }
250:         }
251: 
252:         return true;
253:     }
254: 
255:     /**
256:      * Save Layout
257:      *
258:      * @param string $layoutCode
259:      *
260:      * @return boolean true
261:      */
262:     public function saveLayout($layoutCode = '') {
263:         $fileName = $this->_layoutPath . $this->_fileName;
264: 
265:         if (!$this->isWritable($fileName, $this->_layoutPath)) {
266:             return false;
267:         }
268: 
269:         return $this->_save($layoutCode);
270:     }
271: 
272:     /**
273:      * Save the layout only if layout doesn't exist in filesystem!
274:      * Use it for upgrade!
275:      *
276:      * @param string $layoutCode
277:      */
278:     public function saveLayoutByUpgrade($layoutCode = '') {
279:         // if file exist dont overwirte it
280:         if (cFileHandler::exists($this->_layoutPath . $this->_fileName)) {
281:             return true;
282:         }
283: 
284:         return $this->_save($layoutCode);
285:     }
286: 
287:     private function _save($layoutCode = '') {
288:         if ($layoutCode == '') {
289:             $layoutCode = $this->_layoutCode;
290:         }
291: 
292:         // exist layout path
293:         if (!is_dir($this->_layoutPath)) {
294:             return false;
295:         }
296: 
297:         // convert
298:         $fileEncoding = getEffectiveSetting('encoding', 'file_encoding', 'UTF-8');
299:         $layoutCode = iconv($this->_encoding, $fileEncoding, $layoutCode);
300: 
301:         $save = cFileHandler::write($this->_layoutPath . $this->_fileName, $layoutCode);
302: 
303:         return (strlen($layoutCode) == 0 && $save == 0) || $save > 0;
304:     }
305: 
306:     /**
307:      * Removes this layout from the filesystem.
308:      * Also deletes the version files.
309:      *
310:      * @return boolean true on success or false on failure
311:      */
312:     public function eraseLayout() {
313:         global $area, $frame;
314:         $cfg = cRegistry::getConfig();
315:         $cfgClient = cRegistry::getClientConfig();
316:         $db = cRegistry::getDb();
317:         $client = cRegistry::getClientId();
318: 
319:         $layoutVersion = new cVersionLayout($this->_layoutId, $cfg, $cfgClient, $db, $client, $area, $frame);
320:         $success = true;
321:         if (count($layoutVersion->getRevisionFiles()) > 0 && !$layoutVersion->deleteFile()) {
322:             $success = false;
323:         }
324: 
325:         return $success && cFileHandler::recursiveRmdir($this->_layoutPath);
326:     }
327: 
328:     /**
329:      * Rename the Layout directory and layout file
330:      * @param string $old
331:      * @param string $new
332:      */
333:     public function rename($old, $new) {
334:         // try to rename the dir
335:         $newPath = $this->_layoutMainPath . $new . "/";
336: 
337:         $newFileName = $new . ".html";
338: 
339:         if (rename($this->_layoutPath, $newPath) == FALSE) {
340:             return false;
341:         }
342: 
343:         // if file input exist rename it
344:         if (!cFileHandler::exists($newPath . $this->_fileName)) {
345:             return false;
346:         }
347: 
348:         if (!rename($newPath . $this->_fileName, $newPath . $newFileName)) {
349:             return false;
350:         }
351: 
352:         $this->_layoutName = $new;
353:         $this->_layoutPath = $this->_layoutMainPath . $this->_layoutName . "/";
354:         $this->_fileName = $this->_layoutName . ".html";
355: 
356:         return true;
357:     }
358: 
359:     /**
360:      * Get the contents of the file
361:      *
362:      * @return content or false
363:      */
364:     public function getLayoutCode() {
365:         // cant read it dont exist file
366:         if (!is_readable($this->_layoutPath . $this->_fileName)) {
367:             return false;
368:         }
369: 
370:         if (($content = cFileHandler::read($this->_layoutPath . $this->_fileName)) === FALSE) {
371:             return false;
372:         } else {
373:             // convert
374:             $fileEncoding = getEffectiveSetting('encoding', 'file_encoding', 'UTF-8');
375:             $content = iconv($fileEncoding, $this->_encoding . "//IGNORE", $content);
376:             return $content;
377:         }
378:     }
379: 
380:     /**
381:      * Save all layout in file system.
382:      * Use it for upgrade.
383:      *
384:      * @param cDb database object
385:      * @param array CONTENIDO config array
386:      * @throws cException if the layout could not be saved
387:      */
388:     public static function upgrade($adb, $cfg, $clientId) {
389:         // get name of layout and frontendpath
390:         if (!$adb->query("SELECT * FROM `%s` WHERE idclient='%s'", $cfg['tab']['lay'], $clientId)) {
391:             return;
392:         }
393: 
394:         while ($adb->nextRecord()) {
395:             // init class var for save
396:             $layout = new cLayoutHandler();
397:             $layout->initWithDbObject($adb);
398:             if ($layout->saveLayoutByUpgrade($adb->f('code')) == false) {
399:                 throw new cException('Can not save layout.' . print_r($layout, true));
400:             }
401:         }
402: 
403:         // all layouts are saved, so remove the code field from _lay
404:         $sql = sprintf("UPDATE %s SET code = '' WHERE idclient='%s'", $cfg['tab']['lay'], $clientId);
405:         $adb->query($sql);
406:     }
407: 
408: }
409: 
CMS CONTENIDO 4.9.0 API documentation generated by ApiGen 2.8.0