[5131] branches: beginning work on adding types to comments.

1 view
Skip to first unread message

s...@habariproject.org

unread,
Jun 1, 2011, 2:50:19 PM6/1/11
to habar...@googlegroups.com
Revision
5131
Author
chrisjdavis
Date
2011-06-01 18:50:01 +0000 (Wed, 01 Jun 2011)

Log Message

beginning work on adding types to comments.

Modified Paths

Added Paths

  • branches/comment_types/

Diff

Modified: branches/comment_types/system/classes/comment.php (5130 => 5131)


--- trunk/htdocs/system/classes/comment.php	2011-05-29 00:00:22 UTC (rev 5130)
+++ branches/comment_types/system/classes/comment.php	2011-06-01 18:50:01 UTC (rev 5131)
@@ -77,6 +77,190 @@
 	}
 
 	/**
+	 * returns an associative array of active comment types
+	 * @param bool whether to force a refresh of the cached values
+	 * @return array An array of comment type names => integer values
+	 */
+	public static function list_active_comment_types( $refresh = false )
+	{
+		if ( ( ! $refresh ) && ( ! empty( self::$comment_type_list_active ) ) ) {
+			return self::$comment_type_list_active;
+		}
+		
+		// clear out the previous cache
+		self::$comment_type_list_active = array( 'any' => 0 );
+		
+		$sql = 'SELECT * FROM {commenttype} WHERE active = 1 ORDER BY id ASC';
+		$results = DB::get_results( $sql );
+		foreach ( $results as $result ) {
+			self::$comment_type_list_active[$result->name] = $result->id;
+		}
+		return self::$comment_type_list_active;
+	}
+
+	/**
+	 * returns an associative array of all comment types
+	 * @param bool whether to force a refresh of the cached values
+	 * @return array An array of post type names => (integer values, active values)
+	 */
+	public static function list_all_comment_types( $refresh = false )
+	{
+		if ( ( ! $refresh ) && ( ! empty( self::$comment_type_list_all ) ) ) {
+			return self::$comment_type_list_all;
+		}
+		
+		// clear out the previous cache
+		self::$comment_type_list_all = array( 'any' => 0 );
+		
+		$sql = 'SELECT * FROM {commenttype} ORDER BY id ASC';
+		$results = DB::get_results( $sql );
+		foreach ( $results as $result ) {
+			self::$comment_type_list_all[$result->name] = array(
+				'id' => $result->id,
+				'active' => $result->active
+				);
+		}
+		return self::$comment_type_list_all;
+	}
+
+	/**
+	 * Activate an existing comment type
+	 *
+	 * @param string The comment type to activate
+	 */
+	public static function activate_comment_type( $type )
+	{
+		$all_comment_types = Comment::list_all_comment_types( true ); // We force a refresh
+
+		// Check if it exists
+		if ( array_key_exists( $type, $all_comment_types ) ) {
+			if ( ! $all_comment_types[$type]['active'] == 1 ) {
+				// Activate it
+				$sql = 'UPDATE {commenttype} SET active = 1 WHERE id = ' . $all_comment_types[$type]['id'];
+				DB::query( $sql );
+			}
+			return true;
+		}
+		else {
+			return false; // Doesn't exist
+		}
+	}
+
+	/**
+	 * Deactivate a comment type
+	 *
+	 * @param string The comment type to deactivate
+	 */
+	public static function deactivate_comment_type( $type )
+	{
+		$active_comment_types = Comment::list_active_comment_types( false ); // We force a refresh
+
+		if ( array_key_exists( $type, $active_comment_types ) ) {
+			// $type is active so we'll deactivate it
+			$sql = 'UPDATE {commenttype} SET active = 0 WHERE id = ' . $active_comment_types[$type];
+			DB::query( $sql );
+			return true;
+		}
+		return false;
+	}
+
+	/**
+	 * returns the integer value of the specified comment type, or false
+	 * @param mixed a comment type name or number
+	 * @return mixed an integer or boolean false
+	 */
+	public static function type( $name )
+	{
+		$types = Comment::list_active_comment_types();
+		if ( is_numeric( $name ) && ( false !== in_array( $name, $types ) ) ) {
+			return $name;
+		}
+		if ( isset( $types[ MultiByte::strtolower( $name ) ] ) ) {
+			return $types[ MultiByte::strtolower( $name ) ];
+		}
+		return false;
+	}
+
+	/**
+	 * returns the friendly name of a comment type, or null
+	 * @param mixed a comment type number, or name
+	 * @return mixed a string of the comment type, or null
+	 */
+	public static function type_name( $type )
+	{
+		$types = array_flip( Comment::list_active_comment_types() );
+		if ( is_numeric( $type ) && isset( $types[$type] ) ) {
+			return $types[$type];
+		}
+		if ( false !== in_array( $type, $types ) ) {
+			return $type;
+		}
+		return '';
+	}
+
+	/**
+	 * inserts a new comment type into the database, if it doesn't exist
+	 * @param string The name of the new comment type
+	 * @param bool Whether the new comment type is active or not
+	 * @return none
+	 */
+	public static function add_new_type( $type, $active = true )
+	{
+		// refresh the cache from the DB, just to be sure
+		$types = self::list_all_comment_types( true );
+
+		if ( ! array_key_exists( $type, $types ) ) {
+			// Doesn't exist in DB.. add it and activate it.
+			DB::query( 'INSERT INTO {commenttype} (name, active) VALUES (?, ?)', array( $type, $active ) );
+		}
+		elseif ( $types[$type]['active'] == 0 ) {
+			// Isn't active so we activate it
+			self::activate_comment_type( $type );
+		}
+		ACL::create_token( 'comment_' . Utils::slugify( $type ), _t( 'Permissions to comments of type "%s"', array( $type ) ), _t( 'Content' ), true );
+
+		// now force a refresh of the caches, so the new/activated type
+		// is available for immediate use
+		$types = self::list_active_comment_types( true );
+		$types = self::list_all_comment_types( true );
+	}
+
+	/**
+	 * removes a comment type from the database, if it exists and there are no comments
+	 * of the type
+	 * @param string The comment type name
+	 * @return boolean
+	 *   true if comment type has been deleted
+	 *   false if it has not been deleted (does not exist or there are comments using
+	 *   this content type)
+	 */
+	public static function delete_comment_type( $type )
+	{
+		// refresh the cache from the DB, just to be sure
+		$types = self::list_all_comment_types( true );
+
+		if ( array_key_exists( $type, $types ) ) {
+
+			// Exists in DB.. check if there are content with this type.
+			if ( ! DB::exists( '{comments}', array( 'content_type' => Comment::type( $type ) ) ) ) {
+
+				// Finally, remove from database and destroy tokens
+				DB::delete( '{commenttype}', array( 'name' => $type ) );
+				ACL::destroy_token( 'comment_' . Utils::slugify( $type ) );
+
+				// now force a refresh of the caches, so the removed type is no longer
+				// available for use
+				$types = self::list_active_comment_types( true );
+				$types = self::list_all_comment_types( true );
+
+				return true;
+			}
+		}
+
+		return false;
+	}
+
+	/**
 	 * static function get
 	 * Returns a single comment, by ID
 	 *
@@ -332,24 +516,6 @@
 	}
 
 	/**
-	 * returns an associative array of comment types
-	 * @param bool whether to force a refresh of the cached values
-	 * @return array An array of comment type names => integer values
-	 */
-	public static function list_comment_types( $refresh = false )
-	{
-		if ( ( ! $refresh ) && ( ! empty( self::$comment_type_list ) ) ) {
-			return self::$comment_type_list;
-		}
-		self::$comment_type_list = array(
-			self::COMMENT => 'comment',
-			self::PINGBACK => 'pingback',
-			self::TRACKBACK => 'trackback',
-		);
-		return self::$comment_type_list;
-	}
-
-	/**
 	 * returns an associative array of comment statuses
 	 * @param bool whether to force a refresh of the cached values
 	 * @return array An array of comment statuses names => interger values
@@ -433,42 +599,6 @@
 	}
 
 	/**
-	 * returns the integer value of the specified comment type, or false
-	 * @param mixed a comment type name or number
-	 * @return mixed an integer or boolean false
-	 */
-	public static function type( $name )
-	{
-		$types = Comment::list_comment_types();
-		if ( is_numeric( $name ) && ( isset( $types[$name] ) ) ) {
-			return $name;
-		}
-		$types = array_flip( $types );
-		if ( isset( $types[$name] ) ) {
-			return $types[$name];
-		}
-		return false;
-	}
-
-	/**
-	 * returns the friendly name of a comment type, or null
-	 * @param mixed a comment type number, or name
-	 * @return mixed a string of the comment type, or null
-	 */
-	public static function type_name( $type )
-	{
-		$types = Comment::list_comment_types();
-		if ( is_numeric( $type ) && isset( $types[$type] ) ) {
-			return $types[$type];
-		}
-		$types = array_flip( $types );
-		if ( isset( $types[$type] ) ) {
-			return $type;
-		}
-		return '';
-	}
-
-	/**
 	 * Return the content type of this object
 	 *
 	 * @return string The content type of this object

Modified: branches/comment_types/system/plugins/autop/autop.plugin.php (5130 => 5131)


--- trunk/htdocs/system/plugins/autop/autop.plugin.php	2011-05-29 00:00:22 UTC (rev 5130)
+++ branches/comment_types/system/plugins/autop/autop.plugin.php	2011-06-01 18:50:01 UTC (rev 5131)
@@ -11,6 +11,7 @@
 	public function action_init_theme_any( $theme )
 	{
 		Format::apply( 'autop', 'post_content_out' );
+		Format::apply( 'autop', 'comment_content_out' );
 	}
 	
 }
Reply all
Reply to author
Forward
0 new messages