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