page->head_js[] = '/include/js/auto_width.js'; } /** * Show available themes and style variations * */ public function ShowAvailable(){ $cmd = \gp\tool::GetCommand(); switch($cmd){ case 'preview': case 'preview_iframe': case 'newlayout': case 'addlayout': if( $this->NewLayout($cmd) ){ return; } break; case 'DeleteTheme': $this->DeleteTheme(); $this->GetPossible(); break; } $this->GetAddonData(); $this->ShowHeader(); $this->AvailableList(); $this->InvalidFolders(); } public function AvailableList( $show_options = true ){ global $langmessage, $config; //search settings $this->searchPerPage = 10; $this->searchOrderOptions = array(); $this->searchOrderOptions['modified'] = $langmessage['Recently Updated']; $this->searchOrderOptions['rating_score'] = $langmessage['Highest Rated']; $this->searchOrderOptions['downloads'] = $langmessage['Most Downloaded']; $this->SearchOrder(); $this->SortAvailable(); // pagination $this->searchMax = count($this->avail_addons); $this->searchPage = \gp\special\Search::ReqPage('page', $this->searchMax ); $start = $this->searchPage * $this->searchPerPage; $possible = array_slice( $this->avail_addons, $start, $this->searchPerPage, true); if( $show_options ){ $this->SearchOptions(); echo '
'; } // show themes echo '
'; foreach($possible as $theme_id => $info){ $this->AvailableTheme($theme_id, $info, $show_options); } echo '
'; if( $show_options ){ $this->SearchNavLinks(); } } protected function AvailableTheme($theme_id, $info, $show_options ){ global $langmessage, $config; $theme_label = str_replace('_',' ',$info['name']); $version = ''; $id = false; if( isset($info['version']) ){ $version = $info['version']; } if( isset($info['addon_id']) && is_numeric($info['addon_id']) ){ $id = $info['addon_id']; } //screenshot if( file_exists($info['full_dir'].'/screenshot.png') ){ echo '
'; echo ''.$theme_label.' '.$version.''; echo '
'; }elseif( file_exists($info['full_dir'].'/screenshot.jpg') ){ echo '
'; echo ''.$theme_label.' '.$version.''; echo '
'; }else{ echo '
'; echo ''.$theme_label.' '.$version.''; echo '
'; } //options echo '
'; //colors if( $show_options ){ $color_q = 'cmd=preview'.$this->searchQuery; $color_a = ''; }else{ $color_q = 'cmd=preview_iframe'; $color_a = ' target="gp_layout_iframe" data-cmd="SetPreviewTheme" '; } echo ''.$langmessage['preview'].''; echo '
    '; foreach($info['colors'] as $color){ echo '
  • '; $theme = $theme_id.'/'.$color; $q = $color_q.'&theme='.rawurlencode($theme); $a = $color_a.' data-arg="'.htmlspecialchars($theme).'"'; echo \gp\tool::Link('Admin_Theme_Content/Available',str_replace('_',' ',$color),$q,$a); echo '
  • '; } echo '
'; $options = $this->AvailableThemeOptions($id, $info, $theme_label); if( !empty($options) ){ echo ''.$langmessage['options'].''; echo '
    '; echo $options; echo '
'; } echo '
'; //remote upgrade if( gp_remote_themes && $id && isset(\gp\admin\Tools::$new_versions[$id]) && version_compare(\gp\admin\Tools::$new_versions[$id]['version'], $version ,'>') ){ $version_info = \gp\admin\Tools::$new_versions[$id]; echo \gp\tool::Link('Admin_Theme_Content',$langmessage['new_version'],'cmd=RemoteInstall&id='.$id.'&name='.rawurlencode($version_info['name'])); } echo '
'; } protected function AvailableThemeOptions($id, $info, $theme_label){ global $langmessage, $config; ob_start(); if( $id ){ //more info echo '
  • '.self::DetailLink('theme', $id,'More Info...').'
  • '; //support $forum_id = 1000 + $id; echo '
  • '.$langmessage['Support'].'
  • '; //rating $rating = 0; if( $info['rt'] > 0 ){ $rating = $info['rt']; } echo '
  • '.$langmessage['rate'].' '.$this->ShowRating($info['rel'],$rating).'
  • '; //downloads if( $info['dn'] > 0 ){ echo '
  • Downloads: '.number_format($info['dn']).'
  • '; } } //last updated if( $info['tm'] > 0 ){ echo '
  • '.$langmessage['Modified'].': '; echo \gp\tool::date($langmessage['strftime_datetime'],$info['tm']); echo '
  • '; } if( $info['is_addon'] ){ //delete $folder = $info['folder']; $title = sprintf($langmessage['generic_delete_confirm'], $theme_label ); $attr = array( 'data-cmd'=>'cnreq','class'=>'gpconfirm','title'=> $title ); echo '
  • '.\gp\tool::Link('Admin_Theme_Content/Available',$langmessage['delete'],'cmd=DeleteTheme&folder='.rawurlencode($folder),$attr).'
  • '; //order if( isset($config['themes'][$folder]['order']) ){ echo '
  • Order: '.$config['themes'][$folder]['order'].'
  • '; } } return ob_get_clean(); } /** * Sort the list available addons * */ private function SortAvailable(){ // get addon information for ordering \gp\admin\Tools::VersionData($version_data); $version_data = $version_data['packages']; // combine remote addon information foreach($this->avail_addons as $theme_id => $info){ if( isset($info['id']) ){ $id = $info['id']; if( isset($version_data[$id]) ){ $info = array_merge($info,$version_data[$id]); $info['rt'] *= 5; } //use local rating if( isset($this->addonReviews[$id]) ){ $info['rt'] = $this->addonReviews[$id]['rating']; } }else{ $info['rt'] = 6; //give local themes a high rating to make them appear first, rating won't actually display } $info += array( 'dn'=>0, 'rt'=>0 ); //modified time if( !isset($info['tm']) ){ $info['tm'] = self::ModifiedTime( $info['full_dir'] ); } $this->avail_addons[$theme_id] = $info; } // sort by uasort( $this->avail_addons, array($this,'SortUpdated') ); switch($this->searchOrder){ case 'downloads': uasort( $this->avail_addons, array($this,'SortDownloads') ); break; case 'modified': uasort( $this->avail_addons, array($this,'SortRating') ); uasort( $this->avail_addons, array($this,'SortUpdated') ); break; case 'rating_score': default: uasort( $this->avail_addons, array($this,'SortRating') ); break; } } public static function ModifiedTime($directory){ $files = scandir( $directory ); $time = filemtime( $directory ); foreach($files as $file){ if( $file == '..' || $file == '.' ){ continue; } $full_path = $directory.'/'.$file; if( is_dir($full_path) ){ $time = max( $time, self::ModifiedTime( $full_path ) ); }else{ $time = max( $time, filemtime( $full_path ) ); } } return $time; } public function SortDownloads($a,$b){ return $b['dn'] > $a['dn']; } public function SortRating($a,$b){ return $b['rt'] > $a['rt']; } public function SortUpdated($a,$b){ return $b['tm'] > $a['tm']; } /** * Manage adding new layouts * */ public function NewLayout($cmd){ global $langmessage; //check the requested theme $theme =& $_REQUEST['theme']; $theme_info = $this->ThemeInfo($theme); if( $theme_info === false ){ message($langmessage['OOPS'].' (Invalid Theme)'); return false; } // three steps of installation switch($cmd){ case 'preview': if( $this->PreviewTheme($theme, $theme_info) ){ return true; } break; case 'preview_iframe': $this->PreviewThemeIframe($theme,$theme_info); return true; case 'newlayout': $this->NewLayoutPrompt($theme, $theme_info); return true; case 'addlayout': $this->AddLayout($theme_info); break; } return false; } /** * Preview a theme and give users the option of creating a new layout * */ public function PreviewTheme($theme, $theme_info){ global $langmessage,$config; $_REQUEST += array('gpreq' => 'body'); //force showing only the body as a complete html document $this->page->get_theme_css = false; $this->page->head_js[] = '/include/js/auto_width.js'; $this->page->head_js[] = '/include/js/theme_content_outer.js'; $this->page->css_admin[] = '/include/css/theme_content_outer.scss'; //show site in iframe echo '
    '; $url = \gp\tool::GetUrl('Admin_Theme_Content/Available','cmd=preview_iframe&theme='.rawurlencode($theme)); echo ''; echo '
    '; ob_start(); //new echo '
    '; echo '
    '; echo '
    '; echo \gp\tool::Link('Admin_Theme_Content/Available','« '.$langmessage['available_themes']); echo \gp\tool::Link('Admin_Theme_Content/Available',$langmessage['use_this_theme'],'cmd=newlayout&theme='.rawurlencode($theme),'data-cmd="gpabox" class="add_layout"'); echo '
    '; echo '
    '; echo '
    '; $this->searchUrl = 'Admin_Theme_Content/Available'; $this->AvailableList( false ); //search options $this->searchQuery .= '&cmd=preview&theme='.rawurlencode($theme); $this->SearchOptions( false ); echo '
    '; echo '
    '; echo '
    '; $this->page->admin_html = ob_get_clean(); return true; } public function PreviewThemeIframe($theme, $theme_info){ global $langmessage, $config; \gp\admin\Tools::$show_toolbar = false; $this->page->gpLayout = false; $this->page->theme_name = $theme_info['folder']; $this->page->theme_color = $theme_info['color']; $this->page->theme_dir = $theme_info['full_dir']; $this->page->theme_rel = $theme_info['rel'].'/'.$theme_info['color']; $this->LoremIpsum(); if( isset($theme_info['id']) ){ $this->page->theme_addon_id = $theme_info['id']; } $this->page->theme_path = \gp\tool::GetDir($this->page->theme_rel); $this->page->show_admin_content = false; } /** * Give users a few options before creating the new layout * */ public function NewLayoutPrompt($theme, $theme_info ){ global $langmessage; $label = substr($theme_info['name'].'/'.$theme_info['color'],0,25); echo '

    '.$langmessage['new_layout'].'

    '; echo '
    '; echo ''; echo ''; echo ''; echo ''; echo '
    '; echo $langmessage['options']; echo '
    '; echo $langmessage['label']; echo ''; echo ''; echo '
    '; echo $langmessage['make_default']; echo ''; echo ''; echo '
    '; echo '

    '; echo ' '; echo ' '; echo ' '; echo '

    '; echo '
    '; } /** * Add a new layout to the installation * */ public function AddLayout($theme_info){ global $gpLayouts, $langmessage, $config; $new_layout = array(); $new_layout['theme'] = $theme_info['folder'].'/'.$theme_info['color']; $new_layout['color'] = self::GetRandColor(); $new_layout['label'] = htmlspecialchars($_POST['label']); if( $theme_info['is_addon'] ){ $new_layout['is_addon'] = true; } $installer = new \gp\admin\Addon\Installer(); $installer->addon_folder_rel = dirname($theme_info['rel']); $installer->code_folder_name = '_themes'; $installer->source = $theme_info['full_dir']; $installer->new_layout = $new_layout; if( !empty($_POST['default']) && $_POST['default'] != 'false' ){ $installer->default_layout = true; } $success = $installer->Install(); $installer->OutputMessages(); if( $success && $installer->default_layout ){ $this->page->SetTheme(); $this->SetLayoutArray(); } } /** * Delete a remote theme * */ public function DeleteTheme(){ global $langmessage, $dataDir, $gpLayouts, $config; $config_before = $config; $gpLayoutsBefore = $gpLayouts; $theme_folder_name =& $_POST['folder']; if( empty($theme_folder_name) || !ctype_alnum($theme_folder_name) ){ message($langmessage['OOPS'].' (Invalid Request)'); return false; } $order = false; if( isset($config['themes'][$theme_folder_name]['order']) ){ $order = $config['themes'][$theme_folder_name]['order']; } if( !$this->CanDeleteTheme($theme_folder_name,$message) ){ message($message); return false; } //remove layouts $rm_addon = false; foreach($gpLayouts as $layout_id => $layout_info){ if( !isset($layout_info['is_addon']) || !$layout_info['is_addon'] ){ continue; } $layout_folder = dirname($layout_info['theme']); if( $layout_folder != $theme_folder_name ){ continue; } if( array_key_exists('addon_key',$layout_info) ){ $rm_addon = $layout_info['addon_key']; } $this->RmLayoutPrep($layout_id); unset($gpLayouts[$layout_id]); } //remove from settings unset($config['themes'][$theme_folder_name]); if( $rm_addon ){ $installer = new \gp\admin\Addon\Installer(); if( !$installer->Uninstall($rm_addon) ){ $gpLayouts = $gpLayoutsBefore; } $installer->OutputMessages(); }else{ if( !\gp\admin\Tools::SaveAllConfig() ){ $config = $config_before; $gpLayouts = $gpLayoutsBefore; message($langmessage['OOPS'].' (s1)'); return false; } message($langmessage['SAVED']); if( $order ){ $img_path = \gp\tool::IdUrl('ci'); \gp\tool::IdReq($img_path); } } //delete the folder if it hasn't already been deleted by addon installer $dir = $dataDir.'/data/_themes/'.$theme_folder_name; if( file_exists($dir) ){ \gp\tool\Files::RmAll($dir); } } public function CanDeleteTheme($folder,&$message){ global $gpLayouts, $config, $langmessage; foreach($gpLayouts as $layout_id => $layout){ if( !isset($layout['is_addon']) || !$layout['is_addon'] ){ continue; } $layout_folder = dirname($layout['theme']); if( $layout_folder == $folder ){ if( $config['gpLayout'] == $layout_id ){ $message = $langmessage['delete_default_layout']; return false; } } } return true; } }