• Main Page
  • Related Pages
  • Namespaces
  • Classes
  • Files
  • Examples
  • File List

system/handlers/adminthemeshandler.php

00001 <?php
00007 namespace Habari;
00008 
00014 class AdminThemesHandler extends AdminHandler
00015 {
00019   public function get_themes()
00020   {
00021     $all_themes = Themes::get_all_data();
00022     $theme_names = Utils::array_map_field($all_themes, 'name');
00023 
00024     $available_updates = Options::get( 'updates_available', array() );
00025 
00026     foreach ( $all_themes as $name => $theme ) {
00027 
00028       // only themes with a guid can be checked for updates
00029       if ( isset( $theme['info']->guid ) ) {
00030         if ( isset( $available_updates[ (string)$theme['info']->guid ] ) ) {
00031           // @todo this doesn't use the URL and is therefore worthless
00032           $all_themes[ $name ]['info']->update = $available_updates[ (string)$theme['info']->guid ]['latest_version'];
00033         }
00034       }
00035 
00036       // If this theme requires a parent to be present and it's not, send an error
00037       if(isset($theme['info']->parent) && !in_array((string)$theme['info']->parent, $theme_names)) {
00038         $all_themes[$name]['req_parent'] = $theme['info']->parent;
00039       }
00040     }
00041 
00042     $this->theme->all_themes = $all_themes;
00043 
00044     $this->theme->active_theme = Themes::get_active_data( true );
00045     $this->theme->active_theme_dir = $this->theme->active_theme['path'];
00046 
00047     // If the active theme is configurable, allow it to configure
00048     $this->theme->active_theme_name = $this->theme->active_theme['info']->name;
00049     $this->theme->configurable = Plugins::filter( 'theme_config', false, $this->active_theme );
00050     $this->theme->assign( 'configure', Controller::get_var( 'configure' ) );
00051 
00052     $this->theme->areas = $this->get_areas(0);
00053     $this->theme->previewed = Themes::get_theme_dir( false );
00054 
00055     $this->theme->help = isset($this->theme->active_theme['info']->help) ? $this->theme->active_theme['info']->help : false;
00056     $this->theme->help_active = Controller::get_var('help') == $this->theme->active_theme['dir'];
00057 
00058     $this->prepare_block_list();
00059 
00060     $blocks_areas_t = DB::get_results( 'SELECT b.*, ba.scope_id, ba.area, ba.display_order FROM {blocks} b INNER JOIN {blocks_areas} ba ON ba.block_id = b.id ORDER BY ba.scope_id ASC, ba.area ASC, ba.display_order ASC', array() );
00061     $blocks_areas = array();
00062     foreach ( $blocks_areas_t as $block ) {
00063       if ( !isset( $blocks_areas[$block->scope_id] ) ) {
00064         $blocks_areas[$block->scope_id] = array();
00065       }
00066       $blocks_areas[$block->scope_id][$block->area][$block->display_order] = $block;
00067     }
00068     $this->theme->blocks_areas = $blocks_areas;
00069 
00070     $scopes = DB::get_results( 'SELECT * FROM {scopes} ORDER BY name ASC;' );
00071     $scopes = Plugins::filter( 'get_scopes', $scopes );
00072     $this->theme->scopes = $scopes;
00073     $this->theme->scopeid = 0;
00074 
00075     $this->theme->theme_loader = Plugins::filter( 'theme_loader', '', $this->theme );
00076 
00077     $this->theme->display( 'themes' );
00078   }
00079 
00083   public function post_themes()
00084   {
00085     return $this->get_themes();
00086   }
00087 
00091   public function get_activate_theme()
00092   {
00093     $theme_name = $_GET['theme_name'];
00094     $theme_dir = $_GET['theme_dir'];
00095     $activated = false;
00096     if ( isset( $theme_name ) && isset( $theme_dir ) ) {
00097       $activated = Themes::activate_theme( $theme_name, $theme_dir );
00098     }
00099     if($activated) {
00100       Session::notice( _t( "Activated theme '%s'", array( $theme_name ) ) );
00101     }
00102     Utils::redirect( URL::get( 'display_themes' ) );
00103   }
00104 
00108   public function get_preview_theme()
00109   {
00110     $theme_name = $_GET['theme_name'];
00111     $theme_dir = $_GET['theme_dir'];
00112     if ( isset( $theme_name )  && isset( $theme_dir ) ) {
00113       if ( Themes::get_theme_dir() == $theme_dir ) {
00114         Themes::cancel_preview();
00115         Session::notice( _t( "Ended the preview of the theme '%s'", array( $theme_name ) ) );
00116       }
00117       else {
00118         if(Themes::preview_theme( $theme_name, $theme_dir )) {
00119           Session::notice( _t( "Previewing theme '%s'", array( $theme_name ) ) );
00120         }
00121       }
00122     }
00123     Utils::redirect( URL::get( 'display_themes' ) );
00124   }
00125 
00130   public function get_configure_block()
00131   {
00132     Utils::check_request_method( array( 'GET', 'POST' ) );
00133 
00134     $block = DB::get_row( 'SELECT b.* FROM {blocks} b WHERE id = :id ORDER BY b.title ASC', array( 'id' => $_GET['blockid'] ), 'Block' );
00136     $block_form = $block->get_form();
00137     // @todo This.  Is dumb.  Fix it.
00138     $block_form->set_properties( array('success_message' => '</div><div class="humanMsg" id="humanMsg" style="display: block;top: auto;bottom:-50px;"><div class="imsgs"><div id="msgid_2" class="msg" style="display: block; opacity: 0.8;"><p>' . _t( 'Saved block configuration.' ) . '</p></div></div></div>
00139 <script type="text/javascript">
00140     $("#humanMsg").animate({bottom: "5px"}, 500, function(){ window.setTimeout(function(){$("#humanMsg").animate({bottom: "-50px"}, 500)},3000) })
00141     parent.refresh_block_forms();
00142 </script>
00143 <div style="display:none;">
00144 '));
00145 
00146     $first_control = reset ( $block_form->controls );
00147     $block_admin = FormControlFieldset::create('block_admin')->set_caption(_t( 'Block Display Settings' ));
00148     if ( $first_control ) {
00149       $block_form->insert( $first_control, $block_admin );
00150     }
00151     else {
00152       $block_form->append( $block_admin );
00153     }
00154 
00155     $block_title_storage = new ControlStorage(
00156       function() use ($block) { return $block->title; },
00157       function($name, $value) use ($block) { $block->title = $value; }
00158     );
00159     $block_admin->append( FormControlLabel::wrap(_t( 'Block Title:' ), FormControlText::create('_title', $block_title_storage)->add_validator( 'validate_required' ) ));
00160     $block_admin->append( FormControlLabel::wrap(_t( 'Display Block Title:' ), FormControlCheckbox::create('_show_title', $block)));
00161     $block_form->append( FormControlSubmit::create('save')->set_caption(_t( 'Save' )) );
00162 
00163     $this->theme->content = $block_form->get();
00164 
00165     $this->display( 'block_configure' );
00166   }
00167 
00174   public function post_configure_block()
00175   {
00176     $this->get_configure_block();
00177   }
00178 
00184   public function ajax_add_block( $handler_vars )
00185   {
00186     Utils::check_request_method( array( 'POST' ) );
00187 
00188     $this->setup_admin_theme( '' );
00189 
00190     $title = $_POST['title'];
00191     $type = $_POST['type'];
00192 
00193     if ( !isset( $_POST['title'] ) ) {
00194       $this->theme->blocks = Plugins::filter( 'block_list', array() );
00195       $this->theme->block_instances = DB::get_results( 'SELECT b.* FROM {blocks} b ORDER BY b.title ASC', array(), 'Block' );
00196       $this->theme->active_theme = Themes::get_active_data( true );
00197 
00198       $this->display( 'block_instances' );
00199     }
00200     elseif ( $title == '' ) {
00201       $this->theme->blocks = Plugins::filter( 'block_list', array() );
00202       $this->theme->block_instances = DB::get_results( 'SELECT b.* FROM {blocks} b ORDER BY b.title ASC', array(), 'Block' );
00203       $this->theme->active_theme = Themes::get_active_data( true );
00204 
00205       $this->display( 'block_instances' );
00206 
00207       $msg = json_encode( _t( 'A new block must first have a name.' ) );
00208 
00209       echo '<script type="text/javascript">
00210         //alert(' . $msg . ');
00211         human_msg.display_msg(' . $msg . ');
00212       </script>';
00213     }
00214     else {
00215       $block = new Block( array( 'title' => $title, 'type' => $type ) );
00216       $block->insert();
00217 
00218       $this->prepare_block_list();
00219       $this->theme->active_theme = Themes::get_active_data( true );
00220 
00221       $this->display( 'block_instances' );
00222 
00223       $msg = json_encode( _t( 'Added new block "%1s" of type "%2s".', array( $title, $type ) ) );
00224 
00225       echo '<script type="text/javascript">
00226         human_msg.display_msg(' . $msg . ');
00227         spinner.stop();
00228       </script>';
00229     }
00230   }
00231 
00237   public function ajax_delete_block( $handler_vars )
00238   {
00239     Utils::check_request_method( array( 'POST' ) );
00240 
00241     $this->setup_admin_theme( '' );
00242 
00243     $block_id = $_POST['block_id'];
00244     $block = DB::get_row( 'SELECT b.* FROM {blocks} b WHERE id = :block_id', array( 'block_id' => $block_id ), 'Block' );
00245     if ( $block->delete() ) {
00246       $msg = json_encode( _t( 'Deleted block "%1s" of type "%2s".', array( $block->title, $block->type ) ) );
00247     }
00248     else {
00249       $msg = json_encode( _t( 'Failed to delete block "%1s" of type "%2s".', array( $block->title, $block->type ) ) );
00250     }
00251 
00252     $this->prepare_block_list();
00253 
00254     $this->theme->active_theme = Themes::get_active_data( true );
00255 
00256     $this->display( 'block_instances' );
00257 
00258     echo '<script type="text/javascript">
00259       human_msg.display_msg(' . $msg . ');
00260       spinner.stop();
00261       themeManage.change_scope();
00262     </script>';
00263   }
00264 
00271   public function ajax_save_areas( $handler_vars )
00272   {
00273     Utils::check_request_method( array( 'POST' ) );
00274 
00275     $scope = $_POST['scope'];
00276 
00277     $msg = '';
00278 
00279     $response = new AjaxResponse();
00280     if ( isset( $_POST['changed'] ) ) {
00281       // Empty area, regardless
00282       DB::query( 'DELETE FROM {blocks_areas} WHERE scope_id = :scope_id', array( 'scope_id' => $scope ) );
00283 
00284       // Repopulate area if we have data to repopulate with
00285       if ( isset( $_POST['area_blocks'] ) ) {
00286         $area_blocks = $_POST['area_blocks'];
00287 
00288         foreach ( (array)$area_blocks as $area => $blocks ) {
00289           $display_order = 0;
00290 
00291           // if there are no blocks for a given area, skip it
00292           if ( empty( $blocks ) ) {
00293             continue;
00294           }
00295 
00296           foreach ( $blocks as $block ) {
00297             $display_order++;
00298             DB::query( 'INSERT INTO {blocks_areas} (block_id, area, scope_id, display_order) VALUES (:block_id, :area, :scope_id, :display_order)', array( 'block_id'=>$block, 'area'=>$area, 'scope_id'=>$scope, 'display_order'=>$display_order ) );
00299           }
00300         }
00301       }
00302 
00303 //      $msg = json_encode( _t( 'Saved block areas settings.' ) );
00304 //      $msg = '<script type="text/javascript">
00305 //        human_msg.display_msg(' . $msg . ');
00306 //        spinner.stop();
00307 //      </script>';
00308       $response->message = _t( 'Saved block areas settings.' );
00309     }
00310 
00311     $this->setup_admin_theme( '' );
00312 
00313     $blocks_areas_t = DB::get_results( 'SELECT b.*, ba.scope_id, ba.area, ba.display_order FROM {blocks} b INNER JOIN {blocks_areas} ba ON ba.block_id = b.id ORDER BY ba.scope_id ASC, ba.area ASC, ba.display_order ASC', array() );
00314     $blocks_areas = array();
00315     foreach ( $blocks_areas_t as $block ) {
00316       if ( !isset( $blocks_areas[$block->scope_id] ) ) {
00317         $blocks_areas[$block->scope_id] = array();
00318       }
00319       $blocks_areas[$block->scope_id][$block->area][$block->display_order] = $block;
00320     }
00321     $this->theme->blocks_areas = $blocks_areas;
00322     $this->theme->scopeid = $scope;
00323     $this->theme->areas = $this->get_areas($scope);
00324     $scopes = DB::get_results( 'SELECT * FROM {scopes} ORDER BY name ASC;' );
00325     $scopes = Plugins::filter( 'get_scopes', $scopes );
00326     $this->theme->scopes = $scopes;
00327     $this->theme->active_theme = Themes::get_active_data( true );
00328 
00329     $output = $this->theme->fetch( 'block_areas' );
00330     $response->html('block_areas', $output);
00331 
00332     $response->out();
00333   }
00334 
00335 
00336   function get_areas($scope) {
00337     $activedata = Themes::get_active_data( true );
00338     $areas = array();
00339     if ( isset( $activedata['info']->areas->area ) ) {
00340       foreach ( $activedata['info']->areas->area as $area ) {
00341         $detail = array();
00342         if(isset($area['title'])) {
00343           $detail['title'] = (string)$area['title'];
00344         }
00345         else {
00346           $detail['title'] = (string)$area['name'];
00347         }
00348         $detail['description'] = (string)$area->description;
00349         $areas[(string)$area['name']] = $detail;
00350       }
00351     }
00352     $areas = Plugins::filter('areas', $areas, $scope);
00353     return $areas;
00354   }
00355 
00359   function prepare_block_list() {
00360     $block_types = Plugins::filter( 'block_list', array() );
00361     $dash_blocks = Plugins::filter( 'dashboard_block_list', array() );
00362     $block_types = array_diff_key($block_types, $dash_blocks);
00363     $all_block_instances = DB::get_results( 'SELECT b.* FROM {blocks} b ORDER BY b.title ASC', array(), 'Block' );
00364     $block_instances = array();
00365     $invalid_block_instances = array();
00366 
00367     // get dashboard block instances from plugins that may not be active
00368     $dash_blocks_instances = array();
00369     $scopes = $this->theme->get_scopes( 'dashboard' );
00370     foreach( $scopes as $scope ) {
00371       $dash_blocks_instances = array_merge( $dash_blocks, $this->theme->get_blocks( 'dashboard', $scope->id, $this->theme ) );
00372     }
00373     $dash_blocks_instances = array_merge( $dash_blocks_instances, $this->theme->get_blocks( 'dashboard', 0, $this->theme ) );
00374     foreach( $dash_blocks_instances as $dash_instance) {
00375       $dash_blocks[$dash_instance->type] = $dash_instance->type;
00376     }
00377 
00378     foreach($all_block_instances as $instance) {
00379       if(isset($block_types[$instance->type])) {
00380         $block_instances[] = $instance;
00381       }
00382       elseif(isset($dash_blocks[$instance->type])) {
00383         // Do not add this dashboard block to the block instance list on the theme page
00384       }
00385       else {
00386         $instance->invalid_message = _t('This data is for a block of type "%s", which is no longer provided by a theme or plugin.', array($instance->type));
00387         $invalid_block_instances[] = $instance;
00388       }
00389     }
00390     $this->theme->blocks = $block_types;
00391     $this->theme->block_instances = $block_instances;
00392     $this->theme->invalid_block_instances = $invalid_block_instances;
00393   }
00394 }
00395 ?>

Generated on Sun Aug 4 2013 12:51:43 for Habari by  doxygen 1.7.1