PageRenderTime 65ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/libraries/nextend2/smartslider/smartslider/libraries/import.php

https://bitbucket.org/saltwaterdev/offshorefinancial.com
PHP | 293 lines | 232 code | 50 blank | 11 comment | 44 complexity | 99644583ccc83b3d4aaed9719f79e8c6 MD5 | raw file
Possible License(s): GPL-2.0, GPL-3.0, 0BSD, MIT, Apache-2.0, LGPL-2.1
  1. <?php
  2. N2Loader::import('libraries.zip.reader');
  3. N2Loader::import('libraries.backup', 'smartslider');
  4. class N2SmartSliderImport {
  5. /**
  6. * @var N2SmartSliderBackup
  7. */
  8. private $backup;
  9. private $imageTranslation = array();
  10. private $sectionTranslation = array();
  11. private $sliderId = 0;
  12. private $restore = false;
  13. public function enableRestore() {
  14. $this->restore = true;
  15. }
  16. public function import($filePathOrData, $groupID = 0, $imageImportMode = 'clone', $linkedVisuals = 1, $isFilePath = true) {
  17. if (!$isFilePath) {
  18. $folder = sys_get_temp_dir();
  19. if (!is_writable($folder)) {
  20. $folder = N2Filesystem::getNotWebCachePath();
  21. }
  22. $tmp = tempnam($folder, 'ss3');
  23. file_put_contents($tmp, $filePathOrData);
  24. $filePathOrData = $tmp;
  25. }
  26. $importData = N2ZipReader::read($filePathOrData);
  27. if (!is_array($importData)) {
  28. N2Message::error(n2_('The importing failed at the unzipping part.'));
  29. return false;
  30. } else if (!isset($importData['data'])) {
  31. if (array_key_exists("slider.ss2", $importData)) {
  32. N2Message::error(n2_('You can\'t import sliders from Smart Slider 2.'));
  33. }
  34. return false;
  35. }
  36. $this->backup = unserialize($importData['data']);
  37. if (!empty($this->backup->slider['type']) && $this->backup->slider['type'] == 'group') {
  38. // Groups can not be imported into groups
  39. $groupID = 0;
  40. }
  41. $this->sectionTranslation = array();
  42. $this->importVisuals($this->backup->visuals, $linkedVisuals);
  43. $sliderModel = new N2SmartsliderSlidersModel();
  44. if ($this->restore) {
  45. $this->sliderId = $sliderModel->restore($this->backup->slider, $groupID);
  46. } else {
  47. $this->sliderId = $sliderModel->import($this->backup->slider, $groupID);
  48. }
  49. if (!$this->sliderId) {
  50. return false;
  51. }
  52. switch ($imageImportMode) {
  53. case 'clone':
  54. $images = isset($importData['images']) ? $importData['images'] : array();
  55. $imageStore = new N2StoreImage('slider' . $this->sliderId, true);
  56. foreach ($images AS $file => $content) {
  57. $localImage = $imageStore->makeCache($file, $content);
  58. if ($localImage) {
  59. $this->imageTranslation[$file] = N2ImageHelper::dynamic(N2Uri::pathToUri($localImage));
  60. } else {
  61. $this->imageTranslation[$file] = $file;
  62. }
  63. if (!$this->imageTranslation[$file]) {
  64. $this->imageTranslation[$file] = array_search($file, $this->backup->imageTranslation);
  65. }
  66. }
  67. break;
  68. case 'update':
  69. $keys = array_keys($this->backup->NextendImageHelper_Export);
  70. $values = array_values($this->backup->NextendImageHelper_Export);
  71. foreach ($this->backup->imageTranslation AS $image => $value) {
  72. $this->imageTranslation[$value] = str_replace($keys, $values, $image);
  73. }
  74. break;
  75. default:
  76. break;
  77. }
  78. if (!empty($this->backup->slider['thumbnail'])) {
  79. $sliderModel->setThumbnail($this->sliderId, $this->fixImage($this->backup->slider['thumbnail']));
  80. }
  81. foreach ($this->backup->NextendImageManager_ImageData AS $image => $data) {
  82. $data['tablet']['image'] = $this->fixImage($data['tablet']['image']);
  83. $data['mobile']['image'] = $this->fixImage($data['mobile']['image']);
  84. $fixedImage = $this->fixImage($image);
  85. if (!N2ImageManager::hasImageData($fixedImage)) {
  86. N2ImageManager::addImageData($this->fixImage($image), $data);
  87. }
  88. }
  89. if (empty($this->backup->slider['type'])) {
  90. $this->backup->slider['type'] = 'simple';
  91. }
  92. if ($this->backup->slider['type'] == 'group') {
  93. /**
  94. * Import the sliders for the group!
  95. */
  96. foreach ($importData['sliders'] AS $k => $slider) {
  97. $import = new N2SmartSliderImport();
  98. if ($this->restore) {
  99. $import->enableRestore();
  100. }
  101. $import->import($slider, $this->sliderId, $imageImportMode, $linkedVisuals, false);
  102. }
  103. } else {
  104. unset($importData);
  105. $class = 'N2SSPluginType' . $this->backup->slider['type'];
  106. N2Loader::importPath(call_user_func(array(
  107. $class,
  108. "getPath"
  109. )) . NDS . 'backup');
  110. $class = 'N2SmartSliderBackup' . $this->backup->slider['type'];
  111. call_user_func_array(array(
  112. $class,
  113. 'import'
  114. ), array(
  115. $this,
  116. &$this->backup->slider
  117. ));
  118. $enabledWidgets = array();
  119. $plugins = array();
  120. N2Plugin::callPlugin('sswidget', 'onWidgetList', array(&$plugins));
  121. $params = $this->backup->slider['params'];
  122. foreach ($plugins AS $k => $v) {
  123. $widget = $params->get('widget' . $k);
  124. if ($widget && $widget != 'disabled') {
  125. $enabledWidgets[$k] = $widget;
  126. }
  127. }
  128. foreach ($enabledWidgets AS $k => $v) {
  129. $class = 'N2SSPluginWidget' . $k . $v;
  130. if (class_exists($class, false)) {
  131. $params->fillDefault(call_user_func(array(
  132. $class,
  133. 'getDefaults'
  134. )));
  135. call_user_func_array(array(
  136. $class,
  137. 'prepareImport'
  138. ), array(
  139. $this,
  140. $params
  141. ));
  142. } else {
  143. unset($enabledWidgets);
  144. }
  145. }
  146. $sliderModel->importUpdate($this->sliderId, $params);
  147. $generatorTranslation = array();
  148. N2Loader::import("models.generator", "smartslider");
  149. $generatorModel = new N2SmartsliderGeneratorModel();
  150. foreach ($this->backup->generators as $generator) {
  151. $generatorTranslation[$generator['id']] = $generatorModel->import($generator);
  152. }
  153. $slidesModel = new N2SmartsliderSlidesModel();
  154. for ($i = 0; $i < count($this->backup->slides); $i++) {
  155. $slide = $this->backup->slides[$i];
  156. $slide['params'] = new N2Data($slide['params'], true);
  157. $slide['thumbnail'] = $this->fixImage($slide['thumbnail']);
  158. $slide['params']->set('backgroundImage', $this->fixImage($slide['params']->get('backgroundImage')));
  159. $slide['params']->set('ligthboxImage', $this->fixImage($slide['params']->get('ligthboxImage')));
  160. $slide['params']->set('link', $this->fixLightbox($slide['params']->get('link')));
  161. $slide['params']->set('backgroundVideoMp4', $this->fixImage($slide['params']->get('backgroundVideoMp4')));
  162. $layers = json_decode($slide['slide'], true);
  163. self::prepareImportLayer($this, $layers);
  164. $slide['slide'] = json_encode($layers);
  165. if (isset($generatorTranslation[$slide['generator_id']])) {
  166. $slide['generator_id'] = $generatorTranslation[$slide['generator_id']];
  167. }
  168. $slidesModel->import($slide, $this->sliderId);
  169. }
  170. }
  171. return $this->sliderId;
  172. }
  173. /**
  174. * @param N2SmartSliderImport $import
  175. * @param array $layers
  176. */
  177. public static function prepareImportLayer($import, &$layers) {
  178. for ($i = 0; $i < count($layers); $i++) {
  179. if (isset($layers[$i]['type'])) {
  180. switch ($layers[$i]['type']) {
  181. case 'content':
  182. N2SSSlideComponentContent::prepareImport($import, $layers[$i]);
  183. break;
  184. case 'row':
  185. N2SSSlideComponentRow::prepareImport($import, $layers[$i]);
  186. break;
  187. case 'col':
  188. N2SSSlideComponentCol::prepareImport($import, $layers[$i]);
  189. break;
  190. case 'group':
  191. N2SSSlideComponentGroup::prepareImport($import, $layers[$i]);
  192. break;
  193. default:
  194. N2SSSlideComponentLayer::prepareImport($import, $layers[$i]);
  195. }
  196. } else {
  197. N2SSSlideComponentLayer::prepareImport($import, $layers[$i]);
  198. }
  199. }
  200. }
  201. public function fixImage($image) {
  202. if (isset($this->backup->imageTranslation[$image]) && isset($this->imageTranslation[$this->backup->imageTranslation[$image]])) {
  203. return $this->imageTranslation[$this->backup->imageTranslation[$image]];
  204. }
  205. return $image;
  206. }
  207. public function fixSection($idOrRaw) {
  208. if (isset($this->sectionTranslation[$idOrRaw])) {
  209. return $this->sectionTranslation[$idOrRaw];
  210. }
  211. return $idOrRaw;
  212. }
  213. public function fixLightbox($url) {
  214. preg_match('/^([a-zA-Z]+)\[(.*)](.*)/', $url, $matches);
  215. if (!empty($matches) && $matches[1] == 'lightbox') {
  216. $images = explode(',', $matches[2]);
  217. $newImages = array();
  218. foreach ($images AS $image) {
  219. $newImages[] = $this->fixImage($image);
  220. }
  221. $url = 'lightbox[' . implode(',', $newImages) . ']' . $matches[3];
  222. }
  223. return $url;
  224. }
  225. private function importVisuals($records, $linkedVisuals) {
  226. if (count($records)) {
  227. if (!$linkedVisuals) {
  228. foreach ($records AS $record) {
  229. $this->sectionTranslation[$record['id']] = $record['value'];
  230. }
  231. } else {
  232. $sets = array();
  233. foreach ($records AS $record) {
  234. $storage = N2Base::getApplication($record['application'])->storage;
  235. if (!isset($sets[$record['application'] . '_' . $record['section']])) {
  236. $sets[$record['application'] . '_' . $record['section']] = $storage->add($record['section'] . 'set', '', $this->backup->slider['title']);
  237. }
  238. $this->sectionTranslation[$record['id']] = $storage->add($record['section'], $sets[$record['application'] . '_' . $record['section']], $record['value']);
  239. }
  240. }
  241. }
  242. }
  243. }