Ignore:
Timestamp:
Jan 7, 2013, 10:26:35 PM (11 years ago)
Author:
julien1311
Message:

[piwecard] various improvements

File:
1 edited

Legend:

Unmodified
Added
Removed
  • extensions/Piwecard/include/ecard.class.php

    r19924 r19956  
    11<?php
    2 /*
    3  * Plugin Name: Piwecard
    4  * File :  ecard.php
    5  */
    6  
    72global $user, $conf;
    83
    94class ecard {
    10         var $debug = false;
    11         var $my_config  ;
    12         var $infos = array();   // contains all the info of the ecard : message, subject....
     5        var $my_config;
    136        var $user_groups = array();
    147       
    158        // Class constructor
    16         function ecard() {
     9        function __construct() {
    1710                $this->load_config();
    1811        }
    19 
    2012       
    2113        // Load general configuration from config_database
    2214        function load_config() {
    23                 $query = '
    24                   SELECT value
    25                   FROM '.CONFIG_TABLE.'
    26                   WHERE param = \'ecard\'
    27                 ;';
    28 
     15                $query = 'SELECT value FROM '.CONFIG_TABLE.' WHERE param="piwecard";';
    2916                $result = pwg_query($query);
    3017
     
    4835
    4936        // Save  general configuration to config_database
    50         function save_config()
    51         {
    52                 $query = '
    53                   REPLACE INTO '.CONFIG_TABLE.'
    54                   VALUES(
    55                         \'ecard\',
    56                         \''.serialize($this->my_config).'\',
    57                         \'Configuration ecard\')
    58                 ;';
    59 
     37        function save_config() {
     38                $query = 'UPDATE '.CONFIG_TABLE.' SET value="'.serialize($this->my_config).'" WHERE param="piwecard";';
    6039                $result = pwg_query($query);
    6140
     
    7150                global $user;
    7251               
    73                 $query = 'SELECT group_id FROM ' . USER_GROUP_TABLE . ' WHERE user_id = ' . $user['id'] . ';';
     52                $query = 'SELECT group_id FROM ' . USER_GROUP_TABLE . ' WHERE user_id=' . $user['id'] . ';';
    7453
    7554                $result = pwg_query($query);
     
    10786
    10887        // NB of days between 2 dates "AAAA-MM-JJ HH:hh:ss"
    109         function NbJours($debut, $fin) {
     88        /* function NbJours($debut, $fin) {
    11089                $tDeb = explode("-", substr($debut,0,strpos($debut, ' ')));
    11190                $tFin = explode("-", substr($fin,0,strpos($fin, ' ')));
     
    131110
    132111                return($tFin);
    133         }
     112        } */
    134113       
    135114        function parse($data, $_POST = NULL, $image_element = NULL) {
     
    138117                $patterns = array();
    139118                $replacements = array();
    140                 foreach ($ecard_parse as $key => $value)
    141                 {
     119                foreach ($ecard_parse as $key => $value) {
    142120                        array_push($patterns, $key);
    143121                        array_push($replacements, $value);
     
    149127        // Get the number of ecard in the database
    150128        function get_nb_ecard() {
    151                 $query = 'SELECT COUNT(DISTINCT numero) as nb FROM '.ECARD_TABLE
    152                                 . ' ORDER BY date'
    153                                 .';';
     129                $query = 'SELECT COUNT(DISTINCT id) AS nb FROM '.ECARD_TABLE.' ORDER BY date_creation;';
    154130                $result = pwg_query($query);
     131               
    155132                if ($result) {
    156133                        $nb=mysql_fetch_assoc($result);
    157134                        return $nb['nb'];
    158                 }
    159                 else
     135                } else
    160136                        return 0;
    161137        }
     
    163139        // Get the number of valid ecard in the database
    164140        function get_nb_valid_ecard() {
    165                 $query = 'SELECT numero,date,duration  FROM '.ECARD_TABLE
    166                                 .';';
     141                $query = 'SELECT COUNT(DISTINCT id) AS nb FROM '.ECARD_TABLE.' WHERE date_validity IS NULL OR date_validity > NOW();';
    167142                $result = pwg_query($query);
    168                 $count = 0;
    169                 while($ecard_info = mysql_fetch_assoc($result)) {
    170                         if ($ecard_info['duration'] == 0 OR $this->NbJours($ecard_info['date'], date("Y-m-d H:m:s")) <= $ecard_info['duration'])        // activ  ecard
    171                                 $count++;
    172                 }
    173                 return $count;
     143               
     144                if ($result) {
     145                        $nb=mysql_fetch_assoc($result);
     146                        return $nb['nb'];
     147                } else
     148                        return 0;
    174149        }
    175150       
     
    178153        function get_ecard($ecard_id = null) {
    179154                if ($ecard_id!== null) {
    180                         $query = '
    181                                 SELECT *
    182                                 FROM ' . ECARD_TABLE .'
    183                                 WHERE numero ="' . $ecard_id . '"
    184                                 LIMIT 1
    185                         ';
     155                        $query = 'SELECT * FROM ' . ECARD_TABLE .' WHERE id ="' . $ecard_id . '" LIMIT 1;';
    186156
    187157                        $result = pwg_query($query);
     
    191161                                return false;
    192162                }
    193 
    194         }
    195 
    196         // Get ecard information into array
    197         function is_valid($ecard_id = null, $param_date=false) {
    198                 if ($ecard_id!== null) {
     163        }
     164
     165        function is_valid($ecard_id = null) {
     166                if (isset($ecard_id)) {
    199167                        $ecard_info = $this->get_ecard($ecard_id);
    200                         if ($ecard_info != false)
    201                         {
    202                        
     168                        if (isset($ecard_info)) {
    203169                                // Valid duration for an ecard
    204                                 $duration = ($param_date ? $this->my_config['activ'] : $ecard_info['duration']);
    205                                 if ($this->debug)
    206                                 {
    207                                 foreach ($ecard_info as $i=>$v)
    208                                         echo "ecard[".$i."]=".$v." \ ";
    209                                 echo "NBjours = ".$this->NbJours($ecard_info['date'], date("Y-m-d H:m:s"))." -";
    210                                 echo "Activenb=".$this->my_config['activ']." -";
    211                                 }
     170                                $date_validity = $ecard_info['date_validity'];
    212171                               
    213                                
    214                                 if (isset ($ecard_info)
    215                                         AND $duration != 0              // 0 means always activ
    216                                         AND ($this->NbJours($ecard_info['date'], date("Y-m-d H:m:s")) > $duration)      // Inactiv ecard
    217                                         )
    218                                 {
    219                                         return false;
     172                                if (isset($date_validity)) {
     173                                        $now = new DateTime(date("Y-m-d H:i:s"));
     174                                        $date_validity = new DateTime($date_validity);
     175                                        if ($date_validity > $now)
     176                                                return true;
     177                                        else
     178                                                return false;
    220179                                } else {
    221180                                        return true;
    222181                                }
    223                         }
    224                         else
     182                        } else
    225183                                return false;
    226184                } else {
    227                         return true;
    228                 }
    229         }
    230 
     185                        return false;
     186                }
     187        }
    231188       
    232189        // delete one ecard
    233190        // force to delete valid ecard
    234         function delete_ecard($ecard_id = null, $force = false) {
    235                 if ($ecard_id!== null) {
    236                         $ecard_info = $this->get_ecard($ecard_id);
    237                         if ($this->debug)
    238                         {
    239                         foreach ($ecard_info as $i=>$v)
    240                                 echo "ecard[".$i."]=".$v." \ ";
    241                         }
    242                         if (isset ($ecard_info)
    243                                 and ( ($this->NbJours($ecard_info['date'], date("Y-m-d H:m:s")) > $this->my_config['activ'])    // Inactiv ecard
    244                                           OR $force                                                                                                                                                                     // Or force to delete even if activ
    245                                          )
    246                                 )
    247                                
    248                         $query = '
    249                                 DELETE
    250                                 FROM ' . ECARD_TABLE .'
    251                                 WHERE numero ="' . $ecard_id  . '"
    252                                 ';
     191        function delete_ecard($ecard_id = null) {
     192                if (isset($ecard_id)) {
     193                        $query = 'DELETE FROM ' . ECARD_TABLE .' WHERE id="' . $ecard_id  . '";';
    253194                        pwg_query($query);
    254                 }
    255                 else
     195                } else
    256196                        return false;
    257197        }
     
    259199        // Delete all invalid ecard
    260200        function delete_allinvalid_ecard() {
    261                 $date = $this->AjoutJours(date("Y-m-d H:m:s"), $this->my_config['activ'], true);
    262 
    263                 $query = '
    264                                 DELETE
    265                                 FROM ' . ECARD_TABLE .'
    266                                 WHERE date < "' . $date  . '"
    267                                 ';
    268 
     201                $query = 'DELETE FROM ' . ECARD_TABLE .' WHERE date_validity < NOW();';
    269202                pwg_query($query);
    270        
    271203        }
    272204       
     
    324256                               
    325257                                // author of the photo
    326                                 $authorname = "";
    327                                 $query = '
    328                                                 SELECT author
    329                                                 FROM '.IMAGES_TABLE.'
    330                                                 WHERE id = '.$page['image_id']
    331                                                 .' LIMIT 1'
    332                                                 .';';
     258                                $query = 'SELECT author FROM '.IMAGES_TABLE.' WHERE id = '.$page['image_id'].' LIMIT 1;';
    333259                                $result = pwg_query($query);
    334260                                if (isset($result)) {
     
    367293                                // Template informations
    368294                                $template->assign('ecard', array(
    369                                                 'subject' => l10n('ecard_send_title'),
    370                                                 'message' => l10n('ecard_send_message'),
    371                                                 'sender_name' => $user['username'],
    372                                                 'sender_email' => $user['email'],
    373                                                 'recipient_name' => l10n('ecard_send_dest_name'),
    374                                                 'recipient_email' => l10n('ecard_send_dest_mail'),
    375                                                 'copy'  => $this->my_config['send_copy'] ? 'checked="checked"' : '',
    376                                                 'changemail' => ($this->my_config['expmail_change'] ? '' : 'disabled="disabled"')
     295                                                'subject'                       => l10n('ecard_send_title'),
     296                                                'message'                       => l10n('ecard_send_message'),
     297                                                'sender_name'           => $user['username'],
     298                                                'sender_email'          => $user['email'],
     299                                                'recipient_name'        => l10n('ecard_send_dest_name'),
     300                                                'recipient_email'       => l10n('ecard_send_dest_mail'),
     301                                                'copy'                          => $this->my_config['send_copy'] ? 'checked="checked"' : '',
     302                                                'changemail'            => ($this->my_config['expmail_change'] ? '' : 'disabled="disabled"')
    377303                                                ));
    378304
     
    421347
    422348                                        $insert = array(
    423                                                                 'numero' => $image_element['next_element_id'],
    424                                                                 'nomexp' => $_POST['ecard_sender_name'],
    425                                                                 'nomdest' => $_POST['ecard_recipient_name'],
    426                                                                 'adrexp' => $_POST['ecard_sender_email'],
    427                                                                 'adrdest' => $_POST['ecard_recipient_email'],
    428                                                                 'sujet' => $_POST['ecard_subject'],
    429                                                                 'message' => $_POST['ecard_message'],
    430                                                                 'image' => $image_element['id'],
    431                                                                 'date' => date("Y-m-d H:i:s"),
    432                                                                 'duration' => (isset($_POST['ecard_validity']) ? $_POST['ecard_validity'] : $this->my_config['activ']),
     349                                                                'id'                            => $image_element['next_element_id'],
     350                                                                'sender_name'           => $_POST['ecard_sender_name'],
     351                                                                'recipient_name'        => $_POST['ecard_recipient_name'],
     352                                                                'sender_email'          => $_POST['ecard_sender_email'],
     353                                                                'recipient_email'       => $_POST['ecard_recipient_email'],
     354                                                                'subject'                       => $_POST['ecard_subject'],
     355                                                                'message'                       => $_POST['ecard_message'],
     356                                                                'image'                         => $image_element['id'],
     357                                                                'date_creation'         => date("Y-m-d H:i:s"),
    433358                                        );
    434                                         // TO DO : add valid date (end date or duration) / add number (increment number) /
     359                                        if ($_POST['ecard_validity'] != '0') {
     360                                                $date = new DateTime();
     361                                                $date->modify("+".$_POST['ecard_validity']." day");
     362                                                $insert['date_validity'] = $date->format('Y-m-d H:i:s');
     363                                        }
    435364                                        single_insert(ECARD_TABLE, $insert);
    436365
Note: See TracChangeset for help on using the changeset viewer.