1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14:
15: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
16:
17: 18: 19: 20: 21: 22:
23: class cLayoutSynchronizer {
24:
25: 26: 27: 28:
29: protected $_cfg;
30:
31: 32: 33: 34:
35: protected $_cfgClient;
36:
37: 38: 39: 40:
41: protected $_lang;
42:
43: 44: 45: 46:
47: protected $_client;
48:
49: 50: 51: 52:
53: private $_outputMessage = array();
54:
55: 56: 57: 58: 59: 60: 61:
62: public function __construct($cfg, $cfgClient, $lang, $client) {
63: $this->_cfg = $cfg;
64: $this->_cfgClient = $cfgClient;
65: $this->_lang = $lang;
66: $this->_client = $client;
67: }
68:
69: 70: 71: 72: 73: 74: 75: 76:
77: private function _addOrUpdateLayout($dir, $oldLayoutName, $newLayoutName, $idclient) {
78:
79: if ($this->_isExistInTable($oldLayoutName, $idclient) == false) {
80:
81: $layoutCollection = new cApiLayoutCollection();
82: $layoutCollection->create($newLayoutName, $idclient, $newLayoutName);
83:
84:
85: if (!cFileHandler::exists($dir . $newLayoutName . '/' . $newLayoutName . '.html')) {
86: cFileHandler::write($dir . $newLayoutName . '/' . $newLayoutName . '.html', '');
87: }
88:
89:
90: $this->_outputMessage['info'][] = sprintf(i18n("Layout synchronization successful: %s"), $newLayoutName);
91: } else {
92:
93: if ($oldLayoutName != $newLayoutName) {
94: $this->_updateModulnameInDb($oldLayoutName, $newLayoutName, $idclient);
95: }
96: }
97: }
98:
99: 100: 101: 102: 103: 104: 105: 106: 107: 108:
109: private function _updateModulnameInDb($oldName, $newName, $idclient) {
110: $oLayColl = new cApiLayoutCollection();
111: $oLayColl->select("alias='" . $oLayColl->escape($oldName) . "' AND idclient=" . (int) $idclient);
112: if (false !== $oLay = $oLayColl->next()) {
113: $oLay->set('alias', $newName);
114: $oLay->store();
115: }
116: }
117:
118: 119: 120: 121: 122: 123: 124: 125: 126:
127: private function _renameFileAndDir($dir, $dirNameOld, $dirNameNew, $client) {
128: if (rename($dir . $dirNameOld, $dir . $dirNameNew) == false) {
129: return false;
130: }
131:
132: $this->_renameFiles($dir, $dirNameOld, $dirNameNew);
133:
134: return true;
135: }
136:
137: 138: 139: 140: 141: 142: 143: 144: 145:
146: private function _isExistInTable($alias, $idclient) {
147:
148: $oLayColl = new cApiLayoutCollection();
149: $ids = $oLayColl->getIdsByWhereClause("alias='" . $oLayColl->escape($alias) . "' AND idclient=" . (int) $idclient);
150: return (count($ids) > 0) ? true : false;
151: }
152:
153: 154: 155: 156: 157: 158: 159: 160: 161: 162:
163: private function _renameFiles($dir, $oldLayoutName, $newLayoutName) {
164: if (cFileHandler::exists($dir . $newLayoutName . '/' . $oldLayoutName . '.html') == true) {
165: rename($dir . $newLayoutName . '/' . $oldLayoutName . '.html', $dir . $newLayoutName . '/' . $newLayoutName . '.html');
166: }
167: }
168:
169: 170: 171: 172: 173: 174: 175: 176:
177: public function setLastModified($timestamp, $idlay) {
178: $oLay = new cApiLayout((int) $idlay);
179: if ($oLay->isLoaded()) {
180: $oLay->set('lastmodified', date('Y-m-d H:i:s', $timestamp));
181: $oLay->store();
182: }
183: }
184:
185: 186: 187: 188:
189: private function _compareFileAndLayoutTimestamp() {
190:
191: $sql = sprintf("SELECT UNIX_TIMESTAMP(lastmodified) AS lastmodified, alias, name, description, idlay FROM %s WHERE idclient=%s", $this->_cfg['tab']['lay'], $this->_client);
192: $notification = new cGuiNotification();
193: $dir = $this->_cfgClient[$this->_client]['layout']['path'];
194:
195: $db = cRegistry::getDb();
196: $db->query($sql);
197: $retIdMod = 0;
198: while ($db->nextRecord()) {
199: $lastmodified = $db->f('lastmodified');
200:
201:
202: if (is_dir($dir . $db->f('alias') . '/')) {
203: if (cFileHandler::exists($dir . $db->f('alias') . '/' . $db->f('alias') . '.html')) {
204: $lastmodifiedLayout = filemtime($dir . $db->f('alias') . '/' . $db->f('alias') . '.html');
205:
206:
207: if ($lastmodified < $lastmodifiedLayout) {
208:
209: $this->setLastModified($lastmodifiedLayout, $db->f('idlay'));
210: $layout = new cLayoutHandler($db->f('idlay'), ' ', $this->_cfg, $this->_lang);
211:
212: conGenerateCodeForAllartsUsingLayout($db->f('idlay'));
213: $this->_outputMessage['info'][] = i18n("Layout synchronization successful: ") . $db->f('name');
214: }
215: }
216: } else {
217: $oLayout = new cApiLayout($db->f('idlay'));
218:
219: $layout = new cLayoutHandler($db->f('idlay'), '', $this->_cfg, $this->_lang);
220:
221: if ($oLayout->isInUse()) {
222:
223: $layout->saveLayout('');
224: $this->_outputMessage['info'][] = i18n("Layout synchronization successful, created: ") . $db->f('name');
225: } else {
226:
227: if ($layout->eraseLayout()) {
228: layDeleteLayout($db->f('idlay'));
229: $this->_outputMessage['info'][] = i18n("Layout synchronization successful, deleted: ") . $db->f('name');
230: } else {
231: $this->_outputMessage['error'][] = i18n("Synchronization failed could not delete layout: ") . $db->f('name');
232: }
233: }
234: }
235: }
236: }
237:
238: 239:
240: private function _showOutputMessage() {
241: $emptyMessage = true;
242: $notification = new cGuiNotification();
243: foreach ($this->_outputMessage as $typ) {
244: foreach ($typ as $message) {
245: $emptyMessage = false;
246:
247: $notification->displayNotification($typ, $message);
248: }
249: }
250: if ($emptyMessage) {
251: $notification->displayNotification('info', i18n("Synchronization successful!"));
252: }
253: }
254:
255: 256: 257: 258: 259: 260:
261: public function synchronize() {
262:
263: $this->_compareFileAndLayoutTimestamp();
264:
265:
266: $dir = $this->_cfgClient[$this->_client]['layout']['path'];
267:
268:
269: if (!is_dir($dir)) {
270: return false;
271: }
272:
273: if (false !== ($handle = cDirHandler::read($dir))) {
274: foreach ($handle as $file) {
275:
276:
277: if (cFileHandler::fileNameBeginsWithDot($file)) {
278: continue;
279: }
280:
281:
282: if (false === is_dir($dir . $file . "/")) {
283: continue;
284: }
285:
286: $newFile = strtolower(cString::cleanURLCharacters($file));
287:
288: if ($newFile == $file) {
289:
290: $this->_addOrUpdateLayout($dir, $file, $newFile, $this->_client);
291: } else {
292:
293: if (is_dir($dir . $newFile) && strtolower($file) != $newFile) {
294:
295:
296: $newDirName = $newFile . substr(md5(time() . rand(0, time())), 0, 4);
297:
298: if ($this->_renameFileAndDir($dir, $file, $newDirName, $this->_client) != false) {
299: $this->_addOrUpdateLayout($dir, $file, $newDirName, $this->_client);
300: }
301: } else {
302:
303:
304: if ($this->_renameFileAndDir($dir, $file, $newFile, $this->_client) != false) {
305: $this->_addOrUpdateLayout($dir, $file, $newFile, $this->_client);
306: }
307: }
308: }
309: }
310: }
311:
312: $this->_showOutputMessage();
313: }
314: }
315: