00001 <?php
00007 namespace Habari;
00008
00014 class AdminDashboardHandler extends AdminHandler
00015 {
00020 public function get_dashboard()
00021 {
00022
00023 $firstpostdate = DB::get_value( 'SELECT min(pubdate) FROM {posts} WHERE status = ?', array( Post::status( 'published' ) ) );
00024 if ( $firstpostdate ) {
00025 $this->theme->active_time = DateTime::create( $firstpostdate );
00026 }
00027
00028
00029 $this->theme->updates = Options::get( 'updates_available', array() );
00030
00031
00032 $user = User::identify();
00033 $this->theme->stats = array(
00034 'author_count' => Users::get( array( 'count' => 1 ) ),
00035 'post_count' => Posts::get( array( 'count' => 1, 'content_type' => Post::type( 'any' ), 'status' => Post::status( 'published' ) ) ),
00036 'comment_count' => Comments::count_total( 'approved', false ),
00037 'tag_count' => Tags::vocabulary()->count_total(),
00038 'user_draft_count' => Posts::get( array( 'count' => 1, 'content_type' => Post::type( 'any' ), 'status' => Post::status( 'draft' ), 'user_id' => $user->id ) ),
00039 'unapproved_comment_count' => User::identify()->can( 'manage_all_comments' ) ? Comments::count_total( 'unapproved', false ) : Comments::count_by_author( User::identify()->id, Comment::status('unapproved') ),
00040 'spam_comment_count' => $user->can( 'manage_all_comments' ) ? Comments::count_total( 'spam', false ) : Comments::count_by_author( $user->id, Comment::status('spam') ),
00041 'user_scheduled_count' => Posts::get( array( 'count' => 1, 'content_type' => Post::type( 'any' ), 'status' => Post::status( 'scheduled' ), 'user_id' => $user->id ) ),
00042 );
00043
00044
00045 $u = User::identify();
00046 if ( ! isset( $u->info->experience_level ) ) {
00047 $this->theme->first_run = true;
00048 $u->info->experience_level = 'user';
00049 $u->info->commit();
00050 }
00051 else {
00052 $this->theme->first_run = false;
00053 }
00054
00055 $this->get_additem_form();
00056
00057 $this->display( 'dashboard' );
00058 }
00059
00063 public function post_dashboard()
00064 {
00065 $this->get_dashboard();
00066 }
00067
00071 public function get_additem_form()
00072 {
00074 $additem_form = FormUI::create( 'dash_additem' )->set_properties(array('onsubmit' => 'dashboard.add(); return false;'));
00075 $additem_form->append( FormControlSelect::create('module')->set_options(Plugins::filter( 'dashboard_block_list', array() )) );
00076 $additem_form->append( FormControlSubmit::create('submit')->set_caption(_t('+')));
00077 $this->theme->additem_form = $additem_form->get();
00078 }
00079
00083 public function ajax_dashboard( $handler_vars )
00084 {
00085 Utils::check_request_method( array( 'POST' ) );
00086
00087 $this->create_theme();
00088 $this->get_additem_form();
00089 $available_modules = Plugins::filter('dashboard_block_list', array());
00090 $user_id = User::identify()->id;
00091 $dashboard_area = 'dashboard_' . $user_id;
00092
00093 switch ( $handler_vars['action'] ) {
00094 case 'updateModules':
00095 $modules = $_POST['moduleOrder'];
00096 $order = 0;
00097 foreach ( $modules as $module ) {
00098 $order++;
00099 DB::query('UPDATE {blocks_areas} SET display_order = :display_order WHERE block_id = :id AND area = :dashboardarea', array('display_order' => $order, 'id' => $module, 'dashboardarea' => $dashboard_area));
00100 }
00101 $ar = new AjaxResponse( 200, _t( 'Modules updated.' ) );
00102 break;
00103 case 'addModule':
00104 $type = $handler_vars['module_name'];
00105 $title = $available_modules[$type];
00106 $block = new Block( array( 'title' => $title, 'type' => $type ) );
00107 $block->insert();
00108 $max_display_order = DB::get_value('SELECT max(display_order) FROM {blocks_areas} WHERE area = :dashboardarea and scope_id = 0;', array('dashboardarea' => $dashboard_area));
00109 $max_display_order++;
00110 DB::query( 'INSERT INTO {blocks_areas} (block_id, area, scope_id, display_order) VALUES (:block_id, :dashboardarea, 0, :display_order)', array( 'block_id'=>$block->id, 'display_order'=>$max_display_order, 'dashboardarea' => $dashboard_area ) );
00111
00112 $ar = new AjaxResponse( 200, _t( 'Added module %s.', array( $title ) ) );
00113 $ar->html( 'modules', $this->theme->fetch( 'dashboard_modules' ) );
00114 break;
00115 case 'removeModule':
00116 $block_id = $handler_vars['moduleid'];
00117 DB::delete('{blocks}', array('id' => $block_id));
00118 DB::delete('{blocks_areas}', array('block_id' => $block_id));
00119 $ar = new AjaxResponse( 200, _t( 'Removed module.' ) );
00120 $ar->html( 'modules', $this->theme->fetch( 'dashboard_modules' ) );
00121 break;
00122 case 'configModule':
00123 $block_id = $handler_vars['moduleid'];
00124
00125 $block = DB::get_row('SELECT * FROM {blocks} b WHERE b.id = :id', array('id' => $block_id), 'Block');
00126
00128 $form = $block->get_form();
00129
00130
00131 $form->set_settings( array( 'success_message' => '<p class="form_message">' . _t('Module Configuration Saved.') . '</p>' . '<script type="text/javascript">window.setTimeout(function(){$(".form_message").fadeOut();}, 2000);</script>' ));
00132
00133 $form->append( FormControlHidden::create( 'moduleid', null, array( 'id' => 'moduleid' ) )->set_value( $block->id ) );
00134 $form->append( FormControlHidden::create( 'action', null, array( 'id' => 'action' ) )->set_value( 'configModule' ) );
00135
00136 $form->out();
00137 $form_id = $form->name;
00138 exit;
00139 break;
00140 }
00141
00142 $ar->out();
00143 }
00144 }