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