zikula-modules / EZComments

Add comments to almost anything
22 stars 2 forks source link

Comments with images #35

Open gfr opened 12 years ago

gfr commented 12 years ago

I added a function to EZComments so that a user can also add an image to a comment.

diff -r org/lib/EZComments/Api/User.php mod/lib/EZComments/Api/User.php
221a222,223
>         
>         $args['image'] = isset($args['image']) ? trim($args['image']) : '';
284a287
>           'image'       => $args['image'],
diff -r org/lib/EZComments/Controller/User.php mod/lib/EZComments/Controller/User.php
228a229
>         $image      = isset($args['image'])   ? $args['image']     : $this->request->files->get('image');
290a292,325
>         
>       $fileName = '';
>       
>       if($image['name'] != ''){
>           $fileNameParts = explode('.', $image['name']);
>           $extension = $fileNameParts[count($fileNameParts) - 1];
>           $extension = str_replace('jpeg', 'jpg', strtolower($extension));
>   
>           if($extension != "jpg")
>           {
>               SessionUtil::setVar('ezcomment', serialize($ezcomment));
>               return LogUtil::registerError($this->__('Error! Only .jpg images are allowed!'), null,
>               $redirect."#commentform_{$mod}_{$objectid}");
>           }
>   
>           $basePath = FileUtil::getDataDirectory() . '/EZComments/image/';
>   
>               
>               
>           do {
>               $fileName = md5(uniqid(mt_rand(), TRUE)) . '.' . $extension;
>           }
>           while (file_exists($basePath . $fileName)); // repeat until we have a new name
>   
>           $upload = FileUtil::uploadFile('image', $basePath, $fileName);
>               
>               
>           if($upload !== true){
>               SessionUtil::setVar('ezcomment', serialize($ezcomment));
>               return LogUtil::registerError($this->__('Error during upload!') . ' ' . $upload, null,
>               $redirect."#commentform_{$mod}_{$objectid}");
>           }
> 
>       }
309c344,345
<                                  'anonwebsite' => $anonwebsite));

---
>                                  'anonwebsite' => $anonwebsite,
>                                'image'       => $fileName));
diff -r org/lib/EZComments/Installer.php mod/lib/EZComments/Installer.php
66a67,70
>         // Image upload
>         if(mkdir(FileUtil::getDataDirectory() . '/EZComments/image/') == false){
>             LogUtil::registerError($this->__('Error! Could not create upload directory. (' . FileUtil::getDataDirectory() . '/EZComments/image/)'));
>         }
135a140,144
>                 // Image upload
>                 
>                 if(mkdir(FileUtil::getDataDirectory() . '/EZComments/image/') == false){
>                   LogUtil::registerError($this->__('Error! Could not create upload directory. (' . FileUtil::getDataDirectory() . '/EZComments/image/)'));
>                 }
diff -r org/tables.php mod/tables.php
42c42,43
<         'anonwebsite' => 'anonwebsite'

---
>         'anonwebsite' => 'anonwebsite',
>       'image'     => 'image'
61c62,63
<         'anonwebsite' => "C(255) NOTNULL DEFAULT ''"

---
>         'anonwebsite' => "C(255) NOTNULL DEFAULT ''",
>       'image' => "C(255) NOTNULL DEFAULT ''"
diff -r org/templates/Standard/ezcomments_user_view.tpl mod/templates/Standard/ezcomments_user_view.tpl
54a55,57
>                                 <div class="ezc_image">
>                                     {$comment.image|showImage}
>                                 </div>
105a109,113
>                 </div>
>                 
>                 <div class="z-formrow">
>                     <label for="image">{gt text='Image' domain='module_ezcomments'}</label>
>                     <input name="image" type="file" />
<?php
/**
 * EZComments
 *
 * @copyright (C) EZComments Development Team
 * @link https://github.com/zikula-modules/EZComments
 * @license See license.txt
 */

/**
 * Smarty modifier to show a comment-image
 * 
 * Example
 * 
 *   {$MyVar|showImage}
 * 
 * 
 * @author  Gabriel Freinbichler
 * @since        23.07.2012
 * @param array    $string     the name of the image
 * @return string   html-img tag
 */
function smarty_modifier_showImage($name)
{
    if($name != '')
    {
        return '<img src="' . FileUtil::getDataDirectory() . '/EZComments/image/' . $name . '" />';
    }
    return '';
}
ghost commented 12 years ago

Please could you open a PR with this?

phaidon commented 12 years ago

Is this really the right way? Should not we use a hook for that?

nmpetkov commented 10 years ago

One way to add images to comments, is to hook Scribite (visual editor) to EZcomments, with possibility to upload images (for example CKEditor with KCFinder).

Note: In this case, instead to show comment form under the commented content (with visual editor loaded), it is good idea to display a button only for adding comment. It is not implemented yet in the module, but is not difficult to implement.