/** * Добавляет один файл или целую директорию с файлами рекурсивно * * @param string $path * @param string $mask */ public function add($path, $mask = '#^[^\\.].+#') { if (is_dir($path)) { $files = File::findByMask($path, $mask); foreach ($files as $file) { $archive_file = str_replace(dirname($path) . DS, '', $file); $this->addFile($file, $archive_file); } } elseif (file_exists($path)) { $this->addFile($path, basename($path)); } }
/** * Обновление файлов по указанному пути * * @param $from Путь источника * @param $to Путь назначения */ private function update_files($from, $to) { $files = File::findByMask($from, '/(.+)/i'); foreach ($files as $file) { $source_file = str_replace($from, '', $file); $orig_file = $to . DS . $source_file; if ($source_file == 'config.php' or $source_file == 'site.php') { continue; } if (file_put_contents(file_get_contents($file), $orig_file)) { echo t('Файл <i>%s</i> успешно обновлён…', $source_file); } echo $source_file . '<br/>'; } }
/** * Clear cache folder */ public function clear() { if ($result = File::findByMask($this->options->path)) { foreach ($result as $path) { unlink($path); } } }
/** * Обработка Ajax-запросов * * @param string $action */ public function ajax_action($action = 'scan', $path = '') { if (!Ajax::is()) { return event('403'); } $ajax = new Ajax(); $ajax->success = TRUE; $ajax->text = ''; switch ($action) { case 'change': if ($lang = $this->input->post('lang')) { if ($this->getLangs(array($lang))) { $this->set('lang.lang', $lang); $this->config->store(); } else { $ajax->success = FALSE; } } break; case 'save': $path = $this->input->post('path'); if ($path && $this->checkPath($path)) { $file = ROOT . DS . $path . $this->prepareFilePath(); $options = config('lang'); $options->file = $file; $index = Lang::factory('temp', $options); $index->load(); } else { $index = Lang::factory('index'); } if ($values = $this->input->post('values')) { $index->import($values); } else { $source = $this->input->post('source'); $translation = $this->input->post('translation'); $index->set($source, $translation); } $index->save(); break; case 'scan': if (!$this->checkPath($path)) { exit(t("Вы указали недопустимый путь!")); } // Важно! Если выбираем только шестерёнки или только темы, // то для каждой из них обновляются языковые файлы if (preg_match('#(gears|themes)/?$#', $path)) { session('admin.lang.updateEach', TRUE); } $path = $path ? ROOT . DS . $path : ROOT; $index_file = $path . DS . LANG . DS . $this->lang . EXT; File::mkdir(dirname($index_file)); if ($this->input->get('reset')) { cogear()->session->remove('admin.lang.scan'); $ajax->action = 'reset'; } // Сначала сканируем PHP-файлы $files = session('admin.lang.scan'); if (!is_array($files)) { $files = File::findByMask($path, '/^.+\\.(php|js)$/i'); $files = array_reverse($files); $folder = $path === ROOT ? '/' : File::pathToUri($path); $ajax->text .= t('Сканирование папки <b>%s</b>…', $folder) . '<br/>'; session('admin.lang.scan.path', $path); session('admin.lang.index', array()); session('admin.lang.scan', $files); session('admin.lang.scan.counter', count($files)); session('admin.lang.scan.index', 1); session('admin.lang.scan.translations', 1); session('admin.lang.updateСurrent', FALSE); } if (!$files) { $ajax->success = FALSE; if (session('admin.lang.updateEach')) { $this->dumpIndex($ajax); cogear()->session->remove('admin.lang.updateEach'); cogear()->session->remove('admin.lang.updateCurrent'); } else { $ajax->finish = TRUE; } $ajax->text .= t('<p class="alert alert-success">Сканирование завершено.</p>'); $ajax->result = t('Найдено <b>%d</b> строк', session('admin.lang.scan.translations')); $ajax->text .= $ajax->result; $ajax->index = session('admin.lang.scan.index'); $ajax->total = session('admin.lang.scan.counter'); cogear()->session->remove('admin.lang.scan'); cogear()->session->remove('admin.lang.scan.counter'); // cogear()->session->remove('admin.lang.scan.index'); cogear()->session->remove('admin.lang.scan.translations'); /** * $options = config('lang'); $options->path = dirname($index_file); $index = Lang::factory('index', $options); $index->load(); foreach ($strings as $string) { $index->offsetExists($string) OR $index->set($string, ''); } $index->save(); */ } else { $file = array_pop($files); if (session('admin.lang.updateEach')) { preg_match('#(gear|theme)s(?:\\\\|/)([\\w_-]+)#', $file, $matches); if (!($current = session('admin.lang.updateСurrent'))) { session('admin.lang.updateСurrent', $current = array('type' => $matches[1], 'name' => $matches[2], 'file' => $file)); switch ($current['type']) { case 'gear': $ajax->text .= t('<p>Сканирую шестерёнку <b>«%s»</b>…<br/>', $current['name']); break; case 'theme': $ajax->text .= t('<p>Сканирую тему <b>«%s»</b>…<br/>', $current['name']); break; } } else { if ($current['name'] != $matches[2]) { $this->dumpIndex($ajax, array('type' => $matches[1], 'name' => $matches[2], 'file' => $file)); session('admin.lang.updateСurrent', $current = array('type' => $matches[1], 'name' => $matches[2], 'file' => $file)); } } } if ($strings = $this->parseFile($file)) { if (is_array($strings)) { $index = session('admin.lang.index'); $index = array_merge((array) $index, $strings); session('admin.lang.index', array_unique($index)); } } $ajax->text .= t("Файл <b>%s</b> успешно обработан. ", File::pathToUri($file)); $ajax->result .= $strings ? t('Найдено: <b>%d (строка|строки|строк)</b> для перевода.', sizeof($strings)) : t('Строки для перевода не найдены.'); $ajax->text .= $ajax->result; $ajax->result = icon('file') . ' <b>' . File::pathToUri($file) . '</b><br/>' . $ajax->result; $ajax->index = session('admin.lang.scan.index'); $ajax->total = session('admin.lang.scan.counter'); $ajax->strings = session('admin.lang.scan.translations'); session('admin.lang.scan.index', session('admin.lang.scan.index') + 1); $strings && session('admin.lang.scan.translations', session('admin.lang.scan.translations') + sizeof($strings)); session('admin.lang.scan', $files); } break; } $ajax->json(); }