allow empty 'elements' from input data
[Pman.Cms] / Import / Pages.php
1 <?php
2
3 require_once 'Pman.php';
4
5 class Pman_Cms_Import_Pages extends Pman
6 {
7     
8     static $cli_desc = "Imports a ZIP file exported from the CMS";
9  
10     static $cli_opts = array(
11         'file' => array(
12             'desc' => 'zip file directory',
13             'short' => 'f',
14             'default' => '',
15             'min' => 1,
16             'max' => 1,
17         ),
18     );
19     
20     var $cli = false;
21     
22     var $opts;
23     
24     var $mapping = array();
25     var $links = array();
26     
27     function getAuth() 
28     {
29         $ff = HTML_FlexyFramework::get();
30         
31         if (empty($ff->cli)) {
32             $this->jerr('cli Only');
33         }
34         
35         if(!$this->getAuthUser()){
36             return false;
37         }
38         
39         return true;
40     }
41     
42     function getAuthUser()
43     {
44         $ff = HTML_FlexyFramework::get();
45         
46         $tbl = empty($ff->Pman['authTable']) ? 'core_person' : $ff->Pman['authTable'];
47         
48         $this->authUser = DB_DataObject::factory($tbl);
49         
50         $this->authUser->orderBy('id DESC');
51         
52         if(!$this->authUser->find(true)){
53             return false;
54         }
55         
56         return true;
57     }
58      
59     function get($tbl, $opts = array())
60     {
61         PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, array($this, 'onPearError'));
62         
63         if(empty($opts['file']) || !file_exists($opts['file'])){
64             $this->jerr('Please assign your zip file path [-f]');
65         }
66         
67 //        Pman/Hpasite/DummyPages/cms-page-dummy-page.zip
68         
69         $this->zipFile = $opts['file'];
70         
71         $this->check_environment();
72         
73         $this->check_database();
74         
75         if(!file_exists($this->zipFile)){
76             $this->jerr("Please upload the zip to {$this->zipFile}");
77         }
78         
79         $this->zip = new ZipArchive();
80
81         if ($this->zip->open($this->zipFile) !== true) {
82             $this->jerr('Can not open zip file');
83         }
84         
85         $this->tempDir = ini_get('session.save_path');
86         
87         $this->zip->extractTo($this->tempDir);
88         
89         $this->zip->close();
90         
91         $json = file_get_contents("{$this->tempDir}/data.json");
92         
93         if($json === false){
94             $this->jerr('Can not load the data.json');
95         }
96         
97         $this->data = json_decode($json, true);
98         
99         $this->processCmsPageType();
100         
101         $this->processCmsTemplate();
102         
103         $this->processCmsTemplateElement();
104         
105         $this->processCmsPages();
106         
107         $this->processPageLinks();
108         
109         $this->processCategories();
110         
111         $this->processImages();
112         
113         $this->clean();
114         
115         $this->jok("OK");
116     }
117     
118     function check_environment()
119     {
120         $ui = posix_getpwuid(posix_geteuid());
121         
122         if(empty($ui['name']) || $ui['name'] != 'www-data'){
123             $this->jerr('Please use www-data');
124         }
125         
126         if (!extension_loaded('zip')) { // sudo apt-get install php5.6-zip??
127             $this->jerr('Please install php zip extension');
128         }
129     }
130     
131     function check_database()
132     {
133         $cms_page = DB_DataObject::factory('cms_page');
134         
135         if($cms_page->count()){
136             $this->jerr('Please delete all cms pages before running this');
137         }
138     }
139     
140     function clean()
141     {
142         unlink("{$this->tempDir}/data.json");
143         
144         if(empty($this->data['photos'])){
145             return;
146         }
147         
148         foreach ($this->data['photos'] as $k => $v){
149             unlink("{$this->tempDir}/{$v}");
150         }
151         
152     }
153     
154     function processCmsPageType()
155     {
156         $this->mapping['types'] = array();
157                 
158         if(empty($this->data['types'])){
159             $this->jerr('Invalid Json Data [types]');
160         }
161         
162         foreach ($this->data['types'] as $k => $v){
163             
164             $core_enum = DB_DataObject::factory('core_enum');
165             $core_enum->setFrom(array(
166                 'etype' => $v['etype'],
167                 'active' => 1,
168                 'name' => $v['name']
169             ));
170             
171             if(!$core_enum->find(true)){
172                 $this->jerr("Can not found type '{$v['name']}', type run UpdateDatabase");
173             }
174             
175             $this->mapping['types'][$v['id']] = $core_enum->id;
176         }
177         
178     }
179     
180     function processCmsTemplate()
181     {
182         $this->mapping['templates'] = array();
183         
184         if(empty($this->data['templates'])){
185             $this->jerr('Invalid Json Data [templates]');
186         }
187         
188         foreach ($this->data['templates'] as $k => $v){
189             
190             $cms_template = DB_DataObject::factory('cms_template');
191             $cms_template->setFrom(array(
192                 'template' => $v['template']
193             ));
194             
195             if(!$cms_template->find(true)){
196                 $this->jerr("Can not found template '{$v['template']}', type run UpdateDatabase");
197             }
198             
199             $this->mapping['templates'][$v['id']] = $cms_template->id;
200         }
201         
202     }
203     
204     function processCmsTemplateElement()
205     {
206         $this->mapping['elements'] = array();
207         
208         if(empty($this->data['elements'])){
209             return;
210             // $this->jerr('Invalid Json Data [elements]');
211         }
212         
213         foreach ($this->data['elements'] as $k => $v){
214             
215             $cms_template_element = DB_DataObject::factory('cms_template_element');
216             $cms_template_element->setFrom(array(
217                 'name' => $v['name'],
218                 'template_id' => $this->mapping['templates'][$v['template_id']]
219             ));
220             
221             if(!$cms_template_element->find(true)){
222                 $this->jerr("Can not found element'{$v['name']}', type run UpdateDatabase");
223             }
224             
225             $this->mapping['elements'][$v['id']] = $cms_template_element->id;
226         }
227         
228     }
229     
230     function processCmsPages()
231     {
232         if(empty($this->data['pages'])){
233             $this->jerr('Invalid Json Data [pages]');
234         }
235         
236         foreach ($this->data['pages'] as $k => $v){
237             
238             $cms_page = DB_DataObject::factory('cms_page');
239             
240             $cms_page->setFrom($v);
241             
242             $cms_page->setFrom(array(
243                 'id' => 0,
244                 'author_id' => $this->authUser->id,
245                 'template_id' => (empty($v['template_id'])) ? 0 : $this->mapping['templates'][$v['template_id']],
246                 'element_id' => (empty($v['element_id'])) ? 0 : $this->mapping['elements'][$v['element_id']],
247                 'page_type_id' => (empty($v['page_type_id'])) ? 0 : $this->mapping['types'][$v['page_type_id']],
248                 'parent_id' => 0,
249                 'menu_page_id' => 0,
250                 'translation_of_id' => 0,
251                 'to_replace_id' => 0, // not sure the usage, but let's handle it as well
252                 'category_page_id' => 0 // not sure the usage, but let's handle it as well
253             ));
254             
255             $cms_page->insert();
256             
257             $this->mapping['pages'][$v['id']] = $cms_page->id;
258             
259             $this->links[$cms_page->id] = array(
260                 'parent_id' => (empty($v['parent_id'])) ? 0 : $v['parent_id'],
261                 'menu_page_id' => (empty($v['menu_page_id'])) ? 0 : $v['menu_page_id'],
262                 'translation_of_id' => (empty($v['translation_of_id'])) ? 0 : $v['translation_of_id'],
263                 'to_replace_id' => (empty($v['to_replace_id'])) ? 0 : $v['to_replace_id'],
264                 'category_page_id' => (empty($v['category_page_id'])) ? 0 : $v['category_page_id']
265             );
266         }
267         
268     }
269     
270     function processPageLinks()
271     {
272         foreach ($this->links as $k => $v){
273             
274             $cms_page = DB_DataObject::factory('cms_page');
275             
276             if(!$cms_page->get($k)){
277                 continue;
278             }
279             
280             $o = clone ($cms_page);
281             
282             $cms_page->setFrom(array(
283                 'parent_id' => (empty($v['parent_id'])) ? 0 : $this->mapping['pages'][$v['parent_id']],
284                 'menu_page_id' => (empty($v['menu_page_id'])) ? 0 : $this->mapping['pages'][$v['menu_page_id']],
285                 'translation_of_id' => (empty($v['translation_of_id'])) ? 0 : $this->mapping['pages'][$v['translation_of_id']],
286                 'to_replace_id' => (empty($v['to_replace_id'])) ? 0 : $this->mapping['pages'][$v['to_replace_id']],
287                 'category_page_id' => (empty($v['category_page_id'])) ? 0 : $this->mapping['pages'][$v['category_page_id']]
288             ));
289             
290             $cms_page->update($o);
291         }
292     }
293     
294     function processCategories()
295     {
296         if(empty($this->data['categories'])){
297             return;
298         }
299         
300         foreach ($this->data['categories'] as $k => $v){
301             
302             $cms_page_category = DB_DataObject::factory('cms_page_category');
303             $cms_page_category->setFrom(array(
304                 'page_id' => $this->mapping['pages'][$v['page_id']],
305                 'category_id' => $this->mapping['pages'][$v['category_id']]
306             ));
307             
308             if($cms_page_category->find(true)){
309                 return;
310             }
311             
312             $cms_page_category->insert();
313         }
314         
315     }
316     
317     function processImages()
318     {
319         if(empty($this->data['images'])){
320             return;
321         }
322         
323         foreach ($this->data['images'] as $k => $v){
324             
325             $images = DB_DataObject::factory('Images');
326             $images->setFrom($v);
327             
328             $images->setFrom(array(
329                 'id' => 0,
330                 'onid' => $this->mapping['pages'][$v['onid']]
331             ));
332             
333             $images->createFrom("{$this->tempDir}/{$v['filename']}", $v['filename']);
334             
335         }
336         
337     }
338     
339 }