Location: PHPKode > projects > MyPhpMoney > myphpmoney_2.0/public_html/operate.php
<?php
/**
 * $Id: operate.php 418 2007-05-17 17:29:45Z hpfn $
 *
 * Author     : courou&#64;users.sourceforge.net
 * Website    : http://allreponse.ath.cx
 *
 * Support    : http://sourceforge.net/projects/myphpmoney/
 * CVS        : http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/myphpmoney/
 */

/**
 * INCLUDE FILE
 */
     require_once 'config/settings.inc.php';

/**
 * ENTER IN THE SESSION OR BUILD THE LOGIN FORM
 */
     if (SQL_VerifSession()) {

/**
 * BUILD THE HEADER
 */
     XHTML_header(1,0,1,1,1,1,1,1);

/**
 * DEFINE VARIABLE GLOBALS
 */
      $PageOrder     = 'operate';
      $sum_spent     = '';
      $sum_received  = '';
      $_POST['yearthird'] = isset($_POST['yearthird']) ? $_POST['yearthird'] : '';
      $_MPM['BlockInclude']  = 0;     ## for the include template
      $_MPM['BlockInclude2'] = 0;     ## for the include template

/**
 * SWITCH -- VALIDE THE ACTION
 */
if (isset($_POST['ok'])) {

     switch($_POST['ok']) {

          ## ADD THE NEWS PART
          case 1 :
           if ($_POST['ajout_tiers'] != '') {

               $result = SQL_SearchIdThird(1,$_POST['cat'],htmlspecialchars($_POST['ajout_tiers']));

               if (!$result) {
                   $id = SQL_RecupId(4);

               SQL_InsertThird($id,$_POST['cat'],htmlspecialchars($_POST['ajout_tiers']));
               $_POST['S_cat'] = $_POST['cat'].'|'.$id;
               unset($_POST['opt']);
               }
           }
          break;

          ## ADD THE NEWS THIRD AND NEWS PART
          case 2 :
             $id_cat   = SQL_RecupId(0);
             $id_tiers = SQL_RecupId(4);

             $id_cat   = SQL_BuildCat(htmlspecialchars($_POST['N_cat']),$id_cat);
             $id_tiers = SQL_BuildTiers(htmlspecialchars($_POST['N_tiers']),$id_cat,$id_tiers);

          $_POST['S_cat'] = $id_cat.'|'.$id_tiers;
          unset($_POST['opt']);
          break;

          ## RENAME THE PART
          case 3 :
             if ($_POST['cat'] != __PART_ID__ && $_POST['cat'] != __OPEN_PART__) {

               $result = SQL_SearchIdThird(1,$_POST['cat'],htmlspecialchars($_POST['news_tiers']));

                if (!$result) {
                 SQL_UpdateThird($_POST['tiers'],htmlspecialchars($_POST['news_tiers']));
               } else {
                 XHTML_DisplayJs($_VAR['MESSAGE_JS']['JS_PART_EXITE']);
               }

             $_POST['S_cat'] = $_POST['cat'].'|'.$_POST['tiers'];
             unset($_POST['opt']);
             }

          break;

          ## RENAME THIRD
          case 4 :
                   if (SQL_IdCat(htmlspecialchars($_POST['news_cat'])) == '') {

                       if ($_POST['cat'] == __PART_ID__ && $_POST['cat'] == __OPEN_PART__) {
                         XHTML_DisplayJs(sprintf($_VAR['MESSAGE_JS']['JS_THIRD_DISPAY_1'],$_MPM['lang_file_name']));
                       } else {//just two parm
                         SQL_UpdateCat(htmlspecialchars($_POST['news_cat']),
			 $_POST['cat']);//,$sess_pid);
                       }
                   } else {
                    XHTML_DisplayJs($_VAR['MESSAGE_JS']['JS_THIRD_EXITE']);
                   } ## end of SQL_IdCat()

          $_POST['S_cat'] = $_POST['cat'];
          unset($_POST['opt']);
          break;

          ## DELETE THE THIRD OR THE PART
          case $_VAR['ACTION_END']['P_FILE_SUPP'] :

          if ($_POST['cat'] != __PART_ID__ && $_POST['cat'] != __OPEN_PART__) {

            ## DELETE THE THIRD AND THE PART
            if($_POST['tiers'] != '' && $_POST['tiers'] != __ANOTHER_ACCOUNT__) {

               SQL_DeleteThird(1,$_POST['cat'],$_POST['tiers']);
               SQL_DeleteAccount(1,$_POST['cat'],$_POST['tiers'],4);

             if (SQL_CountThird($_POST['cat']) == 0) {SQL_DeleteCat($_POST['cat']);}
             SQL_UpdateSolde($_POST['cat'],$_POST['tiers'],1);
             SQL_DeleteAccount(2,$_POST['cat'],$_POST['tiers'],4);

             ## DELETE THE THIRD
             } else {
                 SQL_DeleteCat($_POST['cat']);
                 SQL_DeleteThird(2,$_POST['cat'],'');

                 SQL_DeleteAccount(1,$_POST['cat'],'',3);
                 SQL_DeleteAccount(2,$_POST['cat'],'',3);

            SQL_UpdateSolde($_POST['cat'],'',2);
            }
         } ## end of if $_POST['cat'] != __PART_ID__ && $_POST['cat'] != __OPEN_PART__

   unset($_POST['S_cat']);
   unset($_POST['opt']);
   break;
  }
} ## end of if (isset($_POST['ok']))


/**
 * THE TEMPLATES -- BUILD HEADER OF THIRD PART
 */
 if (isset($_GET['S_cat']) && isset($_GET['page'])) $_POST['S_cat'] = $_GET['S_cat'];

     $_MPM['BlockInclude2'] = MPM_IncludeTemp(0,'header_third_part_simple.tpl','header_third_part_double.tpl');
     $_MPM['BeginMixed'] = array
     (
        'OPEN_CLOSE'           => XHTML_OPEN_CLOSE(),
        'OPT_THIRD_DEFAULT_1'  => $_VAR['OPT_THIRD_DEFAULT_1'],
        'LIST_THIRD_PART'      => SQL_ListRecursive(isset($_POST['S_cat']) ? $_POST['S_cat'] : '',0,'Plus')
     );
     XHTML_SetFile($_MPM['BlockInclude2'],$_MPM['BeginMixed'],2);

/**
 * CONDITION FOR THE CHOICE SWITCH OPTION
 */
 if (isset($_POST['S_cat']) && $_POST['S_cat'] != '') {

         if (strpos($_POST['S_cat'],'|') !== false) {
           $explode  = explode('|', $_POST['S_cat']);
            if ($explode[0] == __ANOTHER_ACCOUNT__ && $explode[1] == __ANOTHER_ACCOUNT__) $_POST['opt'] = 3;
            if ($explode[0] != __ANOTHER_ACCOUNT__ && $explode[1] == __ANOTHER_ACCOUNT__) {
              $_POST['cat']   = $explode[0];
              $_POST['tiers'] = $explode[1];
              $_POST['opt'] = 2;
            }
         } else {
            $explode[0] = $_POST['S_cat'];
            $explode[1] = '';
         } ## end of strstr

	$explode[0] = SQL_Clean($explode[0]);
	$explode[1] = SQL_Clean($explode[1]);

/**
 * SWITCH -- CHOICE TEMPLATES HTML
 */
     if (!isset($_POST['opt'])) $_POST['opt'] = '';
     switch($_POST['opt']) {

         // DISPLAY THE OPERATION
         default :

         if (isset($_GET['S_Year']) && isset($_GET['page'])) $_POST['yearthird'] = $_GET['S_Year'];
         if ($explode[0] != '') {
         $search_explode_cat   = SQL_SearchCat($explode[0]);
         $search_explode_third = SQL_SearchThird($explode[1]);

         /**
          * SQL ORDER BY ... ASC ... DESC
          */
           SQL_Orderby($PageOrder,1);
           if (isset($_POST['order1'][$PageOrder]) && strpos($_POST['order1'][$PageOrder], "_") !== false) {
            $this_order = explode("_",$_POST['order1'][$PageOrder]);
	    $this_order[0] = SQL_Clean($this_order[0]);
             if ($this_order[1] == 0) $order = "DATES ".$this_order[0];
             if ($this_order[1] == 1) $order = "ACCOUNT ".$this_order[0];
             if ($this_order[1] == 2) $order = "NUMID ".$this_order[0];
             if ($this_order[1] == 3) $order = "COMMENTS ".$this_order[0];
             if (!isset($order))      $order = "DATES asc";
           } else {
            $order = "DATES asc";
           }

           $_MPM['BlockInclude'] = MPM_IncludeTemp(0,'form_third_part_simple.tpl','form_third_part_double.tpl');
           $_MPM['BeginMixed'] = array
           (
            'IMG_ASC_DESC1'    => XHTML_ASC_DESC($PageOrder,'asc_0','desc_0',1),
            'IMG_ASC_DESC2'    => XHTML_ASC_DESC($PageOrder,'asc_1','desc_1',1),
            'IMG_ASC_DESC3'    => XHTML_ASC_DESC($PageOrder,'asc_2','desc_2',1),
            'IMG_ASC_DESC4'    => XHTML_ASC_DESC($PageOrder,'asc_3','desc_3',1),
            'LANG_ACCOUNT_21'  => $_VAR['LANG_ACCOUNT_21'],
            'DISPLAY_CAT'      => $explode[0],
            'DISPLAY_TIERS'    => $explode[1],
            'DISPLAY_S_CAT'    => $_POST['S_cat'],
            'SEARCH_CAT'       => '['.$search_explode_cat.']',
            'MESS_PART'        => $explode[1] != '' && $explode[1] != __ANOTHER_ACCOUNT__ ? $_VAR['MESS_PART'] : '',
            'SEARCH_TIERS'     => $explode[1] != '' && $explode[1] != __ANOTHER_ACCOUNT__ ? '['.$search_explode_third.']' : '',
            'MESS_THIRD'       => $_VAR['MESS_THIRD'],
            'LANG_ACCOUNT_9'   => $_VAR['LANG_ACCOUNT_9'],
            'SELECT_YEAR'      => XHTML_SelectYearMonth(4,$_POST['yearthird'],SQL_SearchYearOperate($explode[0],$explode[1]),'yearthird')
           );
           XHTML_SetFile($_MPM['BlockInclude'],'',0);

                        if (empty($_GET['page'])) $_GET['page'] = 1;
			$_POST['yearthird'] = SQL_Clean($_POST['yearthird']);

                              if (empty($total)) {
                                 $nbre_rows  = "SELECT count(*) as QTE FROM ".$_MPM['table'][1]." WHERE";
                                 if (isset($_POST['yearthird']) && strlen($_POST['yearthird']) == 4) {
                                 $nbre_rows .= " DATES LIKE '%".$_POST['yearthird']."%' AND";
                                 }
                                 $nbre_rows .= " TEMPID=1 AND USERID='$sess_pid' AND ";
                                      if($explode[1] != "" && $explode[1] != __ANOTHER_ACCOUNT__) {
                                        $nbre_rows .= "CAT='$explode[0]' AND ";
                                      } else {
                                        $nbre_rows .= "CAT='$explode[0]' OR ";
                                      }
                                  $nbre_rows .= "THIRD='$explode[1]' ORDER BY ".$order;
                                  $db->query($nbre_rows);
                                  $db->next_record();
                                  $total=$db->f('QTE');
                              }

                         $debut = ($_GET['page'] - 1) * __NB_OF_PAGE__;

                         ## For current display
                         $select  = "SELECT OPID,NUMID,DATE_FORMAT(DATES,'%d-%m-%Y') as THE_DATES,THIRD,COMMENTS,SOUTT,IMP,ACCOUNT ";
                         $select .= "FROM ".$_MPM['table'][1]." WHERE";
                         if (isset($_POST['yearthird']) && strlen($_POST['yearthird']) == 4) {
                         $select .= " DATES LIKE '%".$_POST['yearthird']."%' AND";
                         }
                         $select .= " TEMPID=1 AND USERID='$sess_pid' AND CAT='$explode[0]' ";
                         $select .= (($explode[1] != "" && $explode[1] != __ANOTHER_ACCOUNT__) ? 'AND ': 'OR ' );
                         $select .= "THIRD='$explode[1]' ORDER BY ".$order." LIMIT $debut,".__NB_OF_PAGE__;

                         $db->query($select);

                         $tpl->set_block('FILE_'.$_MPM['BlockInclude'].'','BOUCLE_THIRD_PART','BOUCLE_BEGIN_THIRD');

                             while ($db->next_record()) {
                                 $_tiers=$db->f('THIRD');
                                 $_depense=$db->f('SOUTT');
                                 $_recette=$db->f('IMP');

                                 $sum_spent += $_depense;
                                 $sum_received += $_recette;
                                 if ($_depense == 0 || $_recette == '') { $_montant = $_recette; $color = 'menu';}
                                 if ($_recette == 0 || $_depense == '') { $_montant = - $_depense; $color = 'menurouge';}

                            $tpl->set_var(array(
                                'HTTP_HOME_ACCOUNT'      => $_MPM['file_account'],
                                'B_THIRD_ID'             => $db->f('OPID'),
                                'B_THIRD_DATE'           => $db->f('THE_DATES'),
                                'BB_THIRD_ACCOUNT'       => $db->f('ACCOUNT'),
                                'BB_THIRD_DATE'          => preg_replace('/^([0-9]{1,2})-([0-9]{1,2})-([0-9]{2,4})$/', '\2-\3', $db->f('THE_DATES')),
                                'B_THIRD_ACCOUNT'        => $tools->Reduit_chaine(SQL_NameAccount($db->f('ACCOUNT')),__LENGTH_ACCOUNT__,0),
                                'B_THIRD_NUMEROID'       => $tools->Reduit_chaine($db->f('NUMID'),__LENGTH_NUMERO__,0),
                                'B_THIRD_COMMENT'        => $tools->Reduit_chaine($db->f('COMMENTS'),__LENGTH_COMMENT__,0),
                                'B_THIRD_AMOUNT'         => $tools->formatage($_montant),
                                'B_THIRD_AMOUNT_OTHER'   => $tools->formatage($_montant  * $_MPM['lang_euro']),
                                'FORMAT_COLOR'           => $color,
                                'DISPLAY_DATE'           => $_MPM['DATE_DAYS'],
                                'classbgcolor'           => isset($colors) && $colors == __COLOR_DISPLAY_1__ ? 'display2' : 'display1',
                                'COLOR'                  => isset($colors) && $colors == __COLOR_DISPLAY_1__ ? $colors = __COLOR_DISPLAY_2__ : $colors = __COLOR_DISPLAY_1__
                             ));
                             $tpl->parse('BOUCLE_BEGIN_THIRD','BOUCLE_THIRD_PART',true);
                             } ## end of while $db


               $_result = $sum_received - $sum_spent;
               $the_cat   = str_replace('"', "'", $tools->add_slashes($search_explode_cat));
               $the_tiers = str_replace('"', "'", $tools->add_slashes($search_explode_third));
               $nbpages = ceil($total / __NB_OF_PAGE__);
               if ($nbpages == '' || $nbpages == 0) $nbpages = 1;

              $tpl->set_var(array(
                   'RENAME'                 => $_VAR['RENAME'],
                   'B_THIRD_RESULT'         => $tools->formatage($_result),
                   'B_THIRD_RESULT_OTHER'   => $tools->formatage($_result  * $_MPM['lang_euro']),
                   'FORMAT_COLOR'           => isset($color) ? $color : 'Chiffre',
                   'ACTION_BEGIN_THIRD_PART'=> $_VAR['ACTION_END']['P_FILE_SUPP']
              ));
              $tpl->set_block('FILE_'.$_MPM['BlockInclude'].'','BOUCLE_THIRD_PAGE','BOUCLE_BEGIN_THIRD_PAGE');

          for ($i = 1; $i <= $nbpages; $i++) {

           $tpl->set_var(array(
               'II_PAGES' => $i == $_GET['page']
                              ? $i
                              : '<a class="menu" href="'.$_SERVER['PHP_SELF'].'?S_cat='.$_POST['S_cat'].'&amp;page='.$i.'&amp;S_Year='.$_POST['yearthird'].'#'.$_VAR['BELOW'].'"> '.$i.' </a>'
           ));
           $tpl->parse('BOUCLE_BEGIN_THIRD_PAGE','BOUCLE_THIRD_PAGE',true);
          } ## end of for $i <= $nbpages


          if ($explode[0] != __PART_ID__ && $explode[0] != __OPEN_PART__) {

                 $mes = $_VAR['MESSAGE_JS']['JS_THIRD_BEGIN_DEL']." : ";
                 if ($explode[1] == '') {
                    $mes .= $_VAR['MESSAGE_JS']['JS_THIRD_BEGIN']." [$the_cat] ";
                    $mes .= $_VAR['MESSAGE_JS']['JS_THIRD_DISPAY'];
                 } else {
                    $mes .= $_VAR['MESSAGE_JS']['JS_PART_BEGIN_DEL']." [$the_tiers] ";
                    $mes .= $_VAR['DUMP_SEPARATOR']." ".$_VAR['MESSAGE_JS']['JS_THIRD_BEGIN'];
                    $mes .= "[$the_cat] ".$_VAR['MESSAGE_JS']['JS_THIRD_DISPAY'];
                 }

                 $_MPM['BoucleMixed'] = array
                 (
                      'CONDITION_FORM'  => XHTML_SetFile('form_opt_third_1.tpl','',1),
                      'CONDITION_THIRD' => XHTML_SetFile('form_third_submit.tpl','',1),
                      'THIRD_ADD'       => $_VAR['THIRD_ADD'],
                      'MESS_ADD_PART'   => $_VAR['MESS_ADD_PART'],
                      'MESSAGE_JS_1'    => "if(confirm('$mes')) return true; else return false;"
                  );

          } else {

                 $mes = sprintf($_VAR['MESSAGE_JS']['JS_THIRD_DISPAY_1'],$_MPM['lang_file_name']);
                 $_MPM['BoucleMixed'] = array
                 (
                       'CONDITION_FORM' => XHTML_SetFile('form_opt_third_2.tpl','',1),
                       'CONDITION_THIRD'=> '',
                       'THIRD_ADD'      => $_VAR['THIRD_ADD'],
                       'RENAME'         => $_VAR['RENAME'],
                       'MESSAGE_JS_1'   => "if(confirm('$mes')) return false; else return false;"
                 );

          } ## end of $explode[0] != __PART_ID__ && $explode[0] != __OPEN_PART__

      $_MPM['BeginMixed'] = array_merge($_MPM['BeginMixed'],$_MPM['BoucleMixed']);
      XHTML_SetFile($_MPM['BlockInclude'],$_MPM['BeginMixed'],4);
      } ## end of $explode[0] != ''
      break;

      // TEMPLATE FORM FOR THE ADD NEWS THIRD
            case 2 :
            case $_VAR['MESS_ADD_PART'] :
            $search_cat = SQL_SearchCat(SQL_Clean($_POST['cat']));
               if ($search_cat != '' ) {
                   $_MPM['BeginMixed'] = array
                   (
                          'THE_CAT'                => str_replace('"', "'",$search_cat),
                          'MESS_PART_FORM_HOME'    => $_VAR['MESS_PART_FORM_HOME'],
                          'MESS_PART_SIMPLE'       => $_VAR['MESS_PART_SIMPLE'],
                          'MESS_NOW_PART'          => $_VAR['MESS_NOW_PART'],
                          'CAT'                    => $_POST['cat'],
                          'TIERS'                  => $_POST['tiers'],
                          'ADD_TIERS'              => isset($_POST['ajout_tiers']) ? $_POST['ajout_tiers'] : ''
                   );
                   XHTML_SetFile('form_news_part.tpl',$_MPM['BeginMixed'],2);
               }
      break;

      // TEMPLATE FORM FOR THE ADD NEWS THIRD AND PART
            case 3 :
            case $_VAR['THIRD_ADD'] :

             $_MPM['BeginMixed'] = array
             (
                   'THIRD_NEWS_THIRD'        => $_VAR['THIRD_NEWS_THIRD'],
                   'MESS_CALENDAR_NEW'       => $_VAR['MESS_CALENDAR_NEW'],
                   'MESS_THIRD_FORM_HOME'    => $_VAR['MESS_THIRD_FORM_HOME'],
                   'MESS_PART_SIMPLE'        => $_VAR['MESS_PART_SIMPLE'],
                   'N_CAT'                   => isset($_POST['N_cat']) ? $_POST['N_cat'] : '',
                   'N_TIERS'                 => isset($_POST['N_tiers']) ? $_POST['N_tiers'] : ''
             );
             XHTML_SetFile('form_news_third.tpl',$_MPM['BeginMixed'],2);
      break;

      // TEMPLATE FORM FOR RENAME THE THIRD OR THE PART
            case $_VAR['RENAME'] :

             $_MPM['BeginMixed'] = array
             (
                 'THIRD_VALID_MODIF'         => $_VAR['THIRD_VALID_MODIF'],
                 'RENAME_CAT'                => $_POST['cat'],
                 'RENAME_TIERS'              => $_POST['tiers'],
                 'MESS_RENAME_FORM_HOME'     => $_POST['tiers'] != '' ? $_VAR['MESS_MODIF_PART'] : $_VAR['MESS_CALENDAR_MODIF'],
                 'CHOICE_RENAME_JS'          => $_POST['tiers'] != '' ? 'return Verif_modif_tiers(this);' : 'return Verif_modif_cat(this);',
                 'CHOICE_RENAME_LANG_1'      => $_POST['tiers'] != '' ? $_VAR['MESS_OLD_PART'] : $_VAR['MESS_OLD_THIRD'],
                 'CHOICE_RENAME_TXT'         => $_POST['tiers'] != ''
                                                ? stripslashes(str_replace('"', "'", SQL_SearchThird(SQL_Clean($_POST['tiers']))))
                                                : stripslashes(str_replace('"', "'", SQL_SearchCat(SQL_Clean($_POST['cat'])))),
                 'CHOICE_RENAME_LANG_2'      => $_POST['tiers'] != '' ? $_VAR['MESS_PART_SIMPLE'] : $_VAR['MESS_CALENDAR_NEW'],
                 'CHOICE_RENAME_NAME'        => $_POST['tiers'] != '' ? 'news_tiers' : 'news_cat',
                 'CHOICE_RENAME_VALUE'       => isset($_POST['news_tiers'])
                                                ? $_POST['news_tiers']
                                                : isset($_POST['news_cat'])
                                                   ? $_POST['news_cat']
                                                   : '',
                 'CHOICE_RENAME_T'           => $_POST['tiers'] != '' ? 3 : 4
             );
             XHTML_SetFile('form_rename_third_part.tpl',$_MPM['BeginMixed'],2);
      break;
    } ## end of switch $_POST['opt']
} ## end of if (isset($_POST['S_cat']) && $_POST['S_cat'] != '')

     } else {
          XHTML_DisplayLogin();
     } ## end if (SQL_VerifSession())

/**
 * BUILD THE FOOTER
 */
     XHTML_footer();
Return current item: MyPhpMoney