3 require_once 'Pman.php';
5 class Pman_Cms_Import_Pages extends Pman
8 static $cli_desc = "Imports a ZIP file exported from the CMS";
10 static $cli_opts = array(
12 'desc' => 'zip file directory',
24 var $mapping = array();
29 $ff = HTML_FlexyFramework::get();
31 if (empty($ff->cli)) {
32 $this->jerr('cli Only');
35 if(!$this->getAuthUser()){
42 function getAuthUser()
44 $ff = HTML_FlexyFramework::get();
46 $tbl = empty($ff->Pman['authTable']) ? 'core_person' : $ff->Pman['authTable'];
48 $this->authUser = DB_DataObject::factory($tbl);
50 $this->authUser->orderBy('id DESC');
52 if(!$this->authUser->find(true)){
59 function get($tbl, $opts = array())
61 PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, array($this, 'onPearError'));
63 if(empty($opts['file']) || !file_exists($opts['file'])){
64 $this->jerr('Please assign your zip file path [-f]');
67 // Pman/Hpasite/DummyPages/cms-page-dummy-page.zip
69 $this->zipFile = $opts['file'];
71 $this->check_environment();
73 $this->check_database();
75 if(!file_exists($this->zipFile)){
76 $this->jerr("Please upload the zip to {$this->zipFile}");
79 $this->zip = new ZipArchive();
81 if ($this->zip->open($this->zipFile) !== true) {
82 $this->jerr('Can not open zip file');
85 $this->tempDir = ini_get('session.save_path');
87 $this->zip->extractTo($this->tempDir);
91 $json = file_get_contents("{$this->tempDir}/data.json");
94 $this->jerr('Can not load the data.json');
97 $this->data = json_decode($json, true);
99 $this->processCmsPageType();
101 $this->processCmsTemplate();
103 $this->processCmsTemplateElement();
105 $this->processCmsPages();
107 $this->processPageLinks();
109 $this->processCategories();
111 $this->processImages();
118 function check_environment()
120 $ui = posix_getpwuid(posix_geteuid());
122 if(empty($ui['name']) || $ui['name'] != 'www-data'){
123 $this->jerr('Please use www-data');
126 if (!extension_loaded('zip')) { // sudo apt-get install php5.6-zip??
127 $this->jerr('Please install php zip extension');
131 function check_database()
133 $cms_page = DB_DataObject::factory('cms_page');
135 if($cms_page->count()){
136 $this->jerr('Please delete all cms pages before running this');
142 unlink("{$this->tempDir}/data.json");
144 if(empty($this->data['photos'])){
148 foreach ($this->data['photos'] as $k => $v){
149 unlink("{$this->tempDir}/{$v}");
154 function processCmsPageType()
156 $this->mapping['types'] = array();
158 if(empty($this->data['types'])){
159 $this->jerr('Invalid Json Data [types]');
162 foreach ($this->data['types'] as $k => $v){
164 $core_enum = DB_DataObject::factory('core_enum');
165 $core_enum->setFrom(array(
166 'etype' => $v['etype'],
171 if(!$core_enum->find(true)){
172 $this->jerr("Can not found type '{$v['name']}', type run UpdateDatabase");
175 $this->mapping['types'][$v['id']] = $core_enum->id;
180 function processCmsTemplate()
182 $this->mapping['templates'] = array();
184 if(empty($this->data['templates'])){
185 $this->jerr('Invalid Json Data [templates]');
188 foreach ($this->data['templates'] as $k => $v){
190 $cms_template = DB_DataObject::factory('cms_template');
191 $cms_template->setFrom(array(
192 'template' => $v['template']
195 if(!$cms_template->find(true)){
196 $this->jerr("Can not found template '{$v['template']}', type run UpdateDatabase");
199 $this->mapping['templates'][$v['id']] = $cms_template->id;
204 function processCmsTemplateElement()
206 $this->mapping['elements'] = array();
208 if(empty($this->data['elements'])){
210 // $this->jerr('Invalid Json Data [elements]');
213 foreach ($this->data['elements'] as $k => $v){
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']]
221 if(!$cms_template_element->find(true)){
222 $this->jerr("Can not found element'{$v['name']}', type run UpdateDatabase");
225 $this->mapping['elements'][$v['id']] = $cms_template_element->id;
230 function processCmsPages()
232 if(empty($this->data['pages'])){
233 $this->jerr('Invalid Json Data [pages]');
236 foreach ($this->data['pages'] as $k => $v){
238 $cms_page = DB_DataObject::factory('cms_page');
240 $cms_page->setFrom($v);
242 $cms_page->setFrom(array(
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']],
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
257 $this->mapping['pages'][$v['id']] = $cms_page->id;
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']
270 function processPageLinks()
272 foreach ($this->links as $k => $v){
274 $cms_page = DB_DataObject::factory('cms_page');
276 if(!$cms_page->get($k)){
280 $o = clone ($cms_page);
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']]
290 $cms_page->update($o);
294 function processCategories()
296 if(empty($this->data['categories'])){
300 foreach ($this->data['categories'] as $k => $v){
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']]
308 if($cms_page_category->find(true)){
312 $cms_page_category->insert();
317 function processImages()
319 if(empty($this->data['images'])){
323 foreach ($this->data['images'] as $k => $v){
325 $images = DB_DataObject::factory('Images');
326 $images->setFrom($v);
328 $images->setFrom(array(
330 'onid' => $this->mapping['pages'][$v['onid']]
333 $images->createFrom("{$this->tempDir}/{$v['filename']}", $v['filename']);