1: <?php
2: 3: 4: 5:
6:
7: if (!defined('XOOPS_ROOT_PATH')) exit();
8:
9: require_once XOOPS_MODULE_PATH . "/legacyRender/class/AbstractEditAction.class.php";
10: require_once XOOPS_MODULE_PATH . "/legacyRender/admin/forms/TplsetUploadForm.class.php";
11:
12: class LegacyRender_TplsetUploadAction extends LegacyRender_Action
13: {
14: var $mActionForm = null;
15: var $mErrorMessages = array();
16:
17: function prepare(&$controller, &$xoopsUser)
18: {
19: $this->mActionForm =new LegacyRender_TplsetUploadForm();
20: $this->mActionForm->prepare();
21: }
22:
23: function getDefaultView(&$controller, &$xoopsUser)
24: {
25: return LEGACYRENDER_FRAME_VIEW_INPUT;
26: }
27:
28: function _addErrorMessage($msg)
29: {
30: $this->mErrorMessages[] = $msg;
31: }
32:
33: function execute(&$controller, &$xoopsUser)
34: {
35: if (xoops_getrequest('_form_control_cancel') != null) {
36: return LEGACYRENDER_FRAME_VIEW_CANCEL;
37: }
38:
39: $this->mActionForm->fetch();
40: $this->mActionForm->validate();
41:
42: if ($this->mActionForm->hasError()) {
43: return $this->getDefaultView($controller, $xoopsUser);
44: }
45:
46: require_once XOOPS_ROOT_PATH . "/class/class.tar.php";
47: $tar =new tar();
48:
49: $formFile = $this->mActionForm->get('upload');
50:
51:
52:
53:
54:
55:
56: $tar->openTar($formFile->_mTmpFileName);
57:
58: if (!is_array($tar->files)) {
59: return LEGACYRENDER_FRAME_VIEW_ERROR;
60: }
61:
62: $tplsetName = null;
63: foreach ($tar->files as $id => $info) {
64: $infoArr = explode('/', str_replace("\\", '/', $info['name']));
65: $tplsetName = $this->mActionForm->get('tplset_name');
66: if ($tplsetName == null) {
67: $tplsetName = trim($infoArr[0]);
68: }
69:
70: if ($tplsetName != null) {
71: break;
72: }
73: }
74:
75:
76:
77:
78: if ($tplsetName == null || preg_match('/[' . preg_quote('\/:*?"<>|','/') . ']/', $tplsetName)) {
79: $this->_addErrorMessage(_AD_LEGACYRENDER_ERROR_TPLSET_NAME_WRONG);
80: return LEGACYRENDER_FRAME_VIEW_ERROR;
81: }
82:
83: $handler =& xoops_getmodulehandler('tplset');
84: if ($handler->getCount(new Criteria('tplset_name', $tplsetName)) != 0) {
85: $this->_addErrorMessage(XCube_Utils::formatMessage(_AD_LEGACYRENDER_ERROR_TPLSET_ALREADY_EXISTS, $tplsetName));
86: return LEGACYRENDER_FRAME_VIEW_ERROR;
87: }
88:
89: $tplset =& $handler->create();
90: $tplset->set('tplset_name', $tplsetName);
91: if (!$handler->insert($tplset)) {
92: $this->_addErrorMessage(_AD_LEGACYRENDER_ERROR_COULD_NOT_SAVE_TPLSET);
93: return LEGACYRENDER_FRAME_VIEW_ERROR;
94: }
95:
96: $themeimages = array();
97: if (!$this->_fetchTemplateFiles($tar, $tplset, $themeimages)) {
98: return LEGACYRENDER_FRAME_VIEW_ERROR;
99: }
100:
101: if (!$this->_fetchImageset($tar, $tplset, $themeimages)) {
102: return LEGACYRENDER_FRAME_VIEW_ERROR;
103: }
104:
105: return LEGACYRENDER_FRAME_VIEW_SUCCESS;
106: }
107:
108: function _fetchTemplateFiles(&$tar, &$tplset, &$themeimages)
109: {
110: $handler =& xoops_getmodulehandler('tplfile');
111: foreach ($tar->files as $id => $info) {
112: $infoArr = explode('/', str_replace("\\", '/', $info['name']));
113: if (isset($infoArr[3]) && trim($infoArr[3]) == 'blocks') {
114: $default =& $handler->find('default', 'block', null, trim($infoArr[2]), trim($infoArr[4]));
115: } elseif ((!isset($infoArr[4]) || trim($infoArr[4]) == '') && $infoArr[1] == 'templates') {
116: $default =& $handler->find('default', 'module', null, trim($infoArr[2]), trim($infoArr[3]));
117: } elseif (isset($infoArr[3]) && trim($infoArr[3]) == 'images') {
118: $infoArr[2] = trim($infoArr[2]);
119: if (preg_match("/(.*)\.(gif|jpg|jpeg|png)$/i", $infoArr[2], $match)) {
120: $themeimages[] = array('name' => $infoArr[2], 'content' => $info['file']);
121: }
122: }
123: if (count($default) > 0) {
124: $tplfile =& $default[0]->createClone($tplset->get('tplset_name'));
125: $tplfile->Source->set('tpl_source', $info['file']);
126: $tplfile->set('tpl_lastimported', time());
127:
128: if (!$handler->insert($tplfile)) {
129: $this->_addErrorMessage(XCube_Utils::formatMessage(_AD_LEGACYRENDER_ERROR_COULD_NOT_SAVE_TPLFILE, $tplfile->get('tpl_file')));
130: }
131: unset($default);
132: }
133: unset($info);
134: }
135:
136: return true;
137: }
138:
139: function _fetchImageset(&$tar, &$tplset, &$themeimages)
140: {
141: if (count($themeimages) == 0) {
142: return true;
143: }
144:
145: $handler =& xoops_gethandler('imageset');
146: $imgset =& $handler->create();
147: $imgset->set('imgset_name', $tplset->get('tplset_name'));
148: $imgset->set('imgset_refid', 0);
149:
150: if (!$handler->insert($imgset)) {
151: $this->_addErrorMessage(XCube_Utils::formatMessage(_AD_LEGACYRENDER_ERROR_COULD_NOT_SAVE_IMAGESET, $tplset->get('tplset_name')));
152: return false;
153: }
154:
155: if (!$handler->linktplset($imgset->get('imgset_id'), $tplset->get('tplset_name'))) {
156: $this->_addErrorMessage(_AD_LEGACYRENDER_ERROR_COULD_NOT_SAVE_LINKTPLSET);
157: return false;
158: }
159:
160: $handler =& xoops_gethandler('imagesetimg');
161: for ($i = 0; $i < count($themeimages); $i++) {
162: if (isset($themeimages[$i]['name']) && $themeimages[$i]['name'] != '') {
163: $image =& $handler->create();
164: $image->set('imgsetimg_file', $themeimages[$i]['name']);
165: $image->set('imgsetimg_imgset', $imgset->get('imgset_id'));
166: $image->set('imgsetimg_body', $themeimages[$i]['content'], true);
167: if (!$handler->insert($image)) {
168: $this->_addErrorMessage(XCube_Utils::formatMessage(_AD_LEGACYRENDER_ERROR_COULD_NOT_SAVE_IMAGE_FILE, $image->get('imgsetimg_file')));
169: }
170: unset($image);
171: }
172: }
173:
174: return true;
175: }
176:
177: function executeViewInput(&$controller, &$xoopsUser, &$render)
178: {
179: $render->setTemplateName("tplset_upload.html");
180: $render->setAttribute('actionForm', $this->mActionForm);
181: }
182:
183: function executeViewSuccess(&$controller, &$xoopsUser, &$render)
184: {
185: $controller->executeForward("./index.php?action=TplsetList");
186: }
187:
188: function executeViewError(&$controller, &$xoopsUser, &$render)
189: {
190: if (count($this->mErrorMessages) == 0) {
191: $controller->executeRedirect("./index.php?action=TplsetList", 1, _AD_LEGACYRENDER_ERROR_DBUPDATE_FAILED);
192: }
193: else {
194: $render->setTemplateName("tplset_upload_error.html");
195: $render->setAttribute('errorMessages', $this->mErrorMessages);
196: }
197: }
198:
199: function executeViewCancel(&$controller, &$xoopsUser, &$render)
200: {
201: $controller->executeForward("./index.php?action=TplsetList");
202: }
203: }
204:
205: ?>
206: