Displays remaining lines and characters in nl article submission (Closes #908).
authorStéphane Jacob <sj@m4x.org>
Sun, 13 Mar 2011 20:52:45 +0000 (21:52 +0100)
committerStéphane Jacob <sj@m4x.org>
Mon, 14 Mar 2011 12:23:20 +0000 (13:23 +0100)
Signed-off-by: Stéphane Jacob <sj@m4x.org>
ChangeLog
include/newsletter.inc.php
modules/newsletter.php
templates/newsletter/remaining.tpl [new file with mode: 0644]
templates/newsletter/submit.tpl

index 26e5f8e..8d90d6c 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -13,6 +13,9 @@ Bug/Wish:
         - #1035: Bases emails on users types (eg educationnal types)       -JAC
         - #1404: Implements best_domain                                    -JAC
 
+    * Newsletter:
+        - #908: Displays remaining lines and characters in submission      -JAC
+
     * Payments:
         - #1398: Creates csv for payments                                  -JAC
 
index 667099c..0ee1423 100644 (file)
@@ -1124,7 +1124,8 @@ class NLIssue
 class NLArticle
 {
     // Maximum number of lines per article
-    const MAX_LINES_PER_ARTICLE = 9;
+    const MAX_LINES_PER_ARTICLE = 8;
+    const MAX_CHARACTERS_PER_LINE = 68;
 
     // {{{ properties
 
@@ -1212,17 +1213,30 @@ class NLArticle
 
     public function check()
     {
-        $text = MiniWiki::WikiToText($this->body);
-        $arr  = explode("\n",wordwrap($text,68));
-        $c    = 0;
-        foreach ($arr as $line) {
-            if (trim($line)) {
-                $c++;
+        $rest = $this->remain();
+
+        return $rest['remaining_lines'] >= 0;
+    }
+
+    // }}}
+    // {{{ function remain()
+
+    public function remain()
+    {
+        $text  = MiniWiki::WikiToText($this->body);
+        $array = explode("\n", wordwrap($text, self::MAX_CHARACTERS_PER_LINE));
+        $lines_count = 0;
+        foreach ($array as $line) {
+            if (trim($line) != '') {
+                ++$lines_count;
             }
         }
-        return $c < self::MAX_LINES_PER_ARTICLE;
-    }
 
+        return array(
+            'remaining_lines'                    => self::MAX_LINES_PER_ARTICLE - $lines_count,
+            'remaining_characters_for_last_line' => self::MAX_CHARACTERS_PER_LINE - strlen($array[count($array) - 1])
+       );
+    }
     // }}}
     // {{{ function parseUrlsFromArticle()
 
index 6d18e7e..632f375 100644 (file)
@@ -27,6 +27,7 @@ class NewsletterModule extends PLModule
             'nl'                           => $this->make_hook('nl',              AUTH_COOKIE),
             'nl/show'                      => $this->make_hook('nl_show',         AUTH_COOKIE),
             'nl/submit'                    => $this->make_hook('nl_submit',       AUTH_MDP),
+            'nl/remaining'                 => $this->make_hook('nl_remaining',    AUTH_MDP),
             'admin/nls'                    => $this->make_hook('admin_nl_groups', AUTH_MDP, 'admin'),
             'admin/newsletter'             => $this->make_hook('admin_nl',        AUTH_MDP, 'admin'),
             'admin/newsletter/categories'  => $this->make_hook('admin_nl_cat',    AUTH_MDP, 'admin'),
@@ -118,6 +119,21 @@ class NewsletterModule extends PLModule
         $page->addCssLink($nl->cssFile());
     }
 
+    function handler_nl_remaining($page)
+    {
+        require_once 'newsletter.inc.php';
+
+        pl_content_headers('text/html');
+        $page->changeTpl('newsletter/remaining.tpl', NO_SKIN);
+
+        $article = new NLArticle('', Post::t('body'), '');
+        $rest = $article->remain();
+
+        $page->assign('too_long', $rest['remaining_lines'] < 0);
+        $page->assign('last_line', ($rest['remaining_lines'] == 0));
+        $page->assign('remaining', ($rest['remaining_lines'] == 0) ? $rest['remaining_characters_for_last_line'] : $rest['remaining_lines']);
+    }
+
     function handler_admin_nl($page, $new = false) {
         $page->changeTpl('newsletter/admin.tpl');
         $page->setTitle('Administration - Newsletter : liste');
diff --git a/templates/newsletter/remaining.tpl b/templates/newsletter/remaining.tpl
new file mode 100644 (file)
index 0000000..9de482a
--- /dev/null
@@ -0,0 +1,31 @@
+{**************************************************************************}
+{*                                                                        *}
+{*  Copyright (C) 2003-2011 Polytechnique.org                             *}
+{*  http://opensource.polytechnique.org/                                  *}
+{*                                                                        *}
+{*  This program is free software; you can redistribute it and/or modify  *}
+{*  it under the terms of the GNU General Public License as published by  *}
+{*  the Free Software Foundation; either version 2 of the License, or     *}
+{*  (at your option) any later version.                                   *}
+{*                                                                        *}
+{*  This program is distributed in the hope that it will be useful,       *}
+{*  but WITHOUT ANY WARRANTY; without even the implied warranty of        *}
+{*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *}
+{*  GNU General Public License for more details.                          *}
+{*                                                                        *}
+{*  You should have received a copy of the GNU General Public License     *}
+{*  along with this program; if not, write to the Free Software           *}
+{*  Foundation, Inc.,                                                     *}
+{*  59 Temple Place, Suite 330, Boston, MA  02111-1307  USA               *}
+{*                                                                        *}
+{**************************************************************************}
+
+<small>
+  {if t($too_long)}
+  L'article est trop long.
+  {else}
+  Il reste encore {$remaining} {if t($last_line)}caractères{else}lignes{/if}, une fois l'article formatté.
+  {/if}
+</small>
+
+{* vim:set et sw=2 sts=2 sws=2 enc=utf-8: *}
index 9e3758f..eb7030d 100644 (file)
@@ -104,9 +104,14 @@ Tu peux <a href='nl/submit#conseils'>lire les conseils de rédaction</a> avant d
     <tr class="pair">
       <td class='titre'>Contenu</td>
       <td>
-        <textarea cols="68" rows="8" name='body'>{if t($art)}{$art->body()}{/if}</textarea>
+        <textarea onchange="{literal}$.post('nl/remaining/', {'body': this.value}, function(data) {$('#remaining').html(data)}){/literal}"
+                  onkeyup="{literal}$.post('nl/remaining/', {'body': this.value}, function(data) {$('#remaining').html(data)}){/literal}"
+                  cols="68" rows="8" name="body" >{if t($art)}{$art->body()}{/if}</textarea>
       </td>
     </tr>
+    <tr class="pair">
+      <td id="remaining" class="center" colspan="2"></td>
+    </tr>
     <tr class="impair">
       <td class='titre'>Ajouts (emails, contacts, tarifs, site web&hellip;)</td>
       <td>