1<?php
2/**
3 * @package Habari
4 *
5 */
6
7/**
8 * Habari AdminDashboardHandler Class
9 * Handles dashboard-related actions in the admin
10 *
11 */
12class AdminDashboardHandler extends AdminHandler
13{
14	/**
15	 * Handles get requests for the dashboard
16	 * @todo update check should probably be cron'd and cached, not re-checked every load
17	 */
18	public function get_dashboard()
19	{
20		// Not sure how best to determine this yet, maybe set an option on install, maybe do this:
21		$firstpostdate = DB::get_value( 'SELECT min(pubdate) FROM {posts} WHERE status = ?', array( Post::status( 'published' ) ) );
22		if ( $firstpostdate ) {
23			$this->theme->active_time = HabariDateTime::date_create( $firstpostdate );
24		}
25
26		// check to see if we have updates to display
27		$this->theme->updates = Options::get( 'updates_available', array() );
28
29		// collect all the stats we display on the dashboard
30		$user = User::identify();
31		$this->theme->stats = array(
32			'author_count' => Users::get( array( 'count' => 1 ) ),
33			'post_count' => Posts::get( array( 'count' => 1, 'content_type' => Post::type( 'any' ), 'status' => Post::status( 'published' ) ) ),
34			'comment_count' => Comments::count_total( Comment::STATUS_APPROVED, false ),
35			'tag_count' => Tags::vocabulary()->count_total(),
36			'user_draft_count' => Posts::get( array( 'count' => 1, 'content_type' => Post::type( 'any' ), 'status' => Post::status( 'draft' ), 'user_id' => $user->id ) ),
37			'unapproved_comment_count' => User::identify()->can( 'manage_all_comments' ) ? Comments::count_total( Comment::STATUS_UNAPPROVED, false ) : Comments::count_by_author( User::identify()->id, Comment::STATUS_UNAPPROVED ),
38			'spam_comment_count' => $user->can( 'manage_all_comments' ) ? Comments::count_total( Comment::STATUS_SPAM, false ) : Comments::count_by_author( $user->id, Comment::STATUS_SPAM ),
39			'user_scheduled_count' => Posts::get( array( 'count' => 1, 'content_type' => Post::type( 'any' ), 'status' => Post::status( 'scheduled' ), 'user_id' => $user->id ) ),
40		);
41
42		$this->fetch_dashboard_modules();
43
44		// check for first run
45		$u = User::identify();
46		if ( ! isset( $u->info->experience_level ) ) {
47			$this->theme->first_run = true;
48			$u->info->experience_level = 'user';
49			$u->info->commit();
50		}
51		else {
52			$this->theme->first_run = false;
53		}
54
55		$this->display( 'dashboard' );
56	}
57
58	/**
59	 * Handles POST requests from the dashboard.
60	 */
61	public function post_dashboard()
62	{
63		$this->get_dashboard();
64	}
65
66	/**
67	 * Fetches active modules for display on the dashboard
68	 */
69	public function fetch_dashboard_modules()
70	{
71		if ( count( Modules::get_all() ) == 0 ) {
72			$this->theme->modules = array();
73			return;
74		}
75
76		// get the active module list
77		$modules = Modules::get_active();
78
79		if ( User::identify()->can( 'manage_dash_modules' ) ) {
80			// append the 'Add Item' module
81			$modules['nosort'] = 'Add Item';
82
83			// register the 'Add Item' filter
84			Plugins::register( array( $this, 'filter_dash_module_add_item' ), 'filter', 'dash_module_add_item' );
85		}
86
87		foreach ( $modules as $id => $module_name ) {
88			$slug = Utils::slugify( (string) $module_name, '_' );
89			$module = array(
90				'name' => $module_name,
91				'title' => $module_name,
92				'content' => '',
93				'options' => ''
94				);
95
96			$module = Plugins::filter( 'dash_module_' .$slug, $module, $id, $this->theme );
97
98			$modules[$id] = $module;
99		}
100
101		$this->theme->modules = $modules;
102	}
103
104	/**
105	 * Handles AJAX requests from the dashboard
106	 */
107	public function ajax_dashboard( $handler_vars )
108	{
109		Utils::check_request_method( array( 'POST' ) );
110
111		$theme_dir = Plugins::filter( 'admin_theme_dir', Site::get_dir( 'admin_theme', true ) );
112		$this->theme = Themes::create( 'admin', 'RawPHPEngine', $theme_dir );
113
114		switch ( $handler_vars['action'] ) {
115			case 'updateModules':
116				$modules = array();
117				foreach ( $_POST as $key => $module ) {
118					// skip POST elements which are not module names
119					if ( preg_match( '/^module\d+$/', $key ) ) {
120						list( $module_id, $module_name ) = explode( ':', $module, 2 );
121						// remove non-sortable modules from the list
122						if ( $module_id != 'nosort' ) {
123							$modules[$module_id] = $module_name;
124						}
125					}
126				}
127
128				Modules::set_active( $modules );
129				$ar = new AjaxResponse( 200, _t( 'Modules updated.' ) );
130				break;
131			case 'addModule':
132				$id = Modules::add( $handler_vars['module_name'] );
133				$this->fetch_dashboard_modules();
134				$ar = new AjaxResponse( 200, _t( 'Added module %s.', array( $handler_vars['module_name'] ) ) );
135				$ar->html( 'modules', $this->theme->fetch( 'dashboard_modules' ) );
136				break;
137			case 'removeModule':
138				Modules::remove( $handler_vars['moduleid'] );
139				$this->fetch_dashboard_modules();
140				$ar = new AjaxResponse( 200, _t( 'Removed module.' ) );
141				$ar->html( 'modules', $this->theme->fetch( 'dashboard_modules' ) );
142				break;
143		}
144
145		$ar->out();
146	}
147
148	/**
149	 * Function used to set theme variables to the add module dashboard widget.
150	 * TODO make this form use an AJAX call instead of reloading the page
151	 */
152	public function filter_dash_module_add_item( $module, $id, $theme )
153	{
154		$modules = Modules::get_all();
155		if ( $modules ) {
156			$modules = array_combine( array_values( $modules ), array_values( $modules ) );
157		}
158
159		$form = new FormUI( 'dash_additem' );
160		$form->append( 'select', 'module', 'null:unused' );
161		$form->module->options = $modules;
162		$form->append( 'submit', 'submit', _t( '+' ) );
163		//$form->on_success( array( $this, 'dash_additem' ) );
164		$form->properties['onsubmit'] = "dashboard.add(); return false;";
165		$theme->additem_form = $form->get();
166
167		$module['content'] = $theme->fetch( 'dash_additem' );
168		$module['title'] = _t( 'Add Item' );
169		return $module;
170	}
171
172	/**
173	 * Adds a module to the user's dashboard
174	 * @param object form FormUI object
175	 */
176	public function dash_additem( $form )
177	{
178		$new_module = $form->module->value;
179		Modules::add( $new_module );
180
181		// return false to redisplay the form
182		return false;
183	}
184
185}
186