-<?php
-
-require_once 'Pman.php';
-
-class Pman_Cms_CreateDummyPages extends Pman
-{
-
- static $cli_desc = "Create Dummy Page SQL - Beta";
-
- static $cli_opts = array(
- 'file' => array(
- 'desc' => 'zip file directory',
- 'short' => 'f',
- 'default' => '',
- 'min' => 1,
- 'max' => 1,
- ),
- );
-
- var $cli = false;
-
- var $opts;
-
- var $mapping = array();
- var $links = array();
-
- function getAuth()
- {
- $ff = HTML_FlexyFramework::get();
-
- if (empty($ff->cli)) {
- $this->jerr('cli Only');
- }
-
- if(!$this->getAuthUser()){
- return false;
- }
-
- return true;
- }
-
- function getAuthUser()
- {
- $ff = HTML_FlexyFramework::get();
-
- $tbl = empty($ff->Pman['authTable']) ? 'core_person' : $ff->Pman['authTable'];
-
- $this->authUser = DB_DataObject::factory($tbl);
-
- $this->authUser->orderBy('id DESC');
-
- if(!$this->authUser->find(true)){
- return false;
- }
-
- return true;
- }
-
- function get($tbl, $opts)
- {
- PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, array($this, 'onPearError'));
-
- if(empty($opts['file']) || !file_exists($opts['file'])){
- $this->jerr('Please assign your zip file path [-f]');
- }
-
-// Pman/Hpasite/DummyPages/cms-page-dummy-page.zip
-
- $this->zipFile = $opts['file'];
-
- $this->check_environment();
-
- $this->check_database();
-
- if(!file_exists($this->zipFile)){
- $this->jerr("Please upload the zip to {$this->zipFile}");
- }
-
- $this->zip = new ZipArchive();
-
- if ($this->zip->open($this->zipFile) !== true) {
- $this->jerr('Can not open zip file');
- }
-
- $this->tempDir = ini_get('session.save_path');
-
- $this->zip->extractTo($this->tempDir);
-
- $this->zip->close();
-
- $json = file_get_contents("{$this->tempDir}/data.json");
-
- if($json === false){
- $this->jerr('Can not load the data.json');
- }
-
- $this->data = json_decode($json, true);
-
- $this->processCmsPageType();
-
- $this->processCmsTemplate();
-
- $this->processCmsTemplateElement();
-
- $this->processCmsPages();
-
- $this->processPageLinks();
-
- $this->processCategories();
-
- $this->processImages();
-
- $this->clean();
-
- $this->jok("OK");
- }
-
- function check_environment()
- {
- $ui = posix_getpwuid(posix_geteuid());
-
- if(empty($ui['name']) || $ui['name'] != 'www-data'){
- $this->jerr('Please use www-data');
- }
-
- if (!extension_loaded('zip')) { // sudo apt-get install php5.6-zip??
- $this->jerr('Please install php zip extension');
- }
- }
-
- function check_database()
- {
- $cms_page = DB_DataObject::factory('cms_page');
-
- if($cms_page->count()){
- $this->jerr('Please delete all cms pages before running this');
- }
- }
-
- function clean()
- {
- unlink("{$this->tempDir}/data.json");
-
- if(empty($this->data['photos'])){
- return;
- }
-
- foreach ($this->data['photos'] as $k => $v){
- unlink("{$this->tempDir}/{$v}");
- }
-
- }
-
- function processCmsPageType()
- {
- $this->mapping['types'] = array();
-
- if(empty($this->data['types'])){
- $this->jerr('Invalid Json Data [types]');
- }
-
- foreach ($this->data['types'] as $k => $v){
-
- $core_enum = DB_DataObject::factory('core_enum');
- $core_enum->setFrom(array(
- 'etype' => $v['etype'],
- 'active' => 1,
- 'name' => $v['name']
- ));
-
- if(!$core_enum->find(true)){
- $this->jerr("Can not found type '{$v['name']}', type run UpdateDatabase");
- }
-
- $this->mapping['types'][$v['id']] = $core_enum->id;
- }
-
- }
-
- function processCmsTemplate()
- {
- $this->mapping['templates'] = array();
-
- if(empty($this->data['templates'])){
- $this->jerr('Invalid Json Data [templates]');
- }
-
- foreach ($this->data['templates'] as $k => $v){
-
- $cms_template = DB_DataObject::factory('cms_template');
- $cms_template->setFrom(array(
- 'template' => $v['template']
- ));
-
- if(!$cms_template->find(true)){
- $this->jerr("Can not found template '{$v['template']}', type run UpdateDatabase");
- }
-
- $this->mapping['templates'][$v['id']] = $cms_template->id;
- }
-
- }
-
- function processCmsTemplateElement()
- {
- $this->mapping['elements'] = array();
-
- if(empty($this->data['elements'])){
- $this->jerr('Invalid Json Data [elements]');
- }
-
- foreach ($this->data['elements'] as $k => $v){
-
- $cms_template_element = DB_DataObject::factory('cms_template_element');
- $cms_template_element->setFrom(array(
- 'name' => $v['name'],
- 'template_id' => $this->mapping['templates'][$v['template_id']]
- ));
-
- if(!$cms_template_element->find(true)){
- $this->jerr("Can not found element'{$v['name']}', type run UpdateDatabase");
- }
-
- $this->mapping['elements'][$v['id']] = $cms_template_element->id;
- }
-
- }
-
- function processCmsPages()
- {
- if(empty($this->data['pages'])){
- $this->jerr('Invalid Json Data [pages]');
- }
-
- foreach ($this->data['pages'] as $k => $v){
-
- $cms_page = DB_DataObject::factory('cms_page');
-
- $cms_page->setFrom($v);
-
- $cms_page->setFrom(array(
- 'id' => 0,
- 'author_id' => $this->authUser->id,
- 'template_id' => (empty($v['template_id'])) ? 0 : $this->mapping['templates'][$v['template_id']],
- 'element_id' => (empty($v['element_id'])) ? 0 : $this->mapping['elements'][$v['element_id']],
- 'page_type_id' => (empty($v['page_type_id'])) ? 0 : $this->mapping['types'][$v['page_type_id']],
- 'parent_id' => 0,
- 'menu_page_id' => 0,
- 'translation_of_id' => 0,
- 'to_replace_id' => 0, // not sure the usage, but let's handle it as well
- 'category_page_id' => 0 // not sure the usage, but let's handle it as well
- ));
-
- $cms_page->insert();
-
- $this->mapping['pages'][$v['id']] = $cms_page->id;
-
- $this->links[$cms_page->id] = array(
- 'parent_id' => (empty($v['parent_id'])) ? 0 : $v['parent_id'],
- 'menu_page_id' => (empty($v['menu_page_id'])) ? 0 : $v['menu_page_id'],
- 'translation_of_id' => (empty($v['translation_of_id'])) ? 0 : $v['translation_of_id'],
- 'to_replace_id' => (empty($v['to_replace_id'])) ? 0 : $v['to_replace_id'],
- 'category_page_id' => (empty($v['category_page_id'])) ? 0 : $v['category_page_id']
- );
- }
-
- }
-
- function processPageLinks()
- {
- foreach ($this->links as $k => $v){
-
- $cms_page = DB_DataObject::factory('cms_page');
-
- if(!$cms_page->get($k)){
- continue;
- }
-
- $o = clone ($cms_page);
-
- $cms_page->setFrom(array(
- 'parent_id' => (empty($v['parent_id'])) ? 0 : $this->mapping['pages'][$v['parent_id']],
- 'menu_page_id' => (empty($v['menu_page_id'])) ? 0 : $this->mapping['pages'][$v['menu_page_id']],
- 'translation_of_id' => (empty($v['translation_of_id'])) ? 0 : $this->mapping['pages'][$v['translation_of_id']],
- 'to_replace_id' => (empty($v['to_replace_id'])) ? 0 : $this->mapping['pages'][$v['to_replace_id']],
- 'category_page_id' => (empty($v['category_page_id'])) ? 0 : $this->mapping['pages'][$v['category_page_id']]
- ));
-
- $cms_page->update($o);
- }
- }
-
- function processCategories()
- {
- if(empty($this->data['categories'])){
- return;
- }
-
- foreach ($this->data['categories'] as $k => $v){
-
- $cms_page_category = DB_DataObject::factory('cms_page_category');
- $cms_page_category->setFrom(array(
- 'page_id' => $this->mapping['pages'][$v['page_id']],
- 'category_id' => $this->mapping['pages'][$v['category_id']]
- ));
-
- if($cms_page_category->find(true)){
- return;
- }
-
- $cms_page_category->insert();
- }
-
- }
-
- function processImages()
- {
- if(empty($this->data['images'])){
- return;
- }
-
- foreach ($this->data['images'] as $k => $v){
-
- $images = DB_DataObject::factory('Images');
- $images->setFrom($v);
-
- $images->setFrom(array(
- 'id' => 0,
- 'onid' => $this->mapping['pages'][$v['onid']]
- ));
-
- $images->createFrom("{$this->tempDir}/{$v['filename']}", $v['filename']);
-
- }
-
- }
-
-}