[pLog-svn] r5612 - in plog/branches/lifetype-1.3-ajax/class: action action/admin controller dao data data/forms data/pager data/serialize/json data/validator database view view/admin view/admin/ajax view/ajax

oscar at devel.lifetype.net oscar at devel.lifetype.net
Wed Jul 4 17:21:33 EDT 2007


Author: oscar
Date: 2007-07-04 17:21:32 -0400 (Wed, 04 Jul 2007)
New Revision: 5612

Added:
   plog/branches/lifetype-1.3-ajax/class/view/ajax/
   plog/branches/lifetype-1.3-ajax/class/view/ajax/ajaxview.class.php
Modified:
   plog/branches/lifetype-1.3-ajax/class/action/action.class.php
   plog/branches/lifetype-1.3-ajax/class/action/admin/adminaction.class.php
   plog/branches/lifetype-1.3-ajax/class/action/admin/adminaddarticlecategoryaction.class.php
   plog/branches/lifetype-1.3-ajax/class/action/admin/adminaddlinkcategoryaction.class.php
   plog/branches/lifetype-1.3-ajax/class/action/admin/adminloginaction.class.php
   plog/branches/lifetype-1.3-ajax/class/controller/controller.class.php
   plog/branches/lifetype-1.3-ajax/class/dao/userinfo.class.php
   plog/branches/lifetype-1.3-ajax/class/data/forms/formvalidator.class.php
   plog/branches/lifetype-1.3-ajax/class/data/pager/pager.class.php
   plog/branches/lifetype-1.3-ajax/class/data/serialize/json/jsonserializer.class.php
   plog/branches/lifetype-1.3-ajax/class/data/timestamp.class.php
   plog/branches/lifetype-1.3-ajax/class/data/validator/validation.class.php
   plog/branches/lifetype-1.3-ajax/class/database/dbobject.class.php
   plog/branches/lifetype-1.3-ajax/class/view/admin/admindefaultview.class.php
   plog/branches/lifetype-1.3-ajax/class/view/admin/adminlinkcategorieslistview.class.php
   plog/branches/lifetype-1.3-ajax/class/view/admin/admintemplatedview.class.php
   plog/branches/lifetype-1.3-ajax/class/view/admin/ajax/adminajaxview.class.php
   plog/branches/lifetype-1.3-ajax/class/view/view.class.php
Log:
I'm just checking in all the [incomplete] work done so far to better integrate Ajax in the core.

What I'm trying to achieve here is that Ajax features should be as easy as possible to implement avoiding code duplication. For example if you look at AdminAddArticleCategoryAction and AdminAddArticleCategoryAjaxAction, they do exactly the same with the only difference that one of them returns a Smarty view and the other one returns some Ajax/json content. Why can't we use the same action class for both? 

Right now, something like this is already working:

http://.../admin.php?op=addArticleCategory&categoryName=newcategory1&categoryDescription=description --> normal HTML view
http://.../admin.php?op=addArticleCategory&categoryName=newcategory1&categoryDescription=description&output=json --> Json view

The goal is that by appending "output=json" to a URL, and as long as the action class implements certain methods, the same logic can be used to return HTML content orjson content. This is now working quite nicely and a couple of action classes have already been modified to support this (changes to the code to support this are needed, but fortunately they're minor changes) So far addLinkCategory and addArticleCategory are working as examples, I will tackle more complicated action classes right away.

These changes don't mean that we're going to start implementing Ajax features in 1.3 like there's no tomorrow but instead, if and when we choose to implement these features, we will have a solid foundation. This is just the easy part anyway, I think adapting/rethinking the user interface to support this is going to be more complicated than this... And in any case, this is now living in a separate branch to see how it evolves so it may or may not end up being part of the final 1.3 release.

Of course ideas and comments are welcome.



Modified: plog/branches/lifetype-1.3-ajax/class/action/action.class.php
===================================================================
--- plog/branches/lifetype-1.3-ajax/class/action/action.class.php	2007-07-04 20:31:21 UTC (rev 5611)
+++ plog/branches/lifetype-1.3-ajax/class/action/action.class.php	2007-07-04 21:21:32 UTC (rev 5612)
@@ -128,6 +128,19 @@
         {	
 			// to be implemented by child classes...
         }
+
+		/**
+		 * Use this method to provide Ajax specific behaviour for your action class. What you would usually do
+		 * is isolate your application logic in a class method and then have Action::perform() and
+		 * Action::performAjax() call the same logic, and then the former method would generate a
+		 * view that returns HTML code to the client and the latter would generate one that returns
+		 * Ajax/Json code to the client.
+		 */
+		function performAjax()
+		{
+			$this->_view = $this->getAjaxErrorView();
+			$this->_view->setErrorMessage( "Output not supported" );
+		}
         
         /**
          * This method can be used for data validation and is <b>always</b> executed
@@ -157,28 +170,55 @@
 			
 			return $validationOk;
 		}
+
+		/**
+		 * When validation errors occur and the expected output is ajax, this method
+		 * will return a suitable view for an ajax client. It can be overridden in child
+		 * classes to provide more specific Ajax behaviour in case the default one is not
+		 * enough
+		 *
+		 * @return An AjaxView object
+		 */
+		function getAjaxErrorView()
+		{
+			lt_include( PLOG_CLASS_PATH."class/view/ajax/ajaxview.class.php" );			
+			return( new AjaxView());
+		}
 		
 		/**
 		 * This method will be called when a validation error happens. Child classes are
 		 * free to extend or reimplement this one and can be used as some sort of a trigger
 		 * in order to do some cleanup if needed.
 		 *
-		 *Ê@return nothing
+		 * @return nothing
 		 */
 		function validationErrorProcessing()
 		{
-			// if there was a validaton error, then inform the view
-			$this->_view = $this->_validationErrorView;
-			$this->_view->setError( true );
+			$this->_form->setFormIsValid( false );
+			
+			if( $this->_request->getValue( "output", "html" ) == "json" ) {
+				// if the output happens to be 'json'/ajax, then we can do some lighter processing
+				// all we need to output whenever there is a validation error when using ajax
+				// is the error message adn the FormValidator object containing information about
+				// the fileds that validated/did not validate
+				$this->_view = $this->getAjaxErrorView();
+				$this->_view->setSuccess( false );
+				$this->_view->setErrorMessage( $this->_validationErrorView->getErrorMessage());
+				$this->_view->setValue( "form", $this->_form );
+			}
+			else {
+				// this is the normal processing for html views
+				$this->_view = $this->_validationErrorView;
+				$this->_view->setError( true );
 				
-			// and  export all the data to the view so that it can be reused in the error view
-			$fieldValues = $this->_form->getFieldValues();
-			foreach( $fieldValues as $fieldName => $fieldValue ) {
-				$this->_view->setValue( "$fieldName", $fieldValue );
+				// and  export all the data to the view so that it can be reused in the error view
+				$fieldValues = $this->_form->getFieldValues();
+				foreach( $fieldValues as $fieldName => $fieldValue ) {
+					$this->_view->setValue( "$fieldName", $fieldValue );
+				}				
+				
+				$this->setCommonData();				
 			}
-				
-			$this->_form->setFormIsValid( false );
-			$this->setCommonData();
 	
 			return true;
 		}

Modified: plog/branches/lifetype-1.3-ajax/class/action/admin/adminaction.class.php
===================================================================
--- plog/branches/lifetype-1.3-ajax/class/action/admin/adminaction.class.php	2007-07-04 20:31:21 UTC (rev 5611)
+++ plog/branches/lifetype-1.3-ajax/class/action/admin/adminaction.class.php	2007-07-04 21:21:32 UTC (rev 5612)
@@ -321,5 +321,14 @@
 			
 			return 0;
 		}
+		
+		/**
+		 * @see Action::getAjaxErrorView()
+		 */
+		function getAjaxErrorView()
+		{
+			lt_include( PLOG_CLASS_PATH."class/view/admin/ajax/adminajaxview.class.php" );			
+			return( new AdminAjaxView( $this->_blogInfo ));
+		}		
     }
 ?>
\ No newline at end of file

Modified: plog/branches/lifetype-1.3-ajax/class/action/admin/adminaddarticlecategoryaction.class.php
===================================================================
--- plog/branches/lifetype-1.3-ajax/class/action/admin/adminaddarticlecategoryaction.class.php	2007-07-04 20:31:21 UTC (rev 5611)
+++ plog/branches/lifetype-1.3-ajax/class/action/admin/adminaddarticlecategoryaction.class.php	2007-07-04 21:21:32 UTC (rev 5612)
@@ -21,6 +21,7 @@
         var $_categoryUrl;
 		var $_properties;
 		var $_categoryDescription;
+		var $_message;
 
     	/**
          * Constructor. If nothing else, it also has to call the constructor of the parent
@@ -43,33 +44,47 @@
 
         }
 
-        /**
-         * Carries out the specified action
-         */
-        function perform()
-        {
+		function addArticleCategory()
+		{
 			// fetch the data, we already know it's valid and that we can trust it!
         	$this->_categoryName     = Textfilter::filterAllHTML($this->_request->getValue( "categoryName" ));
             $this->_categoryUrl      = $this->_request->getValue( "categoryUrl" );
             $this->_categoryInMainPage = Textfilter::checkboxToBoolean($this->_request->getValue( "categoryInMainPage" ));
 			$this->_categoryDescription = Textfilter::filterAllHTML($this->_request->getValue( "categoryDescription" ));
-			$this->_properties = $this->_request->getValue( "properties" );		
-		
+			
 			// create the object...
             $categories = new ArticleCategories();
             $category   = new ArticleCategory( $this->_categoryName,
                                                $this->_categoryUrl,
                                                $this->_blogInfo->getId(),
                                                $this->_categoryInMainPage,
-											   $this->_categoryDescription,
-											   0,
-											   $this->_properties );
+											   $this->_categoryDescription );
 											   
 			// fire the pre event...
-			$this->notifyEvent( EVENT_PRE_CATEGORY_ADD, Array( "category" => &$category ));
+			$this->notifyEvent( EVENT_PRE_CATEGORY_ADD, Array( "category" => &$category ));			
+			
+            if( $categories->addArticleCategory( $category )) {
+				// fire the post event
+				$this->notifyEvent( EVENT_POST_CATEGORY_ADD, Array( "category" => &$category ));	
+				// clear the cache if everything went fine
+				CacheControl::resetBlogCache( $this->_blogInfo->getId(), false );				
+				// define the success message
+				$this->_message = $this->_locale->pr("category_added_ok", $category->getName());
+			}
+			else {
+				$this->_locale->tr( "error_adding_article_category" );
+			}
+			
+			return( $category );
+		}
 
+        /**
+         * Carries out the specified action
+         */
+        function perform()
+        {				
             // once we have built the object, we can add it to the database!
-            if( $categories->addArticleCategory( $category )) {
+            if( $this->addArticleCategory()) {
 				// if everything went fine, transfer the execution flow to the action that
 				// lists all the article categories... without forgetting that we should let the
 				// next class know that we actually added a category alongside a message
@@ -80,26 +95,37 @@
 					$this->_view = new AdminTemplatedView( $this->_blogInfo, "newpostcategory" );
 					
 				$this->_view->setSuccess( true );
-				$this->_view->setSuccessMessage( $this->_locale->pr("category_added_ok", $category->getName()));
-				
-				// fire the post event
-				$this->notifyEvent( EVENT_POST_CATEGORY_ADD, Array( "category" => &$category ));
-				
-				// clear the cache if everything went fine
-				CacheControl::resetBlogCache( $this->_blogInfo->getId(), false );														
-				
+				$this->_view->setSuccessMessage( $this->_message );
 				$this->setCommonData();				
             }
             else {
 				// if there was an error, we should say so... as well as not changing the view since
 				// we're going back to the original view where we can add the category
 				$this->_view->setError( true );
-				$this->_view->setErrorMessage( $this->_locale->tr("error_adding_article_category" ));
+				$this->_view->setErrorMessage( $this->_message );
 				$this->setCommonData( true );
             }
 
             // better to return true if everything fine
             return true;
         }
+
+		/**
+		 * Ajax-specific behaviour
+		 */
+		function performAjax()
+		{
+			lt_include( PLOG_CLASS_PATH."class/view/admin/ajax/adminajaxview.class.php" );			
+			$this->_view = new AdminAjaxView( $this->_blogInfo );
+			if( $category = $this->addArticleCategory()) {
+				$this->_view->setSuccess( true );
+				$this->_view->setSuccessMessage( $this->_message );
+				$this->_view->setValue( "category", $category );
+			}
+			else {
+				$this->_view->setSuccess( true );
+				$this->_view->setSuccessMessage( $this->_message );				
+			}
+		}
     }
 ?>
\ No newline at end of file

Modified: plog/branches/lifetype-1.3-ajax/class/action/admin/adminaddlinkcategoryaction.class.php
===================================================================
--- plog/branches/lifetype-1.3-ajax/class/action/admin/adminaddlinkcategoryaction.class.php	2007-07-04 20:31:21 UTC (rev 5611)
+++ plog/branches/lifetype-1.3-ajax/class/action/admin/adminaddlinkcategoryaction.class.php	2007-07-04 21:21:32 UTC (rev 5612)
@@ -32,38 +32,72 @@
 			
 			$this->requirePermission( "add_link_category" );
         }
+
+		function addLinkCategory()
+		{
+        	// add the new link category to the database
+			$this->_linkCategoryName = Textfilter::filterAllHTML($this->_request->getValue( "linkCategoryName" ));
+            $mylinksCategories = new MyLinksCategories();
+            $this->_mylinksCategory = new MyLinksCategory( $this->_linkCategoryName, 
+			                                        $this->_blogInfo->getId(), 
+													0, 
+													$this->_properties );			
+													
+			$result = $mylinksCategories->addMyLinksCategory( $this->_mylinksCategory, $this->_blogInfo->getId());
+			
+			if( $result ) {
+				// clear the cache				
+				CacheControl::resetBlogCache( $this->_blogInfo->getId(), false );
+				// and set a message
+				$this->_message = $this->_locale->pr("link_category_added_ok", $this->_mylinksCategory->getName());
+			}
+			else {
+				$this->_message = $this->_locale->tr("error_adding_link_category");
+			}
+			
+			return( $result );
+		}
         
         /**
          * Carries out the specified action
          */
         function perform()
         {
-        	// add the new link category to the database
-			$this->_linkCategoryName = Textfilter::filterAllHTML($this->_request->getValue( "linkCategoryName" ));
-            $mylinksCategories = new MyLinksCategories();
-            $mylinksCategory = new MyLinksCategory( $this->_linkCategoryName, 
-			                                        $this->_blogInfo->getId(), 
-													0, 
-													$this->_properties );
+
 			// the view is the same for both conditions
 			if( $this->userHasPermission( "view_link_categories" ))
            		$this->_view = new AdminLinkCategoriesListView( $this->_blogInfo );													
 			else
 				$this->_view = new AdminTemplatedView( $this->_blogInfo, "newlinkcategory" );
+				
+			$result = $this->addLinkCategory();
 													
-            if( !$mylinksCategories->addMyLinksCategory( $mylinksCategory, $this->_blogInfo->getId())) {
+            if( !$result ) {
 				// set an error message
-                $this->_view->setErrorMessage( $this->_locale->tr("error_adding_link_category"));
+                $this->_view->setErrorMessage( $this->_message);
             }
 			else {
-				// clear the cache
-				CacheControl::resetBlogCache( $this->_blogInfo->getId(), false );
-				$this->_view->setSuccessMessage( $this->_locale->pr("link_category_added_ok", $mylinksCategory->getName()));	
-			}
+				// or success
+				$this->_view->setSuccessMessage( $this->_message );	
+			}			
 			
             $this->setCommonData();
 
             return true;
         }
+
+		function performAjax()
+		{
+			$result = $this->addLinkCategory();
+			
+			lt_include( PLOG_CLASS_PATH."class/view/admin/ajax/adminajaxview.class.php" );
+			$this->_view = new AdminAjaxView( $this->_blogInfo, "blah blah" );
+			
+			$this->_view->setSuccess( $result );
+			$this->_view->setMessage( $this->_message );
+			$this->_view->setValue( "linkcategory", $this->_mylinksCategory );
+			
+			return( true );
+		}
     }
 ?>

Modified: plog/branches/lifetype-1.3-ajax/class/action/admin/adminloginaction.class.php
===================================================================
--- plog/branches/lifetype-1.3-ajax/class/action/admin/adminloginaction.class.php	2007-07-04 20:31:21 UTC (rev 5611)
+++ plog/branches/lifetype-1.3-ajax/class/action/admin/adminloginaction.class.php	2007-07-04 21:21:32 UTC (rev 5612)
@@ -47,6 +47,11 @@
 			$this->setValidationErrorView( $view );
         }
 
+		function authenticateUser()
+		{
+			
+		}
+
         /**
          * Carries out the specified action
          */
@@ -55,7 +60,6 @@
         	// get the parameters, which have already been validated
             $this->_userName     = Textfilter::filterAllHTML($this->_request->getValue( "userName" ));
             $this->_userPassword = $this->_request->getValue( "userPassword" );
-            $this->_op           = Textfilter::filterAllHTML($this->_request->getValue( "op" ));
 
 			// create a plugin manager
 			$pm =& PluginManager::getPluginManager();	
@@ -138,5 +142,11 @@
             // better to return true if everything's fine
             return true;
         }
+
+		function performAjax()
+		{
+			lt_include( PLOG_CLASS_PATH."class/view/ajax/ajaxview.class.php" );
+			$this->_view = new AjaxView();			
+		}
     }
-?>
+?>
\ No newline at end of file

Modified: plog/branches/lifetype-1.3-ajax/class/controller/controller.class.php
===================================================================
--- plog/branches/lifetype-1.3-ajax/class/controller/controller.class.php	2007-07-04 20:31:21 UTC (rev 5611)
+++ plog/branches/lifetype-1.3-ajax/class/controller/controller.class.php	2007-07-04 21:21:32 UTC (rev 5612)
@@ -340,11 +340,15 @@
                     $actionObject = new $actionClass( $actionInfo, $httpRequest );
 					$actionObject->setPreviousAction( $_plogController_previousAction );
 					
+					// determine whether we should be calling the Ajax stuff or the normal stuff
+					$outputFormat = $request->getValue( "output", "html" );
+					$outputFormat == "json" ? $method = "performAjax" : $method = "perform";
+					
 					if( $actionObject->canPerform()) {
 	                    // we can use the validate method to check the values of the form variables. If validate()
 	                    // returns 'true', then we call the 'perform' method. If not, then we won't :)
 	                    if( $actionObject->validate()) {
-	                        if( $actionObject->perform())
+	                        if( $actionObject->$method())
 	                        	$actionObject->setSuccess( true );
 	                        else
 	                        	$actionObject->setSuccess( false );
@@ -359,7 +363,8 @@
 	                	$actionClass = $this->_cannotPerformAction;
 	                	$this->loadActionClass( $actionClass );
 	                	$actionObject = new $actionClass( $actionInfo, $httpRequest );
-						$actionObject->perform();
+	
+						$actionObject->$method();
                 	}
                 }
 
@@ -375,8 +380,9 @@
                 $e = new Exception( 'The view is empty after calling the perform method.' );
                 throw( $e );
             }
-            else
-                $view->render();
+            else {
+				$view->render();
+			}
         }
     }
-?>
+?>
\ No newline at end of file

Modified: plog/branches/lifetype-1.3-ajax/class/dao/userinfo.class.php
===================================================================
--- plog/branches/lifetype-1.3-ajax/class/dao/userinfo.class.php	2007-07-04 20:31:21 UTC (rev 5611)
+++ plog/branches/lifetype-1.3-ajax/class/dao/userinfo.class.php	2007-07-04 21:21:32 UTC (rev 5612)
@@ -368,5 +368,13 @@
 			
 			return( $this->perms[$blogId] );
 		}		
+		
+		function toJson()
+		{
+			$data = parent::toJson();
+			unset( $data["password"] );
+			
+			return( $data );
+		}		
 	}
 ?>

Modified: plog/branches/lifetype-1.3-ajax/class/data/forms/formvalidator.class.php
===================================================================
--- plog/branches/lifetype-1.3-ajax/class/data/forms/formvalidator.class.php	2007-07-04 20:31:21 UTC (rev 5611)
+++ plog/branches/lifetype-1.3-ajax/class/data/forms/formvalidator.class.php	2007-07-04 21:21:32 UTC (rev 5612)
@@ -109,6 +109,7 @@
 				
 				$this->_validationResults["$fieldName"] = $validationResult;
 				$this->_fieldValues["$fieldName"] = $fieldValue;
+				$this->_fieldErrorMessages["$fieldName"] = "whatever message";
 				
 				// if one of the validations is false, then cancel the whole thing
 				$finalValidationResult = $finalValidationResult && $validationResult;				
@@ -256,5 +257,23 @@
 			}
 			print("</pre>");
 		}
+		
+		/**
+		 * Serializes this object into a suitable Javascript object
+		 */
+		function toJson()
+		{
+			$data = Array();
+			$data["fields"] = Array();
+			foreach( $this->_fieldValidators as $field => $validationInfo ) {
+				$data["fields"][] = Array( 
+					"field" => $field, 
+					"validated" => $this->isFieldValid( $field ), 
+					"message" => $this->getFieldErrorMessage( $field )
+				);
+			}
+			
+			return( $data );
+		}
 	}
 ?>
\ No newline at end of file

Modified: plog/branches/lifetype-1.3-ajax/class/data/pager/pager.class.php
===================================================================
--- plog/branches/lifetype-1.3-ajax/class/data/pager/pager.class.php	2007-07-04 20:31:21 UTC (rev 5611)
+++ plog/branches/lifetype-1.3-ajax/class/data/pager/pager.class.php	2007-07-04 21:21:32 UTC (rev 5612)
@@ -286,5 +286,16 @@
 			$this->_pageLinks        = $this->generateLinks();
         }
 
+		function toJson()
+		{
+			return( Array( 
+					"totalPages" => $this->getTotalPages(),
+					"pageLinks" => $this->getPageLinks(),
+					"totalRecs" => $this->getTotalRegs(),
+					"recsPerPage" => $this->getRegsForPage(),
+					"curPage" => $this->getCurrentPage()
+				)
+			);
+		}
     }
-?>
+?>
\ No newline at end of file

Modified: plog/branches/lifetype-1.3-ajax/class/data/serialize/json/jsonserializer.class.php
===================================================================
--- plog/branches/lifetype-1.3-ajax/class/data/serialize/json/jsonserializer.class.php	2007-07-04 20:31:21 UTC (rev 5611)
+++ plog/branches/lifetype-1.3-ajax/class/data/serialize/json/jsonserializer.class.php	2007-07-04 21:21:32 UTC (rev 5612)
@@ -378,7 +378,7 @@
                 * parameter is only accessible using ECMAScript's
                 * bracket notation.
                 */
-                
+
                 // treat as a JSON object  
                 if (is_array($var) && count($var) && (array_keys($var) !== range(0, sizeof($var) - 1))) {
                     $properties = array_map(array($this, 'name_value'),
@@ -398,21 +398,48 @@
                 foreach($elements as $element)
                     if(JsonSerializer::isError($element))
                         return $element;
-                
+
                 return '[' . join(',', $elements) . ']';
                 
             case 'object':
-                $vars = get_object_vars($var);
 
-                $properties = array_map(array($this, 'name_value'),
-                                        array_keys($vars),
-                                        array_values($vars));
-            
-                foreach($properties as $property)
-                    if(JsonSerializer::isError($property))
-                        return $property;
+				// check if the object has a method called toJson()
+				$serialized = false;
+				$methods = get_class_methods( $var );				
+				foreach( $methods as $method ) {
+					if( strtolower( $method ) == "tojson" ) {
+						$vars = $var->toJson();
+						
+						
+						if( is_array( $vars )) {
+			                $properties = array_map(array($this, 'name_value'),
+			                                        array_keys($vars),
+			                                        array_values($vars));						
+
+	                		$data = '{' . join(',', $properties) . '}';	
+						}
+						else {
+							$data = $this->encode( $vars );
+						}
+						
+						$serialized = true;
+					}
+				}
+				
+				if( !$serialized ) {
+	                $vars = get_object_vars($var);
+	                $properties = array_map(array($this, 'name_value'),
+	                                        array_keys($vars),
+	                                        array_values($vars));
+
+	                foreach($properties as $property)
+	                    if(JsonSerializer::isError($property))
+	                        return $property;
+	
+                	$data = '{' . join(',', $properties) . '}';	
+				}
                 
-                return '{' . join(',', $properties) . '}';
+				return( $data );
 
             default:
                 return ($this->use & SERVICES_JSON_SUPPRESS_ERRORS)

Modified: plog/branches/lifetype-1.3-ajax/class/data/timestamp.class.php
===================================================================
--- plog/branches/lifetype-1.3-ajax/class/data/timestamp.class.php	2007-07-04 20:31:21 UTC (rev 5611)
+++ plog/branches/lifetype-1.3-ajax/class/data/timestamp.class.php	2007-07-04 21:21:32 UTC (rev 5612)
@@ -412,6 +412,15 @@
             
             // and return the whole thing
             return( $date );
-        }    	 
+        }
+
+		/**
+		 * Serialize this object into a suitable Json representation. It returns the time
+		 * in Unix time, which is suitable to be parsed by Javascript's Date class.
+		 */
+		function toJson()
+		{
+			return( $this->getUnixDate());
+		}
 	}
 ?>
\ No newline at end of file

Modified: plog/branches/lifetype-1.3-ajax/class/data/validator/validation.class.php
===================================================================
--- plog/branches/lifetype-1.3-ajax/class/data/validator/validation.class.php	2007-07-04 20:31:21 UTC (rev 5611)
+++ plog/branches/lifetype-1.3-ajax/class/data/validator/validation.class.php	2007-07-04 21:21:32 UTC (rev 5612)
@@ -62,5 +62,4 @@
             die();
         }
     }
-
-?>
+?>
\ No newline at end of file

Modified: plog/branches/lifetype-1.3-ajax/class/database/dbobject.class.php
===================================================================
--- plog/branches/lifetype-1.3-ajax/class/database/dbobject.class.php	2007-07-04 20:31:21 UTC (rev 5611)
+++ plog/branches/lifetype-1.3-ajax/class/database/dbobject.class.php	2007-07-04 21:21:32 UTC (rev 5612)
@@ -65,6 +65,16 @@
     	{
     		return( $this->_fields );
     	}
+
+		function toJson()
+		{
+			$data = Array();
+			foreach( $this->getFieldGetters() as $field => $getter ) {
+				$data[$field] = $this->$getter();
+			}
+			
+			return( $data );
+		}
 		
 		/**
 		 * No null values are serialized to the session. If there any values in your data class

Modified: plog/branches/lifetype-1.3-ajax/class/view/admin/admindefaultview.class.php
===================================================================
--- plog/branches/lifetype-1.3-ajax/class/view/admin/admindefaultview.class.php	2007-07-04 20:31:21 UTC (rev 5611)
+++ plog/branches/lifetype-1.3-ajax/class/view/admin/admindefaultview.class.php	2007-07-04 21:21:32 UTC (rev 5612)
@@ -29,6 +29,13 @@
         function AdminDefaultView()
         {
         	$this->View();
+
+			// set the view character set based on the default locale
+            $config =& Config::getConfig();
+            $locale =& Locales::getLocale( $config->getValue( "default_locale" ));
+            $this->setValue( 'version', Version::getVersion());
+            $this->setValue( "locale", $locale );   	
+			$this->setCharset( $locale->getCharset());
         }
 
         /**
@@ -38,18 +45,11 @@
          */
         function render()
         {
-			// set the view character set based on the default locale
-            $config =& Config::getConfig();
-            $locale =& Locales::getLocale( $config->getValue( "default_locale" ));
-            $this->setValue( 'version', Version::getVersion());            	
-			$this->setCharset( $locale->getCharset());
-		
 			parent::render();
 					
         	// to find the template we need, we can use the TemplateService
             $ts = new TemplateService();
         	$template = $ts->Template( DEFAULTADMIN_TEMPLATE, "admin" );
-            $this->setValue( "locale", $locale );
             // assign all the values
             $template->assign( $this->_params->getAsArray());
 			
@@ -57,4 +57,4 @@
             print $template->fetch();
         }
     }
-?>
+?>
\ No newline at end of file

Modified: plog/branches/lifetype-1.3-ajax/class/view/admin/adminlinkcategorieslistview.class.php
===================================================================
--- plog/branches/lifetype-1.3-ajax/class/view/admin/adminlinkcategorieslistview.class.php	2007-07-04 20:31:21 UTC (rev 5611)
+++ plog/branches/lifetype-1.3-ajax/class/view/admin/adminlinkcategorieslistview.class.php	2007-07-04 21:21:32 UTC (rev 5612)
@@ -23,10 +23,7 @@
 			$this->setValue( "searchTerms", $params["searchTerms"] );
 
 			$this->_page = $this->getCurrentPageFromRequest();
-		}
-		
-		function render()
-		{
+			
             // get all the link categories
             $searchTerms = $this->getValue( "searchTerms" );
 			$blogSettings = $this->_blogInfo->getSettings();
@@ -49,14 +46,18 @@
 			                    $this->_page,
 								$numLinkCategories,
 								DEFAULT_ITEMS_PER_PAGE );
-
+								
             // create the view and fill the template context
             $this->setValue( "linkcategories", $blogLinkCategories );
 			$this->setValue( "searchTerms", $searchTerms );
 			$this->setValue( "pager", $pager );
-			
+								
+		}
+		
+		function render()
+		{
 			// transfer control to the parent class
-			parent::render();
+			return( parent::render());
 		}
 	}
 ?>
\ No newline at end of file

Modified: plog/branches/lifetype-1.3-ajax/class/view/admin/admintemplatedview.class.php
===================================================================
--- plog/branches/lifetype-1.3-ajax/class/view/admin/admintemplatedview.class.php	2007-07-04 20:31:21 UTC (rev 5611)
+++ plog/branches/lifetype-1.3-ajax/class/view/admin/admintemplatedview.class.php	2007-07-04 21:21:32 UTC (rev 5612)
@@ -46,4 +46,4 @@
             print( $output );
         }
     }
-?>
+?>
\ No newline at end of file

Modified: plog/branches/lifetype-1.3-ajax/class/view/admin/ajax/adminajaxview.class.php
===================================================================
--- plog/branches/lifetype-1.3-ajax/class/view/admin/ajax/adminajaxview.class.php	2007-07-04 20:31:21 UTC (rev 5611)
+++ plog/branches/lifetype-1.3-ajax/class/view/admin/ajax/adminajaxview.class.php	2007-07-04 21:21:32 UTC (rev 5612)
@@ -1,33 +1,17 @@
 <?php
 
-	lt_include( PLOG_CLASS_PATH."class/view/admin//adminview.class.php" );
+	lt_include( PLOG_CLASS_PATH."class/view/ajax/ajaxview.class.php" );
 
-	class AdminAjaxView extends AdminView
+	class AdminAjaxView extends AjaxView
 	{
-		function AjaxView( $blogInfo, $method = "" )
+		
+		function AdminAjaxView( $blogInfo )
 		{
-			$this->AdminView( $blogInfo );
+			$this->_blogInfo = $blogInfo;
 			
-            $this->_params = new Properties();
-			
-			$this->setValue( "method", $method );
+			$this->AjaxView();
 		}
 		
-		function setSuccess( $success )
-		{
-			$this->setValue( "success", $success );
-		}
-		
-		function setMessage( $message )
-		{
-			$this->setValue( "message", $message );
-		}
-		
-		function setResult( $result )
-		{
-			$this->setValue( "result", $result );
-		}
-		
 		function render()
 		{
 			// simply serialize the array with the parameters as json stream and send it to the client
@@ -39,9 +23,10 @@
 			if( isset( $data["url"] )) unset( $data["url"] );
 			if( isset( $data["config"] )) unset( $data["config"] );
 			if( isset( $data["blogsettings"] )) unset( $data["blogsettings"] );
-			if( isset( $data["form"] )) unset( $data["blogsettings"] );	
+			if( isset( $data["viewIsError"] )) unset( $data["viewIsError"] );
+			if( isset( $data["viewErrorMessage"] )) unset( $data["viewErrorMessage"] );			
 			
 			print( SerializerFactory::toJson( $data ));
-		}		
+		}
 	}
 ?>
\ No newline at end of file

Added: plog/branches/lifetype-1.3-ajax/class/view/ajax/ajaxview.class.php
===================================================================
--- plog/branches/lifetype-1.3-ajax/class/view/ajax/ajaxview.class.php	                        (rev 0)
+++ plog/branches/lifetype-1.3-ajax/class/view/ajax/ajaxview.class.php	2007-07-04 21:21:32 UTC (rev 5612)
@@ -0,0 +1,49 @@
+<?php
+
+	lt_include( PLOG_CLASS_PATH."class/view/view.class.php" );
+
+	class AjaxView extends View
+	{
+		function AjaxView( $method = "" )
+		{
+			$this->View();
+			$this->setValue( "method", $method );
+		}
+		
+		function setSuccess( $success )
+		{
+			$this->setValue( "success", $success );
+		}
+		
+		function setMessage( $message )
+		{
+			$this->setValue( "message", $message );
+		}
+		
+		function setErrorMessage( $message )
+		{
+			$this->setMessage( $message );
+			$this->setSuccess( false );
+		}
+		
+		function setSuccessMessage( $message )
+		{
+			$this->setMessage( $message );
+			$this->setSuccess( true );
+		}
+		
+		function setResult( $result )
+		{
+			$this->setValue( "result", $result );
+		}
+		
+		function render()
+		{
+			// simply serialize the array with the parameters as json stream and send it to the client
+			lt_include( PLOG_CLASS_PATH."class/data/serialize/serializerfactory.class.php" );
+			
+			$data = $this->_params->getAsArray();
+			print( SerializerFactory::toJson( $data ));
+		}
+	}
+?>
\ No newline at end of file

Modified: plog/branches/lifetype-1.3-ajax/class/view/view.class.php
===================================================================
--- plog/branches/lifetype-1.3-ajax/class/view/view.class.php	2007-07-04 20:31:21 UTC (rev 5611)
+++ plog/branches/lifetype-1.3-ajax/class/view/view.class.php	2007-07-04 21:21:32 UTC (rev 5612)
@@ -67,8 +67,6 @@
          */
 		function View()
         {
-			
-
             $this->_params = new Properties();
 			
 			// set a default content type and character set for responses
@@ -186,6 +184,11 @@
 			return true;
 		}
 		
+		function getErrorMessage()
+		{
+			return( $this->getValue( "viewErrorMessage" ));
+		}
+		
 		/**
 		 * Whether the view has to show some error message or not. Views can
 		 * show success messages as well as error messages at the same time.
@@ -229,6 +232,11 @@
 			return true;
 		}
 		
+		function getSuccessMessage()
+		{
+			return( $this->getValue( "viewSuccessMessage" ));
+		}		
+		
 		/**
 		 * stores a value in the session, associated to one key, in case
 		 * the view wants to keep some value for later use such as filter settings
@@ -318,4 +326,4 @@
             $this->sendContentType();            
         }
     }
-?>
+?>
\ No newline at end of file



More information about the pLog-svn mailing list