From f22ee63ecc46395102f694f0ce1b41e435658d58 Mon Sep 17 00:00:00 2001 From: dogmatic69 Date: Tue, 29 Dec 2009 02:10:06 +0200 Subject: [PATCH] finished up commentable integration, adding form to test email sending in newsletter manager. --- app/app_controller.php | 32 +++++++------- app/plugins/blog/models/post.php | 3 +- app/plugins/blog/views/posts/comment.ctp | 3 ++ .../core/models/behaviors/commentable.php | 43 ++++++++++++++----- .../controllers/campaigns_controller.php | 24 +++++++++++ .../controllers/newsletters_controller.php | 5 +++ .../views/newsletters/admin_test_send.ctp | 0 .../views/newsletters/admin_view.ctp | 9 ++++ 8 files changed, 91 insertions(+), 28 deletions(-) create mode 100644 app/plugins/blog/views/posts/comment.ctp create mode 100644 app/plugins/newsletter/views/newsletters/admin_test_send.ctp diff --git a/app/app_controller.php b/app/app_controller.php index f7520d260..4898a22a3 100644 --- a/app/app_controller.php +++ b/app/app_controller.php @@ -146,34 +146,32 @@ private function __setupCache() * Common methods for the app */ - protected function comment() + protected function comment($id = null) { - if ( !empty( $this->data ) ) + if ( !empty( $this->data['Comment'] ) ) { - if ( !isset( $this->data[Inflector::classify( $this->name )]['id'] ) ) + + $message = 'Your comment has been saved and will be available after admin moderation.'; + if ( Configure::read( 'Comments.auto_moderate' ) === true ) { - $this->Session->setFlash( __( 'There is a problem with your comment.', true ) ); - $this->redirect( $this->referer() ); + $this->data['Comment']['active'] = 1; + $message = 'Your comment has been saved and is active.'; } - $this->data['Comment']['foreign_id'] = $this->data[Inflector::classify( $this->name )]['id']; - $this->data['Comment']['class'] = $this->__getClassName(); - - $comment['Comment'] = $this->data['Comment']; - - ClassRegistry::init( 'Core.Comment' )->create(); - - if ( ClassRegistry::init( 'Core.Comment' )->save( $comment ) ) + if ( $this->Post->createComment( $id, $this->data ) ) { - $this->Session->setFlash( __( 'Your comment has been saved for review.', true ) ); - $this->redirect( $this->referer() ); + $this->Session->setFlash( __( $message, true ) ); + $this->redirect( array( 'action' => 'view', $this->data[$this->modelClass]['id'] ) ); } - $this->Session->setFlash( __( 'Your comment could not be saved, please try again.', true ) ); - $this->redirect( $this->referer() ); + else + { + $this->Session->setFlash( __( 'Your comment was not saved. Please check for errors and try again', true ) ); + } } } + function __getClassName() { if ( isset( $this->params['plugin'] ) ) diff --git a/app/plugins/blog/models/post.php b/app/plugins/blog/models/post.php index b0421cd88..9faf7769c 100644 --- a/app/plugins/blog/models/post.php +++ b/app/plugins/blog/models/post.php @@ -33,7 +33,8 @@ class Post extends BlogAppModel var $actsAs = array( 'Core.Sluggable', 'Core.Viewable', - 'Core.Feedable' + 'Core.Feedable', + 'Core.Commentable' ); var $validate = array( diff --git a/app/plugins/blog/views/posts/comment.ctp b/app/plugins/blog/views/posts/comment.ctp new file mode 100644 index 000000000..610f2e275 --- /dev/null +++ b/app/plugins/blog/views/posts/comment.ctp @@ -0,0 +1,3 @@ +element( 'comments/add', array( 'plugin' => 'core', 'fk' => $this->data['Post']['id'] ) ); +?> \ No newline at end of file diff --git a/app/plugins/core/models/behaviors/commentable.php b/app/plugins/core/models/behaviors/commentable.php index e805c224e..5346141be 100644 --- a/app/plugins/core/models/behaviors/commentable.php +++ b/app/plugins/core/models/behaviors/commentable.php @@ -30,6 +30,7 @@ class CommentableBehavior extends ModelBehavior 'class' => 'Comment', // name of Comment model 'foreign_key' => 'foreign_id', // foreign key of Comment model 'counter_cache' => true, + 'counter_cache_scope' => array( 'Comment.active' => 1 ), 'dependent' => true, // model dependency 'conditions' => array(), // conditions for find method on Comment model 'auto_bind' => true, // automatically bind the model to the User model (default true), @@ -40,7 +41,7 @@ class CommentableBehavior extends ModelBehavior 'column_email' => 'email', // Column name for the authors email 'column_website' => 'website', // Column name for the authors website 'column_foreign_id' => 'foreign_id', // Column name of the foreign id that links to the article/entry/etc - 'column_status' => 'active', // Column name for automatic rating + 'column_status' => 'status', // Column name for automatic rating 'column_points' => 'points', // Column name for accrued points 'blacklist_keywords' => array( 'levitra', 'viagra', 'casino', 'sex', 'loan', 'finance', @@ -88,10 +89,12 @@ function setup( &$model, $settings = array() ) 'foreignKey' => $this->__settings[$model->alias]['foreign_key'], 'dependent' => $this->__settings[$model->alias]['dependent'], 'conditions' => $this->__settings[$model->alias]['conditions'] ) ); + $commentBelongsTo = array( $model->alias => array( 'className' => $model->alias, 'foreignKey' => $this->__settings[$model->alias]['foreign_key'], - 'counterCache' => $this->__settings[$model->alias]['counter_cache'] + 'counterCache' => $this->__settings[$model->alias]['counter_cache'], + 'counterScope' => $this->__settings[$model->alias]['counter_cache_scope'] ) ); $model->bindModel( array( 'hasMany' => $hasManyComment ), false ); @@ -151,6 +154,17 @@ function createComment( &$model, $id, $data = array() ) $data[$this->__settings[$model->alias]['class']][$this->__settings[$model->alias]['column_class']] = $model->alias; $data[$this->__settings[$model->alias]['class']][$this->__settings[$model->alias]['column_foreign_id']] = $id; $data[$this->__settings[$model->alias]['class']] = $this->_rateComment( $model, $data['Comment'] ); + + if ( $data[$this->__settings[$model->alias]['class']]['status'] == 'spam' ) + { + $data[$this->__settings[$model->alias]['class']]['active'] == 0; + } + + else if ( Configure::read( 'Comments.auto_moderate' ) === true && $data[$this->__settings[$model->alias]['class']]['status'] != 'spam') + { + $data[$this->__settings[$model->alias]['class']]['active'] == 1; + } + if ( $this->__settings[$model->alias]['sanitize'] ) { App::import( 'Sanitize' ); @@ -243,11 +257,11 @@ function _rateLinks( $model, $data ) $data[$this->__settings[$model->alias]['column_content']], $matches ); $links = $matches[0]; - $totalLinks = count( $links ); + $this->totalLinks = count( $links ); $length = mb_strlen( $data[$this->__settings[$model->alias]['column_content']] ); // How many links are in the body // -1 per link if over 2, otherwise +2 if less than 2 - $points = ( $totalLinks > 2 ) ? $totalLinks * - 1 : 2; + $points = ( $this->totalLinks > 2 ) ? $totalLinks * - 1 : 2; // URLs that have certain words or characters in them // -1 per blacklisted word // URL length @@ -274,13 +288,18 @@ function _rateLength( $model, $data ) // How long is the body // +2 if more then 20 chars and no links, -1 if less then 20 $length = mb_strlen( $data[$this->__settings[$model->alias]['column_content']] ); - if ( $length >= 20 && $totalLinks <= 0 ) + + if ( $length >= 20 && $this->totalLinks <= 0 ) { return 2; - } elseif ( $length >= 20 && $totalLinks == 1 ) + } + + elseif ( $length >= 20 && $this->totalLinks == 1 ) { return 1; - } elseif ( $length < 20 ) + } + + elseif ( $length < 20 ) { return - 1; } @@ -291,13 +310,15 @@ function _rateEmail( $model, $data ) $points = 0; // Number of previous comments from email // +1 per approved, -1 per spam - $comments = $model->Comment->find( 'all', array( + $comments = $model->Comment->find( + 'all', array( 'fields' => array( 'Comment.id', 'Comment.status' ), 'conditions' => array( 'Comment.' . $this->__settings[$model->alias]['column_email'] => $data[$this->__settings[$model->alias]['column_email']] ), 'recursive' => - 1, 'contain' => false - ) ); + ) + ); if ( !empty( $comments ) ) { @@ -306,7 +327,9 @@ function _rateEmail( $model, $data ) if ( $comment['Comment']['status'] == 'spam' ) { --$points; - } elseif ( $comment['Comment']['status'] == 'approved' ) + } + + elseif ( $comment['Comment']['status'] == 'approved' ) { ++$points; } diff --git a/app/plugins/newsletter/controllers/campaigns_controller.php b/app/plugins/newsletter/controllers/campaigns_controller.php index 80a5e5e55..6f799e52c 100644 --- a/app/plugins/newsletter/controllers/campaigns_controller.php +++ b/app/plugins/newsletter/controllers/campaigns_controller.php @@ -167,6 +167,30 @@ function admin_toggle( $id = null ) function admin_delete( $id = null ) { + + if ( !$id ) + { + $this->Session->setFlash( 'That Campaign could not be found', true ); + $this->redirect( $this->referer() ); + } + + $campaign = $this->Campaign->read( null, $id ); + + $sent = false; + foreach( $campaign['Newsletter'] as $newsletter ) + { + if ( $newsletter['sent'] == 1 || $newsletter['sends'] > 0 ) + { + $sent = true; + } + } + + if ( $sent ) + { + $this->Session->setFlash( 'You can not delete a Campaign that has sent newsletters', true ); + $this->redirect( $this->referer() ); + } + return parent::admin_delete( $id ); } } diff --git a/app/plugins/newsletter/controllers/newsletters_controller.php b/app/plugins/newsletter/controllers/newsletters_controller.php index db9201158..216136cd1 100644 --- a/app/plugins/newsletter/controllers/newsletters_controller.php +++ b/app/plugins/newsletter/controllers/newsletters_controller.php @@ -238,6 +238,11 @@ function admin_preview( $id = null ) } + function admin_testSend( $id = null ) + { + + } + function admin_delte() { diff --git a/app/plugins/newsletter/views/newsletters/admin_test_send.ctp b/app/plugins/newsletter/views/newsletters/admin_test_send.ctp new file mode 100644 index 000000000..e69de29bb diff --git a/app/plugins/newsletter/views/newsletters/admin_view.ctp b/app/plugins/newsletter/views/newsletters/admin_view.ctp index 5c2714e45..e529d8ca8 100644 --- a/app/plugins/newsletter/views/newsletters/admin_view.ctp +++ b/app/plugins/newsletter/views/newsletters/admin_view.ctp @@ -1,4 +1,13 @@ +Letter->adminOtherHead( $this ); ?>

Letter->preview( $newsletter['Newsletter']['id'], 'newsletters' ); +?> + +

+Form->create( 'Newsletter', array( 'action' => 'view' ) ); + echo '

'.__( 'Enter the email addresses you would like to send to seperated by a , {comma}.', true ).'

'; + echo $this->Form->input( 'email_addresses', array( 'type' => 'textarea', 'class' => 'title', 'value' => 'dogmatic69@gmail.com,carl@php-dev.co.za' ) ); + echo $this->Form->end( 'Send the test' ); ?> \ No newline at end of file