[pLog-svn] r5107 - in plugins/branches/lifetype-1.2: . copyarticle copyarticle/class copyarticle/class/action copyarticle/imgs copyarticle/locale

oscar at devel.lifetype.net oscar at devel.lifetype.net
Thu Mar 15 19:40:27 EDT 2007


Author: oscar
Date: 2007-03-15 19:40:27 -0400 (Thu, 15 Mar 2007)
New Revision: 5107

Added:
   plugins/branches/lifetype-1.2/copyarticle/
   plugins/branches/lifetype-1.2/copyarticle/class/
   plugins/branches/lifetype-1.2/copyarticle/class/action/
   plugins/branches/lifetype-1.2/copyarticle/class/action/admincopyarticleaction.class.php
   plugins/branches/lifetype-1.2/copyarticle/imgs/
   plugins/branches/lifetype-1.2/copyarticle/imgs/copy.png
   plugins/branches/lifetype-1.2/copyarticle/locale/
   plugins/branches/lifetype-1.2/copyarticle/locale/locale_en_UK.php
   plugins/branches/lifetype-1.2/copyarticle/plugincopyarticle.class.php
Log:
A little plugin that will add an extra icon to copy an article to each row in the "edit posts" page. When copying the article, its counters are reset and the status is set back to 'draft'.
This plugin serves as a test of the new EVENT_PROCESS_BLOG_ADMIN_TEMPLATE_OUTPUT event, it wasn't too difficult after all to add the contents I wanted to the page via a couple of regular expressions...


Added: plugins/branches/lifetype-1.2/copyarticle/class/action/admincopyarticleaction.class.php
===================================================================
--- plugins/branches/lifetype-1.2/copyarticle/class/action/admincopyarticleaction.class.php	                        (rev 0)
+++ plugins/branches/lifetype-1.2/copyarticle/class/action/admincopyarticleaction.class.php	2007-03-15 23:40:27 UTC (rev 5107)
@@ -0,0 +1,65 @@
+<?php
+
+	lt_include( PLOG_CLASS_PATH."class/action/admin/adminaction.class.php" );
+    lt_include( PLOG_CLASS_PATH."class/view/admin/adminpostslistview.class.php" );	
+    lt_include( PLOG_CLASS_PATH."class/data/validator/integervalidator.class.php" );
+    lt_include( PLOG_CLASS_PATH."class/dao/articles.class.php" );
+
+	class AdminCopyArticleAction extends AdminAction
+	{
+		function AdminCopyArticleAction( $actionInfo, $request ) 
+		{
+			$this->AdminAction( $actionInfo, $request );
+			
+			$this->registerFieldValidator( "postId", new IntegerValidator());
+        	$view = new AdminPostsListView( $this->_blogInfo );
+        	$view->setErrorMessage( $this->_locale->tr("error_incorrect_article_id"));
+        	$this->setValidationErrorView( $view );			
+		}
+		
+		function perform()
+		{
+	        // fetch the post id that has already been validated
+	     	$postId = $this->_request->getValue( "postId" );   
+	        
+        	// fetch the post from the database
+            $posts = new Articles();
+            $post  = $posts->getBlogArticle( $postId, $this->_blogInfo->getId(), false );
+
+            // if the article does not exist, quit
+            if( !$post ) {
+            	$this->_view = new AdminPostsListView( $this->_blogInfo );
+                $this->_view->setErrorMessage( $this->_locale->tr("error_fetching_article" ));
+                $this->setCommonData();
+
+                return false;
+            }
+
+			// if the article was found:
+			// 		remove its current id
+			//		set the status to 'draft'
+			//		reset its counters (num reads, comment and trackback counters)
+			$post->setId( -1 );
+			$post->setStatus( POST_STATUS_DRAFT );
+			$post->setTotalComments( 0 );
+			$post->setTotalTrackbacks( 0 );
+			$post->setNumComments( 0 );
+			$post->setNumTrackbacks( 0 );			
+			$post->setNumReads( 0 );
+			
+			// and save it again
+			if( !$posts->addArticle( $post )) {
+            	$this->_view = new AdminPostsListView( $this->_blogInfo );
+                $this->_view->setErrorMessage( $this->_locale->tr("error_copying_article" ));
+                $this->setCommonData();
+
+                return( false );
+			}
+			
+        	$this->_view = new AdminPostsListView( $this->_blogInfo );
+            $this->_view->setSuccessMessage( $this->_locale->tr("article_copied_ok" ));
+            $this->setCommonData();
+			return( true );			
+		}		
+	}
+?>
\ No newline at end of file

Added: plugins/branches/lifetype-1.2/copyarticle/imgs/copy.png
===================================================================
(Binary files differ)


Property changes on: plugins/branches/lifetype-1.2/copyarticle/imgs/copy.png
___________________________________________________________________
Name: svn:mime-type
   + application/octet-stream

Added: plugins/branches/lifetype-1.2/copyarticle/locale/locale_en_UK.php
===================================================================
--- plugins/branches/lifetype-1.2/copyarticle/locale/locale_en_UK.php	                        (rev 0)
+++ plugins/branches/lifetype-1.2/copyarticle/locale/locale_en_UK.php	2007-03-15 23:40:27 UTC (rev 5107)
@@ -0,0 +1,4 @@
+<?php
+$messages['error_copying_article'] = 'There was an error copying the selected article';
+$messages['article_copied_ok'] = 'The article was copied successfully';
+?>
\ No newline at end of file

Added: plugins/branches/lifetype-1.2/copyarticle/plugincopyarticle.class.php
===================================================================
--- plugins/branches/lifetype-1.2/copyarticle/plugincopyarticle.class.php	                        (rev 0)
+++ plugins/branches/lifetype-1.2/copyarticle/plugincopyarticle.class.php	2007-03-15 23:40:27 UTC (rev 5107)
@@ -0,0 +1,56 @@
+<?php
+
+	lt_include( PLOG_CLASS_PATH."class/plugin/pluginbase.class.php" );
+	
+	class PluginCopyArticle extends PluginBase
+	{
+		function PluginCopyArticle( $source )
+		{
+			$this->PluginBase( $source );
+			
+			$this->id = "copyarticle";
+			$this->version = "20070315";
+			$this->author = "The LifeType Project";
+			$this->desc = "Provides extra functionality in the \"Edit Post\" screen to copy an article.";
+			
+			if( $source == "admin" ) {
+				// hook up to our event
+				$this->registerNotification( EVENT_PROCESS_BLOG_ADMIN_TEMPLATE_OUTPUT );
+				// and register a new admin action
+				$this->registerAdminAction( "copyArticle", "AdminCopyArticleAction" );
+			}
+		}
+		
+		function process( $event, $params )
+		{
+			// only process the "editposts" template
+			if( $params["template"] != "editposts" )
+				return;
+
+			// load the current contents of the template
+			$content = $params["content"];
+						
+			$regexp = "/<div class=\"list_action_button\">.*?<\/div>/s";
+			
+			preg_match_all( $regexp, $content, $matches, PREG_SET_ORDER );
+
+			//print_r($matches);			
+			
+			// now process each one of the matches and do the replacements...
+			foreach( $matches as $match ) {
+				// fetch the article id 
+				$res = preg_match( "/<a href=\"\?op=editPost&amp;postId=([0-9]+)\"/i", $match[0], $result );
+				if( $res ) {
+					// regenerate the block, adding the new markup
+					$artId = $result[1];
+					$toAdd = "<a href=\"?op=copyArticle&amp;postId={$artId}\"><img src=\"plugins/copyarticle/imgs/copy.png\" alt=\"copy\" /></a></div>";
+					$newStr = str_replace( "</div>", $toAdd, $match[0] );
+					// and now replace the old block with the new one in the global $content variable
+					$content = str_replace( $match[0], $newStr, $content );
+				}
+			}
+			
+			$params["content"] = $content;
+		}
+	}
+?>
\ No newline at end of file



More information about the pLog-svn mailing list