Community letter: add article submission limitations
authorNicolas Iooss <nicolas.iooss_git@polytechnique.org>
Sat, 28 Dec 2013 21:55:53 +0000 (22:55 +0100)
committerNicolas Iooss <nicolas.iooss_git@polytechnique.org>
Mon, 30 Dec 2013 19:10:17 +0000 (20:10 +0100)
include/comletter.inc.php
modules/comletter.php
templates/comletter/submit.tpl

index 713101a..25efeea 100644 (file)
 
 class ComLArticle
 {
+    // Maximum number of lines per article, as wanted by the NL master
+    const MAX_LINES_PER_ARTICLE = 4;
+    const MAX_CHARACTERS_PER_LINE = 68;
+
     // {{{ properties
 
     public $aid;
@@ -105,6 +109,35 @@ class ComLArticle
     }
 
     // }}}
+    // {{{ function check()
+
+    public function check()
+    {
+        $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 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])
+       );
+    }
+    // }}}
 }
 
 // }}}
index 959b62a..b6e7194 100644 (file)
@@ -32,6 +32,7 @@ class ComLetterModule extends NewsletterModule
         return array(
             'comletter'                   => $this->make_hook('nl',              AUTH_COOKIE, 'user'),
             'comletter/submit'            => $this->make_hook('coml_submit',     AUTH_PASSWD, 'user'),
+            'comletter/remaining'         => $this->make_hook('coml_remaining',  AUTH_PASSWD, 'user'),
             'comletter/out'               => $this->make_hook('out',             AUTH_PUBLIC),
             'comletter/show'              => $this->make_hook('nl_show',         AUTH_COOKIE, 'user'),
             'comletter/search'            => $this->make_hook('nl_search',       AUTH_COOKIE, 'user'),
@@ -78,6 +79,19 @@ class ComLetterModule extends NewsletterModule
         $page->addCssLink($nl->cssFile());
     }
 
+    function handler_coml_remaining($page)
+    {
+        pl_content_headers('text/html');
+        $page->changeTpl('newsletter/remaining.tpl', NO_SKIN);
+
+        $article = new ComLArticle('', 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_out($page, $hash = null, $issue_id = null)
     {
         if (!$hash) {
index c2aa702..f086bb9 100644 (file)
@@ -36,6 +36,14 @@ Nous te recontacterons éventuellement si nous avons des renseignements à te de
 
 
 {if t($art)}
+
+{if !$art->check()}
+<p class='erreur'>
+L'article que tu as proposé est trop long&nbsp;!<br />
+Il te faut te limiter à 4 lignes de 68 caractères.
+</p>
+{/if}
+
 <form action="comletter/submit" method='post'>
   <table class='bicol'>
     <tr><th>Version texte</th></tr>
@@ -50,6 +58,7 @@ Nous te recontacterons éventuellement si nous avons des renseignements à te de
         </div>
       </td>
     </tr>
+    {if $art->check()}
     <tr>
       <th>Soumettre</th>
     </tr>
@@ -67,6 +76,7 @@ Nous te recontacterons éventuellement si nous avons des renseignements à te de
         <input type='submit' name='valid' value='Soumettre' />
       </td>
     </tr>
+    {/if}
   </table>
 </form>
 
@@ -93,7 +103,9 @@ Tu peux <a href='comletter/submit#conseils'>lire les conseils de rédaction</a>
     <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('comletter/remaining/', {'body': this.value}, function(data) {$('#remaining').html(data)}){/literal}"
+                  onkeyup="{literal}$.post('comletter/remaining/', {'body': this.value}, function(data) {$('#remaining').html(data)}){/literal}"
+                  cols="68" rows="4" name="body" >{if t($art)}{$art->body()}{/if}</textarea>
       </td>
     </tr>
     <tr class="pair">