00001 <?php
00002
00003 namespace Habari;
00004
00005 if ( !defined( 'HABARI_PATH' ) ) { die( 'No direct access' ); }
00006
00017 class Undelete extends Plugin
00018 {
00024 public function action_plugin_activation( $file )
00025 {
00026 if ( realpath( $file ) == __FILE__ ) {
00027 Post::add_new_status( 'deleted', true );
00028 Options::set( 'undelete__style', '#primarycontent .deleted { background-color: #933; text-decoration: line-through; }' );
00029 }
00030 }
00031
00041 function filter_post_delete_allow( $result, $post )
00042 {
00043
00044
00045
00046
00047
00048
00049
00050 if ( $post->status != Post::status( 'deleted' ) && ACL::access_check( $post->get_access(), 'delete' ) && User::get_by_id( $post->user_id ) ) {
00051 $post->info->prior_status = $post->status;
00052 $post->status = Post::status( 'deleted' );
00053 $post->update();
00054 return false;
00055 }
00056 else {
00057 return true;
00058 }
00059 }
00060
00061 public function action_post_actions(FormControlDropbutton $actions, $post)
00062 {
00063 if ( $post->status == Post::status('deleted') && ACL::access_check( $post->get_access(), 'delete' ) ) {
00064 $actions->append(
00065 FormControlSubmit::create('restore')->set_caption(_t('Restore'))
00066 ->set_property('title', _t('Permanently delete this post'))
00067 ->set_url('javascript:unDelete.post('. $post->id . ');')
00068 );
00069 $actions->delete->set_caption('Delete Forever')->set_property('title', _t('Permanently delete this post'));
00070 }
00071 }
00072
00073 public function action_posts_manage_actions( FormControlDropbutton $post_actions )
00074 {
00075
00076 $require_any = array( 'own_posts' => 'delete' );
00077 $types = Post::list_active_post_types();
00078 foreach ($types as $key => $value ) {
00079 $require_any['post_' . $key] = 'delete';
00080 }
00081
00082 if ( User::identify()->can_any( $require_any ) ) {
00083 $post_actions->append(
00084 FormControlSubmit::create('restore')
00085 ->set_caption(_t('Restore Selected Entries'))
00086 ->set_properties(array(
00087 'onclick' => 'itemManage.update(\'restore\');return false;',
00088 'title' => _t('Restore Selected Entries'),
00089 ))
00090 );
00091 }
00092 }
00093
00094 public function filter_admin_entries_action( $status_msg, $action, $posts )
00095 {
00096 $num = 0;
00097
00098 switch ( $action ) {
00099 case 'restore':
00100 foreach( $posts as $post ) {
00101 $result = $this->undelete_post( $post->id );
00102 if ( $result ) {
00103 $num++;
00104 }
00105 }
00106 if ( $num == count( $posts ) ) {
00107 $status_msg = sprintf( _n('Restored %d post', 'Restored %d posts', $num ), $num );
00108 }
00109 else {
00110 $status_msg = _t( 'You did not have permission to restore some entries.' );
00111 }
00112 break;
00113 }
00114
00115 return $status_msg;
00116 }
00117
00123 private function undelete_post( $post_id )
00124 {
00125 $post = Post::get( array( 'id' => $post_id, 'status' => Post::status('any') ) );
00126 if ( $post->status == Post::status('deleted') && ACL::access_check( $post->get_access(), 'delete' ) ) {
00127 $post->status = $post->info->prior_status ? $post->info->prior_status : Post::status( 'draft' );
00128 unset( $post->info->prior_status );
00129 $post->update();
00130
00131 EventLog::log(
00132 _t('Post %1$s (%2$s) restored.', array( $post->id, $post->slug ) ),
00133 'info', 'content', 'habari'
00134 );
00135
00136 if ( $post->status == Post::status( 'scheduled' ) ) {
00137 Posts::update_scheduled_posts_cronjob();
00138 }
00139 return true;
00140 }
00141 else {
00142 return false;
00143 }
00144 }
00145
00146 public function action_auth_ajax_undelete()
00147 {
00148 if ( $this->undelete_post($_POST['id']) ) {
00149 _e('Restored post %d', array($_POST['id']) );
00150 }
00151 else {
00152 _e( 'Could not restore post %d', array($_POST['id']) );
00153 }
00154 }
00155
00156 private function get_perms()
00157 {
00158 $type_perms = array();
00159 $types = Post::list_active_post_types();
00160 foreach( $types as $key => $value ) {
00161 $perm = array( 'post_' . $key => ACL::get_bitmask( 'delete' ) );
00162 $types_perms = array_merge( $type_perms, $perm );
00163 }
00164 $perms = array( 'own_posts' => ACL::get_bitmask( 'delete' ), 'post_any' => ACL::get_bitmask( 'delete' ) );
00165 $perms = array_merge( $perms, $type_perms );
00166 return $perms;
00167 }
00168
00169 public function filter_plugin_config( $actions, $plugin_id )
00170 {
00171 if ( $plugin_id == $this->plugin_id() ) {
00172 $actions[]= _t( 'Configure' );
00173 if ( User::identify()->can_any( $this->get_perms() ) ) {
00174 $actions[]= _t( 'Empty Trash' );
00175 }
00176 }
00177 return $actions;
00178 }
00179
00180 public function action_plugin_ui( $plugin_id, $action )
00181 {
00182 if ( $plugin_id == $this->plugin_id() ) {
00183 switch ( $action ) {
00184 case _t( 'Configure' ):
00185 $ui = new FormUI( strtolower( get_class( $this ) ) );
00186 $ui->append( 'textarea', 'style', 'option:undelete__style', _t( 'Style declaration for deleted content:' ) );
00187 $ui->append( 'submit', 'save', _t( 'Save' ) );
00188 $ui->on_success( array( $this, 'updated_config' ) );
00189 $ui->out();
00190 break;
00191
00192 case _t( 'Empty Trash' ):
00193 $ui = new FormUI( strtolower( get_class( $this ) ) );
00194 $ui->append( 'static', 'explanation', _t('Pressing this button will permanently delete all posts from the virtual trash can. You cannot undo this.') );
00195 $ui->append( 'submit', 'delete', _t( 'Delete All' ) );
00196 $ui->on_success( array( $this, 'deleted_all' ) );
00197 $ui->out();
00198 break;
00199 }
00200
00201 }
00202 }
00203
00204 public function updated_config( $ui )
00205 {
00206 $ui->save();
00207 return false;
00208 }
00209
00210 public function deleted_all( $ui )
00211 {
00212 $count = self::delete_all();
00213
00214 Session::notice( _t( 'Permanently deleted %d posts', array( $count ) ) );
00215 return false;
00216 }
00217
00218
00219 private function delete_all()
00220 {
00221 $posts = Posts::get(array('status' => Post::status('deleted'), 'nolimit' => true));
00222
00223 $count = 0;
00224
00225 foreach($posts as $post) {
00226 if ( ACL::access_check( $post->get_access(), 'delete' ) ) {
00227 $post->delete();
00228 $count++;
00229 }
00230 }
00231
00232 return $count;
00233 }
00234
00240 public function action_template_header()
00241 {
00242
00243 if ( User::identify()->loggedin ) {
00244 echo '<style type="text/css">';
00245 Options::out( 'undelete__style' );
00246 echo '</style>';
00247 }
00248 }
00249
00250 public function action_admin_header( $theme )
00251 {
00252 if ( $theme->page == 'posts' ) {
00253 Stack::add( 'admin_stylesheet', array($this->get_url() . '/undelete.css', 'screen') );
00254 $url = URL::get( 'auth_ajax', array('context' => 'undelete') );
00255 $script = <<<JS
00256 var unDelete = {
00257 post : function(id) {
00258 spinner.start();
00259 \$.post(
00260 '$url',
00261 {'id':id},
00262 function( result ) {
00263 spinner.stop();
00264 human_msg.display_msg( result );
00265 if ( $('.timeline').length ) {
00266 var loupeInfo = timeline.getLoupeInfo();
00267 itemManage.fetch( 0, loupeInfo.limit, true );
00268 timeline.updateLoupeInfo();
00269 }
00270 else {
00271 itemManage.fetch( 0, 20, false );
00272 }
00273 }
00274 );
00275 }
00276 }
00277 JS;
00278 Stack::add( 'admin_header_javascript', $script, 'undelete', 'admin-js' );
00279 }
00280 }
00281 }
00282
00283 ?>