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