Ajaxify event submission form
authorx2003bruneau <x2003bruneau@839d8a87-29fc-0310-9880-83ba4fa771e5>
Sun, 25 Feb 2007 23:11:48 +0000 (23:11 +0000)
committerx2003bruneau <x2003bruneau@839d8a87-29fc-0310-9880-83ba4fa771e5>
Sun, 25 Feb 2007 23:11:48 +0000 (23:11 +0000)
git-svn-id: svn+ssh://murphy/home/svn/platal/trunk@1516 839d8a87-29fc-0310-9880-83ba4fa771e5

htdocs/javascript/ajax.js
include/xorg.misc.inc.php
modules/events.php
templates/events/admin.tpl
templates/events/form.tpl [moved from templates/include/form.evenement.tpl with 71% similarity]
templates/events/preview.tpl [new file with mode: 0644]
templates/events/submit.tpl

index 5cff50b..2bf3c02 100644 (file)
@@ -55,7 +55,7 @@ Ajax = {
                        if (obj != null) {
                                document.getElementById(obj).innerHTML = Ajax.xml_client.responseText;
                         }
-                      if (func != null) {
+                        if (func != null) {
                                func(Ajax.xml_client.responseText);
                        }
                     } else if (Ajax.xml_client.status == 403) {
index e98250e..d4154d8 100644 (file)
@@ -19,7 +19,8 @@
  *  59 Temple Place, Suite 330, Boston, MA  02111-1307  USA                *
  ***************************************************************************/
 
-function quoted_printable_encode($input, $line_max = 76) {
+function quoted_printable_encode($input, $line_max = 76)
+{
     $lines = preg_split("/(?:\r\n|\r|\n)/", $input);
     $eol = "\n";
     $linebreak = "=0D=0A=\n    ";
@@ -72,6 +73,14 @@ function isvalid_email_redirection($email) {
        !preg_match("/@(polytechnique\.(org|edu)|melix\.(org|net)|m4x\.org)$/", $email);
 }
 
+/** Check if the string is utf8
+ */
+function is_utf8($s)
+{
+    return @iconv('utf-8', 'utf-8', $s) == $s;
+}
+
+
 /** Remove accent from a string and replace them by the nearest letter
  */
 global $lc_convert, $uc_convert;
index 3881aa0..894d5c2 100644 (file)
@@ -26,6 +26,7 @@ class EventsModule extends PLModule
         return array(
             'events'         => $this->make_hook('ev',        AUTH_COOKIE),
             'rss'            => $this->make_hook('rss', AUTH_PUBLIC),
+            'events/preview' => $this->make_hook('preview', AUTH_PUBLIC, '', NO_AUTH),
             'events/submit'  => $this->make_hook('ev_submit', AUTH_MDP),
             'admin/events'   => $this->make_hook('admin_events',     AUTH_MDP, 'admin'),
 
@@ -166,11 +167,35 @@ class EventsModule extends PLModule
                   WHERE  u.user_id = {?} AND FIND_IN_SET(e.flags, "valide")
                                          AND peremption >= NOW()', $uid);
         $page->assign('rss', $rss);
-    }   
+    }
+
+    function handler_preview(&$page)
+    {
+        require_once('url_catcher.inc.php');
+        $page->changeTpl('events/preview.tpl', NO_SKIN);
+        $texte = Get::v('texte');
+        if (!is_utf8($texte)) {
+            $texte = utf8_encode($texte);
+        }
+        if (strpos($_SERVER['HTTP_REFERER'], 'admin') === false) {
+            $texte = url_catcher(pl_entities($texte));
+        }
+        $titre = Get::v('titre');
+        if (!is_utf8($titre)) {
+            $titre = utf8_encode($titre);
+        }
+        $page->assign('texte_html', $texte);
+        $page->assign('titre', $titre);
+        header('Content-Type: text/html; charset=utf-8');
+    }
 
     function handler_ev_submit(&$page)
     {
         $page->changeTpl('events/submit.tpl');
+        $page->addJsLink('ajax.js');
+        
+        require_once('wiki.inc.php');
+        wiki_require_page('Xorg.Annonce');
 
         $titre      = Post::v('titre');
         $texte      = Post::v('texte');
@@ -202,7 +227,7 @@ class EventsModule extends PLModule
 
         if ($action && (!trim($texte) || !trim($titre))) {
             $page->trig("L'article doit avoir un titre et un contenu");
-        } elseif ($action == 'Confirmer') {
+        } elseif ($action) {
                $texte = $texte_catch_url;
             require_once 'validations.inc.php';
             $evtreq = new EvtReq($titre, $texte, $promo_min, $promo_max,
@@ -257,6 +282,7 @@ class EventsModule extends PLModule
     function handler_admin_events(&$page, $action = 'list', $eid = null) 
     {
         $page->changeTpl('events/admin.tpl');
+        $page->addJsLink('ajax.js');
         $page->assign('xorg_title','Polytechnique.org - Administration - Evenements');
         $page->register_modifier('hde', 'html_entity_decode');
 
@@ -288,6 +314,7 @@ class EventsModule extends PLModule
             list($titre, $texte, $peremption, $promo_min, $promo_max) = $res->fetchOneRow();
             $page->assign('titre',$titre);
             $page->assign('texte',$texte);
+            $page->assign('texte_html', pl_entity_decode($texte));
             $page->assign('promo_min',$promo_min);
             $page->assign('promo_max',$promo_max);
             $page->assign('peremption',$peremption);
index 23ee183..bb95087 100644 (file)
@@ -41,7 +41,7 @@
 
 {if $action eq 'edit'}
 
-{include file="include/form.evenement.tpl"}
+{include file="events/form.tpl"}
 
 {else}
 
similarity index 71%
rename from templates/include/form.evenement.tpl
rename to templates/events/form.tpl
index 7afc495..8a370f3 100644 (file)
 {*                                                                        *}
 {**************************************************************************}
 
+<script type="text/javascript">
+  {literal}
+  function updatePreview()
+  {
+    var titre = document.getElementById('titre').value;
+    var texte = document.getElementById('texte').value;
+
+    if (titre == '' || texte == '') {
+      document.getElementById('valid').style.display = 'none';
+      document.getElementById('info').style.display = '';
+    } else {
+      document.getElementById('valid').style.display = '';
+      document.getElementById('info').style.display = 'none';
+    }
+    var page  = 'events/preview?titre=' + titre + '&texte=' + texte;
+    if (is_IE) {
+      {/literal}
+      page = "{$globals->baseurl}/" + page;
+      {literal}
+    }
+    Ajax.update_html('preview', page, null);
+    return false;
+  }
+  {/literal}
+</script>
+
+<div id="preview">
+{include file="events/preview.tpl"}
+</div>
+<br />
+
 <form action="{$platal->path}" method="post">
   <table class="bicol">
     <tr>
-      <th colspan="2">Contenu du message</th>
-    </tr>
-    {if strlen(trim($texte))}
-    <tr class="pair">
-      <td colspan="2" style="border-bottom: 1px dotted #777">
-        {$texte|smarty:nodefaults|nl2br}
-      </td>
+      <th colspan="2">Contenu de l'annonce</th>
     </tr>
-    {/if}
     <tr>
       <td class="titre">Titre</td>
       <td>
-        <input type="text" name="titre" size="50" maxlength="200" value="{$titre}" />
+        <input type="text" name="titre" id="titre" size="50" maxlength="200" value="{$titre}" />
       </td>
     </tr>
     <tr>
       <td class="titre">Texte</td>
-      <td><textarea name="texte" rows="10" cols="60" onfocus="update_texte_count(this.form)">{$texte}</textarea></td>
+      <td><textarea name="texte" id="texte" rows="10" cols="60" onfocus="update_texte_count(this.form)">{$texte}</textarea></td>
     </tr>
     <tr>
       <td colspan="2" class="smaller">
           }
           {/literal}
         </script>
-        Essaie de faire un <strong>texte court</strong>, une annonce ne doit pas excéder 800 caractères soit une douzaine de ligne. Tu en es déjà à <input type='text' name='texte_count' size="4"/> caractères.
+        Essaie de faire un <strong>texte court</strong>, une annonce ne doit pas excéder 800 caractères soit une douzaine de ligne. Tu en es déjà à <input type='text' name='texte_count' size="4" /> caractères.
       </td>
     </tr>
-    <tr>
-      <th colspan="2">Informations complémentaires</th>
-    </tr>
   </table>
 
+  <div class="center">
+    <input type="submit" name="preview" value="Aperçu" onclick="updatePreview(); return false;" />
+  </div>
+  <p id="info" {if trim($texte) && trim($titre)}style="display: none"{/if}>
+    Le bouton de confirmation n'apparaît que si l'aperçu est concluant.
+  </p>
+  <p class="erreur">
+    N'oublie pas de remplir suivantes&nbsp;:
+  </p>
+
   <table class="bicol">
+    <tr>
+      <th colspan="2">Informations complémentaires</th>
+    </tr>
     <tr class="pair">
       <td colspan="2">
         Tu peux limiter la visibilité de ton annonce aux camarades de certaines promotions :
     </tr>
   </table>
 
-  <div class="center">
+  <div class="center" {if !trim($texte) || !trim($titre)}style="display: none"{/if} id="valid">
     <input type="hidden" name="evt_id" value="{$smarty.post.evt_id}" />
     <input type="submit" name="action" value="Proposer" />
   </div>
diff --git a/templates/events/preview.tpl b/templates/events/preview.tpl
new file mode 100644 (file)
index 0000000..18058ed
--- /dev/null
@@ -0,0 +1,30 @@
+{**************************************************************************}
+{*                                                                        *}
+{*  Copyright (C) 2003-2007 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               *}
+{*                                                                        *}
+{**************************************************************************}
+
+{if trim($titre) || trim($texte_html)}
+<table class="bicol">
+  <tr><th><em>Aperçu de : </em>{$titre|nl2br}</th></tr>
+  <tr class="pair"><td>{$texte_html|nl2br|smarty:nodefaults}</td></tr>
+</table>
+{/if}
+
+{* vim:set et sw=2 sts=2 sws=2 enc=utf-8: *}
index 29df338..44a71db 100644 (file)
 
 <h1>Proposition d'information événementielle</h1>
 
-{if $action eq "proposer" && trim($titre) && trim($texte)}
-
-<p>
-Voici ton annonce :
-</p>
-
-<table class="bicol">
-  <tr><th>{$titre|nl2br}</th></tr>
-  <tr><td>{$texte_html|nl2br|smarty:nodefaults}</td></tr>
-</table>
-
-<p>
-Ce message est à destination
-{if $promo_min || $promo_max}
-des promotions {if $promo_min}X{$promo_min}{/if} {if $promo_max}jusqu'à X{$promo_max}{else} et plus{/if}
-{else}
-de toutes les promotions
-{/if}
-et sera affiché sur la page d'accueil jusqu'au {$peremption|date_format}.
-</p>
-
-{if $valid_mesg}
-<p>
-Tu as ajouté le message suivant à l'intention du validateur : {$valid_mesg|nl2br}
-</p>
-{/if}
-
-<form action="events/submit" method="post">
-  <div>
-    <input type="hidden" name="titre"      value="{$titre}" />
-    <input type="hidden" name="texte"      value="{$texte}" />
-    <input type="hidden" name="promo_min"  value="{$promo_min}" />
-    <input type="hidden" name="promo_max"  value="{$promo_max}" />
-    <input type="hidden" name="peremption" value="{$peremption}" />
-    <input type="hidden" name="valid_mesg" value="{$valid_mesg}" />
-    <input type="submit" name="action"     value="Confirmer" />
-    <input type="submit" name="action"     value="Modifier" />
-  </div>
-</form>
-
-
-{elseif $action eq "confirmer"}
-
 {if $ok}
 <p>
 Ta proposition a bien été enregistrée, un administrateur va se charger de la valider aussi rapidement que possible.
@@ -76,15 +33,10 @@ Merci pour ta contribution à la vie du site!
 <a href="events">Retour à la page d'accueil</a>
 </p>
 {else}
-<p class="erreur">
-Une erreur s'est produite pendant l'enregistrement de ta proposition.
-Merci de nous <a href="Xorg/NousContacter">contacter</a>!
-</p>
-{/if}
 
-{else}
+{include file="events/form.tpl"}
 
-{include file="include/form.evenement.tpl"}
+{include file="../spool/wiki.d/cache_Xorg.Annonce.tpl included=1}
 
 {/if}