]> git.agnieray.net Git - galette.git/commitdiff
Can now change member photo from voir_adherent and ajouter_adherent using drag'n...
authorJohan Cwiklinski <johan@x-tnd.be>
Fri, 25 Nov 2011 19:22:39 +0000 (19:22 +0000)
committerJohan Cwiklinski <johan@x-tnd.be>
Fri, 25 Nov 2011 19:22:39 +0000 (19:22 +0000)
galette/ajax_photo.php [new file with mode: 0644]
galette/classes/picture.class.php
galette/config/paths.inc.php
galette/templates/default/galette.css
galette/templates/default/member.tpl
galette/templates/default/photo_dnd.tpl [new file with mode: 0644]
galette/templates/default/voir_adherent.tpl

diff --git a/galette/ajax_photo.php b/galette/ajax_photo.php
new file mode 100644 (file)
index 0000000..2f9519a
--- /dev/null
@@ -0,0 +1,94 @@
+<?php
+
+/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
+
+/**
+ * Displays a picture
+ *
+ * PHP version 5
+ *
+ * Copyright © 2004-2011 The Galette Team
+ *
+ * This file is part of Galette (http://galette.tuxfamily.org).
+ *
+ * Galette 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 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Galette 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 Galette. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * @category  Main
+ * @package   Galette
+ *
+ * @author    Frédéric Jaqcuot <unknown@unknow.com>
+ * @author    Johan Cwiklinski <johan@x-tnd.be>
+ * @copyright 2004-2011 The Galette Team
+ * @license   http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
+ * @version   SVN: $Id: picture.php 877 2011-06-01 06:08:18Z trashy $
+ * @link      http://galette.tuxfamily.org
+ * @since     Available since 0.62
+ */
+
+/** @ignore */
+require_once 'includes/galette.inc.php';
+
+if ( !$login->isLogged() || !$login->isAdmin() && !$login->isStaff() ) {
+    $log->log(
+        'Trying to display ajax_members.php without appropriate permissions',
+        PEAR_LOG_INFO
+    );
+    die();
+}
+
+require_once 'classes/adherent.class.php';
+
+if ( !isset($_POST)
+    || !isset($_POST['member_id'])
+    || !isset($_POST['file'])
+    || !isset($_POST['filename'])
+    || !isset($_POST['filesize'])
+) {
+    die('Required argument not present.');
+}
+
+$mid = $_POST['member_id'];
+$fsize = $_POST['filesize'];
+$fname = $_POST['filename'];
+$tmpname = GALETTE_TEMPIMAGES_PATH . 'ajax_upload_' . $fname;
+
+$temp = explode('base64,', $_POST['file']);
+$mime = str_replace('data:', '', trim($temp[0], ';'));
+$raw_file = base64_decode($temp[1]);
+
+//write temporary file
+$fp = fopen($tmpname, 'w');
+fwrite($fp, $raw_file);
+fclose($fp);
+
+$adh = new Adherent((int)$mid);
+
+$ret = array();
+$res = $adh->picture->store(
+    array(
+        'name' => $fname,
+        'tmp_name' => $tmpname, 
+        'size' => $fsize
+    ),
+    true
+);
+if ( $res < 0 ) {
+    $ret['result'] = false;
+    $ret['message'] = $adh->picture->getErrorMessage($res);
+} else {
+    $ret['result'] = true;
+}
+
+echo json_encode($ret);
+?>
index f0f8afa39cb9513128673758c128d501c34b04ab..d00d644eeddba6bdd468ae501c519559a87528a2 100644 (file)
@@ -375,11 +375,12 @@ class Picture
     /**
     * Stores an image on the disk and in the database
     *
-    * @param object $file the uploaded file
+    * @param object  $file the uploaded file
+    * @param boolean $ajax If the image cames from an ajax call (dnd)
     *
     * @return true|false result of the storage process
     */
-    public function store($file)
+    public function store($file, $ajax = false)
     {
         /** TODO:
             - fix max size (by preferences ?)
@@ -447,7 +448,11 @@ class Picture
 
         $new_file = $this->store_path .
             $this->id . '.' . $extension;
-        move_uploaded_file($tmpfile, $new_file);
+        if ( $ajax === true ) {
+            rename($tmpfile, $new_file);
+        } else {
+            move_uploaded_file($tmpfile, $new_file);
+        }
 
         // current[0] gives width ; current[1] gives height
         if ( $current[0] > $this->max_width || $current[1] > $this->max_height ) {
index d1cc3d83fd9502910e7428949e354971e3fd8421..843deb9e881bed1262088cd64d38e0814e4fca85 100644 (file)
@@ -82,4 +82,7 @@ if ( !defined('GALETTE_EXPORTS_PATH') ) {
 if ( !defined('GALETTE_PHOTOS_PATH') ) {
     define('GALETTE_PHOTOS_PATH', WEB_ROOT . 'photos/');
 }
+if ( !defined('GALETTE_TEMPIMAGES_PATH') ) {
+    define('GALETTE_TEMPIMAGES_PATH', WEB_ROOT . 'tempimages/');
+}
 ?>
index 76f02c98ebea791b7d36161269718e6328f60daa..8298cee568a92e3dd0378741b79713401769e965 100644 (file)
@@ -1596,6 +1596,10 @@ td.photo img{
        margin-bottom: 5px;
 }
 
+.dndhover {
+    opacity:0.4;
+}
+
 #details_menu{
        padding:0;
        text-align:center;
index 763c80173b947a90e7ff1ade7c0a382317128520..2966de03e50293b7c14cea67f2870855b71297c7 100644 (file)
@@ -40,7 +40,7 @@
     {if !$self_adh}
                                        <p>
                                                <span class="bline">{_T string="Picture:"}</span>
-                                               <img src="{$galette_base_path}picture.php?id_adh={$member->id}&amp;rand={$time}" class="picture" width="{$member->picture->getOptimalWidth()}" height="{$member->picture->getOptimalHeight()}" alt="{_T string="Picture"}"/><br/>
+                                               <img id="photo_adh" src="{$galette_base_path}picture.php?id_adh={$member->id}&amp;rand={$time}" class="picture" width="{$member->picture->getOptimalWidth()}" height="{$member->picture->getOptimalHeight()}" alt="{_T string="Picture"}"/><br/>
         {if $member->hasPicture() eq 1 }
                                                <span class="labelalign"><label for="del_photo">{_T string="Delete image"}</label></span><input type="checkbox" name="del_photo" id="del_photo" value="1"/><br/>
         {/if}
                     {rdelim});
 
                 {rdelim}
+
+                {include file="photo_dnd.tpl"}
             {rdelim});
                </script>
 {/if}
\ No newline at end of file
diff --git a/galette/templates/default/photo_dnd.tpl b/galette/templates/default/photo_dnd.tpl
new file mode 100644 (file)
index 0000000..a201d74
--- /dev/null
@@ -0,0 +1,57 @@
+                //Photo dnd
+                // Check if window.FileReader exists to make
+                // sure the browser supports file uploads
+                if ( typeof(window.FileReader) ) {ldelim}
+                    var _dz = $('#photo_adh');
+
+                    // Add a nice drag effect
+                    _dz[0].ondragover = function() {ldelim}
+                        _dz.addClass('dndhover');
+                        return false;
+                    {rdelim};
+
+                    // Remove the drag effect when stopping our drag
+                    _dz[0].ondragend = function() {ldelim}
+                        _dz.removeClass('dndhover');
+                        return false;
+                    {rdelim};
+
+                    // The drop event handles the file sending
+                    _dz[0].ondrop = function(event) {ldelim}
+                        // Stop the browser from opening the file in the window
+                        event.preventDefault();
+                        _dz.removeClass('dndhover');
+
+                        var file = event.dataTransfer.files[0];
+                        var reader = new FileReader();
+                        reader.readAsDataURL(file);
+
+                        reader.onload = function(evt) {ldelim}
+                            $.ajax({ldelim}
+                                    type: 'POST',
+                                    dataType: 'json',
+                                    url : 'ajax_photo.php',
+                                    data: {ldelim}
+                                        member_id: {$member->id},
+                                        filename: file.name,
+                                        filesize: file.size,
+                                        file: evt.target.result
+                                    {rdelim},
+                                    success: function(res){ldelim}
+                                        if ( res.result == true ) {ldelim}
+                                            d = new Date();
+                                            var _photo = $('#photo_adh');
+                                            _photo.removeAttr('width').removeAttr('height');
+                                            _photo.attr('src', $('#photo_adh')[0].src + '&' + d.getTime());
+                                            alert('{_T string="Member photo has been changed."}');
+                                        {rdelim} else {ldelim}
+                                            alert(res.message);
+                                        {rdelim}
+                                    {rdelim},
+                                error: function() {ldelim}
+                                    alert("{_T string="An error occured sending photo :(" escape="js"}");
+                                {rdelim}
+                            {rdelim});
+                        {rdelim}
+                    {rdelim}
+                {rdelim}
index 4010516127087554c2b1b852b87b755aa5b5c0cc..28424198aa9d91917127ee9dca57d8c30d9f673a 100644 (file)
@@ -61,7 +61,8 @@ We have to use a template file, so Smarty will do its work (like replacing varia
                         src="{$galette_base_path}picture.php?id_adh={$member->id}&amp;rand={$time}"
                         width="{$member->picture->getOptimalWidth()}"
                         height="{$member->picture->getOptimalHeight()}"
-                        alt="{_T string="Picture"}"/>
+                        alt="{_T string="Picture"}"
+                        id="photo_adh"/>
                 </td>
                        </tr>
 {if $member->isCompany()}
@@ -226,3 +227,10 @@ We have to use a template file, so Smarty will do its work (like replacing varia
 
 {include file="display_dynamic_fields.tpl" is_form=false}
        </div>
+{if $login->isAdmin() or $login->isStaff() or $login->login eq $member->login}
+    <script type="text/javascript">
+        $(function() {ldelim}
+            {include file="photo_dnd.tpl"}
+        {rdelim});
+    </script>
+{/if}
\ No newline at end of file