Can add image in announce on x.net.
authorFlorent Bruneau <florent.bruneau@polytechnique.org>
Sat, 8 Mar 2008 11:59:35 +0000 (12:59 +0100)
committerFlorent Bruneau <florent.bruneau@polytechnique.org>
Sat, 8 Mar 2008 11:59:35 +0000 (12:59 +0100)
Do not completely fix #740 since admin page is not yet handled

Signed-off-by: Florent Bruneau <florent.bruneau@polytechnique.org>
classes/plupload.php
modules/xnetgrp.php
templates/xnetgrp/announce-edit.tpl
templates/xnetgrp/form.announce.tpl
upgrade/0.9.16/05_xnet_announce_photo.sql [new file with mode: 0644]

index 133b55d..155a8a4 100644 (file)
@@ -228,6 +228,10 @@ class PlUpload
         $array = getimagesize($this->filename);
         $array[2] = @$map[$array[2]];
         if (!$array[2]) {
+            list($image, $type) = explode('/', $array['mime']);
+            $array[2] = $type;
+        }
+        if (!$array[2]) {
             trigger_error('unknown image type', E_USER_NOTICE);
             return null;
         }
index 80ef915..eeeee1f 100644 (file)
@@ -104,6 +104,7 @@ class XnetGrpModule extends PLModule
             '%grp/rss'             => $this->make_hook('rss', AUTH_PUBLIC, 'user', NO_HTTPS),
             '%grp/announce/new'    => $this->make_hook('edit_announce', AUTH_MDP,  'groupadmin'),
             '%grp/announce/edit'   => $this->make_hook('edit_announce', AUTH_MDP,  'groupadmin'),
+            '%grp/announce/photo'  => $this->make_hook('photo_announce', AUTH_PUBLIC),
             '%grp/admin/announces' => $this->make_hook('admin_announce', AUTH_MDP, 'groupadmin'),
         );
     }
@@ -134,7 +135,8 @@ class XnetGrpModule extends PLModule
                             Env::i('unread'), S::i('uid'));
                 pl_redirect("#art" . Env::i('unread'));
             }
-            $arts = XDB::iterator("SELECT a.*, u.nom, u.prenom, u.promo, l.alias AS forlife
+            $arts = XDB::iterator("SELECT a.*, u.nom, u.prenom, u.promo, l.alias AS forlife,
+                                          FIND_IN_SET('photo', a.flags) AS photo
                                      FROM groupex.announces AS a
                                INNER JOIN auth_user_md5 AS u USING(user_id)
                                INNER JOIN aliases AS l ON (u.user_id = l.id AND l.type = 'a_vie')
@@ -155,7 +157,7 @@ class XnetGrpModule extends PLModule
                                    S::i('uid'), $globals->asso('id'), S::i('promo'), S::i('promo'));
             $page->assign('article_index', $index);
         } else {
-            $arts = XDB::iterator("SELECT a.*, u.nom, u.prenom, u.promo
+            $arts = XDB::iterator("SELECT a.*, u.nom, u.prenom, u.promo, FIND_IN_SET('photo', a.flags) AS photo
                                      FROM groupex.announces AS a
                                INNER JOIN auth_user_md5 AS u USING(user_id)
                                     WHERE asso_id = {?} AND peremption >= CURRENT_DATE()
@@ -1047,6 +1049,48 @@ class XnetGrpModule extends PLModule
         $page->assign('rss', $rss);
     }
 
+    private function upload_image(PlatalPage &$page, PlUpload &$upload)
+    {
+        if (@!$_FILES['image']['tmp_name'] && !Env::v('image_url')) {
+            return true;
+        }
+        if (!$upload->upload($_FILES['image'])  && !$upload->download(Env::v('image_url'))) {
+            $page->trig('Impossible de télécharger l\'image');
+            return false;
+        } elseif (!$upload->isType('image')) {
+            $page->trig('Le fichier n\'est pas une image valide au format JPEG, GIF ou PNG.');
+            $upload->rm();
+            return false;
+        } elseif (!$upload->resizeImage(200, 300, 100, 100, 32284)) {
+            $page->trig('Impossible de retraiter l\'image');
+            return false;
+        }
+        return true;
+    }
+
+    function handler_photo_announce(&$page, $eid = null) {
+        if ($eid) {
+            $res = XDB::query("SELECT * FROM groupex.announces_photo WHERE eid = {?}", $eid);
+            if ($res->numRows()) {
+                $photo = $res->fetchOneAssoc();
+                header('Content-Type: image/' . $photo['attachmime']);
+                echo $photo['attach'];
+                exit;
+            }
+        } else {
+            $upload = new PlUpload(S::v('forlife'), 'xnetannounce');
+            if ($upload->exists() && $upload->isType('image')) {
+                header('Content-Type: ' . $upload->contentType());
+                echo $upload->getContents();
+                exit;
+            }
+        }
+        global $globals;
+        header('Content-Type: image/png');
+        echo file_get_contents($globals->spoolroot . '/htdocs/images/logo.png');
+        exit;
+    }
+
     function handler_edit_announce(&$page, $aid = null)
     {
         global $globals, $platal;
@@ -1054,7 +1098,8 @@ class XnetGrpModule extends PLModule
         $page->assign('new', is_null($aid));
         $art = array();
 
-        if (Post::v('valid') == 'Visualiser' || Post::v('valid') == 'Enregistrer') {
+        if (Post::v('valid') == 'Visualiser' || Post::v('valid') == 'Enregistrer'
+            || Post::v('valid') == 'Supprimer l\'image') {
             if (!is_null($aid)) {
                 $art['id'] = $aid;
             }
@@ -1072,6 +1117,8 @@ class XnetGrpModule extends PLModule
             $art['xorg']       = Post::has('xorg');
             $art['nl']         = Post::has('nl');
             $art['event']      = Post::v('event');
+            $upload     = new PlUpload(S::v('forlife'), 'xnetannounce');
+            $this->upload_image($page, $upload);
 
             $art['contact_html'] = $art['contacts'];
             if ($art['event']) {
@@ -1091,36 +1138,59 @@ class XnetGrpModule extends PLModule
                 $page->trig("L'article doit avoir un titre et un contenu");
                 Post::kill('valid');
             }
+
+            if (Post::v('valid') == 'Supprimer l\'image') {
+                $upload->rm();
+                Post::kill('valid');
+            }
+            $art['photo'] = $upload->exists();
         }
 
         if (Post::v('valid') == 'Enregistrer') {
             $promo_min = ($art['public'] ? 0 : $art['promo_min']);
             $promo_max = ($art['public'] ? 0 : $art['promo_max']);
+            $flags = array();
+            if ($art['public']) {
+                $flags[] = 'public';
+            }
+            if ($art['photo']) {
+                $flags[] = 'photo';
+            }
+            $flags = implode(',', $flags);
             if (is_null($aid)) {
                 $fulltext = $art['texte'];
                 if (!empty($art['contact_html'])) {
                     $fulltext .= "\n\n'''Contacts :'''\\\\\n" . $art['contact_html'];
                 }
-                $post = null;
+                $post = null;/*
                 if ($globals->asso('forum')) {
                     require_once 'banana/forum.inc.php';
                     $banana = new ForumsBanana(S::v('forlife'));
                     $post = $banana->post($globals->asso('forum'), null,
                                           $art['titre'], MiniWiki::wikiToText($fulltext, false, 0, 80));
-                }
+                }*/
                 XDB::query("INSERT INTO groupex.announces
                                  (user_id, asso_id, create_date, titre, texte, contacts,
                                    peremption, promo_min, promo_max, flags, post_id)
                             VALUES ({?}, {?}, NOW(), {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?})",
                            S::i('uid'), $globals->asso('id'), $art['titre'], $art['texte'], $art['contact_html'],
-                           $art['peremption'], $promo_min, $promo_max, $art['public'] ? 'public' : '', $post);
+                           $art['peremption'], $promo_min, $promo_max, $flags, $post);
                 $aid = XDB::insertId();
+                if ($art['photo']) {
+                    list($imgx, $imgy, $imgtype) = $upload->imageInfo();
+                    XDB::execute("INSERT INTO groupex.announces_photo
+                                          SET eid = {?}, attachmime = {?}, x = {?}, y = {?}, attach = {?}",
+                                 $aid, $imgtype, $imgx, $imgy, $upload->getContents());
+                }
                 if ($art['xorg']) {
                     require_once('validations.inc.php');
                     $article = new EvtReq("[{$globals->asso('nom')}] " . $art['titre'], $fulltext,
-                                    $art['promo_min'], $art['promo_max'], $art['peremption'], "", S::v('uid'));
+                                    $art['promo_min'], $art['promo_max'], $art['peremption'], "", S::v('uid'),
+                                    $upload);
                     $article->submit();
                     $page->trig("L'affichage sur la page d'accueil de Polytechnique.org est en attente de validation");
+                } else if ($upload && $upload->exists()) {
+                    $upload->rm();
                 }
                 if ($art['nl']) {
                     require_once('validations.inc.php');
@@ -1135,7 +1205,7 @@ class XnetGrpModule extends PLModule
                                    promo_min={?}, promo_max={?}, flags={?}
                              WHERE id={?} AND asso_id={?}",
                            $art['titre'], $art['texte'], $art['contacts'], $art['peremption'],
-                           $promo_min, $promo_max,  $art['public'] ? 'public' : '',
+                           $promo_min, $promo_max,  $flags,
                            $art['id'], $globals->asso('id'));
             }
         }
@@ -1171,6 +1241,7 @@ class XnetGrpModule extends PLModule
 
         $art['contact_html'] = @MiniWiki::WikiToHTML($art['contact_html']);
         $page->assign('art', $art);
+        $page->assign_by_ref('upload', $upload);
     }
 
     function handler_admin_announce(&$page)
index 5e66122..36fcadc 100644 (file)
@@ -41,7 +41,7 @@ function visibilityChange(box)
 </div>
 {/if}
 
-<form method="post" action="{$platal->ns}announce/{if $new}new{else}edit/{$art.id}{/if}">
+<form method="post" action="{$platal->ns}announce/{if $new}new{else}edit/{$art.id}{/if}" enctype="multipart/form-data">
 <div>
   <table class="bicol">
     <tr>
@@ -98,6 +98,29 @@ function visibilityChange(box)
       </td>
     </tr>
     <tr style="border-top: 1px solid gray">
+      <td class="titre">Illustration</td>
+      <td>
+        {if $art.id && $art.img}
+        <div style="float: left; text-align: center">
+          <em>Image actuelle</em><br />
+          <img src="{$platal->ns}announce/photo/{$art.id}" alt="Image actuelle" /><br />
+          <input type="submit" name="valid" value="Pas d'image" />
+        </div>
+        {/if}
+        {if $upload && $art.photo}
+        <div style="float: right; text-align: center">
+          <em>Nouvelle image</em><br />
+          <img src="{$platal->ns}announce/photo" alt="Nouvelle Image" /><br />
+          <input type="submit" name="valid" value="Supprimer l'image" />
+        </div>
+        {/if}
+        <div style="clear: both">
+          Choisir un fichier&nbsp;: <input type="file" name="image" id="image" /><br />
+          Indiquer une adresse&nbsp;: <input type="text" name="image_url" id="image_url" value="" />
+        </div>
+      </td>
+    </tr>
+    <tr style="border-top: 1px solid gray">
       <td colspan="2" class="center">
         <a href="wiki_help/notitle" class="popup3">
           {icon name=information title="Syntaxe wiki"} Voir la liste des marqueurs de mise en forme autorisés
@@ -121,7 +144,7 @@ function visibilityChange(box)
         <select name="event">
           <option value="" {if !$art.event}selected="selected"{/if}>-- Aucun --</option>
           {iterate item=evt from=$events}
-          <option value="{$evt.short_name|default:$evt.eid}" 
+          <option value="{$evt.short_name|default:$evt.eid}"
             {if $art.event eq $evt.short_name|default:$evt.eid}selected="selected"{/if}>
             {$evt.intitule}
           </option>
@@ -179,7 +202,7 @@ function visibilityChange(box)
     {/if}
     <input type="submit" name="valid" value="Visualiser" /><br />
     {if $art.texte}
-    <input type="submit" name="valid" value="Enregistrer" /> 
+    <input type="submit" name="valid" value="Enregistrer" />
     {if !$new}
     <input type="submit" name="valid" value="Annuler" />
     {/if}
index c1d6a49..961bd54 100644 (file)
@@ -22,7 +22,7 @@
 
 <table class="tinybicol" id="art{$art.id}">
   <tr>
-    <th>
+    <th {if $art.photo}colspan="2"{/if}>
       {if $is_logged && !$admin}
       <div style="float: right">
         {if $is_admin}
     </th>
   </tr>
   <tr>
+    {if $art.photo}
+    <td rowspan="{if ($is_logged || $admin) && $art.contacts}3{else}2{/if}" style="width: 100px">
+      <img src="{$platal->ns}announce/photo/{$art.id}" alt="{$art.titre}" style="width: 100px" /> 
+    </td>
+    {/if}
     <td style="padding-bottom: 1em">
       {$art.texte|miniwiki|smarty:nodefaults}
     </td>
@@ -58,7 +63,7 @@
   </tr>
   {/if}
   <tr class="pair">
-    <td>
+    <td {if $art.photo}colspan="2"{/if}>
       <div style="float: right">
       <small>
         Annonce proposée par
diff --git a/upgrade/0.9.16/05_xnet_announce_photo.sql b/upgrade/0.9.16/05_xnet_announce_photo.sql
new file mode 100644 (file)
index 0000000..97d3d40
--- /dev/null
@@ -0,0 +1,16 @@
+use groupex;
+
+CREATE TABLE `announces_photo` (
+  `eid` smallint(4) unsigned NOT NULL,
+  `attachmime` enum('jpeg','png','gif') NOT NULL default 'jpeg',
+  `attach` blob NOT NULL,
+  `x` smallint(5) unsigned NOT NULL default '0',
+  `y` smallint(5) unsigned NOT NULL default '0',
+  PRIMARY KEY  (`eid`)
+) CHARSET=utf8;
+
+alter table groupex.announces change column flags flags set('public', 'photo') not null;
+
+use x4dat;
+
+# vim:set syntax=mysql: