class_fiche_def.php

Go to the documentation of this file.
00001 <?php
00002 /*
00003  *   This file is part of PhpCompta.
00004  *
00005  *   PhpCompta is free software; you can redistribute it and/or modify
00006  *   it under the terms of the GNU General Public License as published by
00007  *   the Free Software Foundation; either version 2 of the License, or
00008  *   (at your option) any later version.
00009  *
00010  *   PhpCompta is distributed in the hope that it will be useful,
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  *   GNU General Public License for more details.
00014  *
00015  *   You should have received a copy of the GNU General Public License
00016  *   along with PhpCompta; if not, write to the Free Software
00017  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018 */
00019 /* $Revision: 4983 $ */
00020 // Copyright Author Dany De Bontridder ddebontridder@yahoo.fr
00021 include_once("class_fiche_attr.php");
00022 require_once("class_itext.php");
00023 require_once('class_fiche_def_ref.php');
00024 require_once('class_fiche.php');
00025 require_once('user_common.php');
00026 require_once('class_iradio.php');
00027 
00028 /*! \file
00029  * \brief define Class fiche and fiche def, those class are using
00030  *        class attribut
00031  */
00032 /*!
00033  * \brief define Class fiche and fiche def, those class are using
00034  *        class attribut
00035  */
00036 class Fiche_Def
00037 {
00038     var $cn;           // database connection
00039     var $id;                    // id (fiche_def.fd_id
00040     var $label;                 // fiche_def.fd_label
00041     var $class_base;            // fiche_def.fd_class_base
00042     var $fiche_def;             // fiche_def.frd_id = fiche_def_ref.frd_id
00043     var $create_account;                // fd_create_account: flag
00044     var $all;
00045     var $attribut;              // get from attr_xxx tables
00046     function __construct($p_cn,$p_id = 0)
00047     {
00048         $this->cn=$p_cn;
00049         $this->id=$p_id;
00050     }
00051     /*!\brief show the content of the form to create  a new Fiche_Def_Ref
00052     */
00053     function input ()
00054     {
00055         $ref=$this->cn->get_array("select * from fiche_def_ref order by frd_text");
00056         $iradio=new IRadio();
00057         /* the accounting item */
00058         $class_base=new IPoste('class_base');
00059         $class_base->set_attribute('ipopup','ipop_account');
00060         $class_base->set_attribute('account','class_base');
00061         $class_base->set_attribute('label','acc_label');
00062         $f_class_base=$class_base->input();
00063         require_once ('template/fiche_def_input.php');
00064         return;
00065     }
00066 
00067     /*!
00068      *  \brief  Get attribut of a fiche_def
00069      *
00070      * \return string value of the attribute
00071      */
00072     function getAttribut()
00073     {
00074         $sql="select * from jnt_fic_attr ".
00075              " natural join attr_def where fd_id=".$this->id.
00076              " order by jnt_order";
00077 
00078         $Ret=$this->cn->exec_sql($sql);
00079 
00080         if ( ($Max=Database::num_row($Ret)) == 0 )
00081             return ;
00082         for ($i=0;$i < $Max;$i++)
00083         {
00084             $row=Database::fetch_array($Ret,$i);
00085             $t = new Fiche_Attr($this->cn);
00086             $t->ad_id=$row['ad_id'];
00087             $t->ad_text=$row['ad_text'];
00088             $t->jnt_order=$row['jnt_order'];
00089             $t->ad_size=$row['ad_size'];
00090             $t->ad_type=$row['ad_type'];
00091             $t->ad_extra=$row['ad_extra'];
00092             $this->attribut[$i]=clone $t;
00093         }
00094         return $this->attribut;
00095     }
00096 
00097     /*!
00098     * \brief  Get attribut of the fiche_def
00099     *
00100     */
00101     function Get()
00102     {
00103         if ( $this->id == 0 )
00104             return 0;
00105         /*    $this->cn->exec_sql('select fiche_attribut_synchro($1)',
00106         array($this->id));
00107         */
00108         $sql="select * from fiche_def ".
00109              " where fd_id=".$this->id;
00110         $Ret=$this->cn->exec_sql($sql);
00111         if ( ($Max=Database::num_row($Ret)) == 0 )
00112             return ;
00113         $row=Database::fetch_array($Ret,0);
00114         $this->label=$row['fd_label'];
00115         $this->class_base=$row['fd_class_base'];
00116         $this->fiche_def=$row['frd_id'];
00117         $this->create_account=$row['fd_create_account'];
00118     }
00119     /*!
00120      **************************************************
00121      * \brief  Get all the fiche_def
00122      *
00123      * \return an array of fiche_def object
00124      */
00125     function GetAll()
00126     {
00127         $sql="select * from fiche_def ";
00128 
00129         $Ret=$this->cn->exec_sql($sql);
00130         if ( ($Max=Database::num_row($Ret)) == 0 )
00131             return ;
00132 
00133         for ( $i = 0; $i < $Max;$i++)
00134         {
00135             $row=Database::fetch_array($Ret,$i);
00136             $this->all[$i]=new Fiche_Def($this->cn,$row['fd_id']);
00137             $this->all[$i]->label=$row['fd_label'];
00138             $this->all[$i]->class_base=$row['fd_class_base'];
00139             $this->all[$i]->fiche_def=$row['frd_id'];
00140             $this->all[$i]->create_account=$row['fd_create_account'];
00141         }
00142     }
00143     /*!
00144      **************************************************
00145      * \brief  Check in vw_fiche_def if a fiche has
00146      *           a attribut X
00147      *
00148      *
00149      * \param  $p_attr attribut to check
00150      * \return  true or false
00151      */
00152     function HasAttribute($p_attr)
00153     {
00154         return ($this->cn->count_sql("select * from vw_fiche_def where ad_id=$p_attr and fd_id=".$this->id)>0)?true:false;
00155 
00156     }
00157     /*!
00158      **************************************************
00159      * \brief  Display category into a table
00160      *
00161      * \return HTML row
00162      */
00163     function Display()
00164     {
00165                 $tab = new Sort_Table();
00166 
00167                 $url = HtmlInput::get_to_string(array('ac', 'gDossier'));
00168                 $tab->add("Nom de fiche", $url, "order by fd_label asc", "order by fd_label desc", "na", "nd");
00169                 $tab->add("Basé sur le poste comptable", $url, "order by fd_class_base asc", "order by fd_class_base desc", "pa", "pd");
00170                 $tab->add("Calcul automatique du poste comptable", $url, "order by fd_create_account asc", "order by fd_create_account desc", "ca", "cd");
00171                 $tab->add("Basé sur le modèle", $url, "order by frd_text asc", "order by frd_text  desc", "ma", "md");
00172 
00173                 $order = (isset($_GET['ord'])) ? $tab->get_sql_order($_GET["ord"]) : $tab->get_sql_order("na");
00174 
00175 
00176                 $res = $this->cn->exec_sql("SELECT fd_id, fd_class_base, fd_label, fd_create_account, fiche_def_ref.frd_id,
00177 frd_text  FROM fiche_def join fiche_def_ref on (fiche_def.frd_id=fiche_def_ref.frd_id)
00178 $order
00179 ");
00180 
00181                 require_once 'template/fiche_def_list.php';
00182         }
00183     /*!\brief Add a fiche category thanks the element from the array
00184      * you cannot add twice the same cat. name
00185      * table : insert into fiche_def
00186      *         insert into attr_def
00187      *
00188      * \param $array array
00189      *        index FICHE_REF
00190      *              nom_mod
00191      *              class_base
00192      */
00193     function Add($array)
00194     {
00195         foreach ( $array as $key=>$element )
00196         {
00197             ${"p_$key"}=$element;
00198         }
00199         // Format correctly the name of the cat. of card
00200         $p_nom_mod=sql_string($p_nom_mod);
00201 
00202 
00203         // Name can't be empty
00204         if ( strlen(trim($p_nom_mod)) == 0 )
00205                 {
00206                         alert (_('Le nom de la catégorie ne peut pas être vide'));
00207             return 1;
00208                 }
00209         // $p_FICHE_REF cannot be null !!! (== fiche_def_ref.frd_id
00210         if (! isset ($p_FICHE_REF) || strlen($p_FICHE_REF) == 0 )
00211         {
00212             echo alert (_('Vous devez choisir une categorie'));
00213             return 1;
00214         }
00215         $fiche_Def_ref=new Fiche_Def_Ref($this->cn,$p_FICHE_REF);
00216         $fiche_Def_ref->Get();
00217 
00218         // build the sql request for fiche_def
00219         // and insert into fiche_def
00220         // if p_class_base is null get the default class base from
00221         // fiche_def_ref
00222         if ( sql_string($p_class_base) == null )
00223         { // p_class is null
00224             // So we take the default one
00225             $p_class_base=$fiche_Def_ref->frd_class_base;
00226         }
00227         /* check if the cat. name already exists */
00228         $sql="select count(*) from fiche_Def where upper(fd_label)=upper($1)";
00229         $count=$this->cn->get_value($sql,array(trim($p_nom_mod)));
00230 
00231         if ($count != 0 ) {
00232                          echo alert (_('Catégorie existante'));
00233                         return 1;
00234                 }
00235         // Set the value of fiche_def.fd_create_account
00236         // automatic creation for 'poste comptable'
00237         if ( isset($p_create) && strlen(trim($p_class_base)) != 0)
00238             $p_create='true';
00239         else
00240             $p_create='false';
00241 
00242         // Class is valid ?
00243         if ( sql_string($p_class_base) != null || strpos(',',$p_class_base) != 0 )
00244         {
00245             // p_class is a valid number
00246             $sql="insert into fiche_def(fd_label,fd_class_base,frd_id,fd_create_account)
00247                  values ($1,$2,$3,$4) returning fd_id";
00248 
00249             $fd_id=$this->cn->get_value($sql,array($p_nom_mod,$p_class_base,$p_FICHE_REF,$p_create));
00250 
00251             // p_class must be added to tmp_pcmn if it is a single accounting
00252             if ( strpos(',',$p_class_base) ==0)
00253             {
00254                 $sql="select account_add($1,$2)";
00255                 $Res=$this->cn->exec_sql($sql,array($p_class_base,$p_nom_mod));
00256             }
00257 
00258             // Get the fd_id
00259             $fd_id=$this->cn->get_current_seq('s_fdef');
00260 
00261             // update jnt_fic_attr
00262             $sql=sprintf("insert into jnt_fic_attr(fd_id,ad_id,jnt_order)
00263                          values (%d,%d,10)",$fd_id,ATTR_DEF_ACCOUNT);
00264             $Res=$this->cn->exec_sql($sql);
00265 
00266         }
00267         else
00268         {
00269             //There is no class base not even as default
00270             $sql=sprintf("insert into fiche_def(fd_label,frd_id,fd_create_account) values ('%s',%d,'%s') returning fd_id",
00271                          $p_nom_mod,$p_FICHE_REF,$p_create);
00272 
00273             $this->id=$this->cn->get_value($sql);
00274 
00275             // Get the fd_id
00276             $fd_id=$this->cn->get_current_seq('s_fdef');
00277 
00278         }
00279 
00280         // Get the default attr_def from attr_min
00281         $def_attr=$this->get_attr_min($p_FICHE_REF);
00282 
00283         //if defaut attr not null
00284         // build the sql insert for the table attr_def
00285         if (sizeof($def_attr) != 0 )
00286         {
00287             // insert all the mandatory fields into jnt_fiche_attr
00288             $jnt_order=10;
00289             foreach ( $def_attr as $i=>$v)
00290             {
00291                                 $order=$jnt_order;
00292                 if ( $v['ad_id'] == ATTR_DEF_NAME )
00293                     $order=0;
00294                 $sql=sprintf("insert into jnt_fic_Attr(fd_id,ad_id,jnt_order)
00295                              values (%d,%s,%d)",
00296                              $fd_id,$v['ad_id'],$order);
00297                 $this->cn->exec_sql($sql);
00298                                 $jnt_order+=10;
00299             }
00300         }
00301         $this->id=$fd_id;
00302         return 0;
00303 
00304     }//--------------end function Add ----------------------------
00305     /*!
00306      * \brief Get all the card where the fiche_def.fd_id is given in parameter
00307      * \param $step = 0 we don't use the offset, page_size,...
00308      *        $step = 1 we use the jnr_bar_nav
00309      *
00310      * \return array ('f_id'=>..,'ad_value'=>..)
00311      *\see fiche
00312      */
00313     function get_by_type($step=0)
00314     {
00315         $sql="select f_id,ad_value
00316              from
00317              fiche join fiche_detail using(f_id)
00318              where ad_id=1 and fd_id=$1 order by 2";
00319 
00320         // we use navigation_bar
00321         if ($step == 1  && $_SESSION['g_pagesize'] != -1   )
00322         {
00323             $offset=(isset($_GET['offset']))?$_GET['offset']:0;
00324             $step=$_SESSION['g_pagesize'];
00325             $sql.=" offset $offset limit $step";
00326         }
00327 
00328         $Ret=$this->cn->get_array($sql,array($this->id));
00329 
00330         return $Ret;
00331     }
00332     /*!
00333      * \brief Get all the card where the fiche_def.frd_id is given in parameter
00334      * \return array of fiche or null is nothing is found
00335      */
00336     function get_by_category($p_cat)
00337     {
00338         $sql="select f_id,ad_value
00339              from
00340              fiche join fiche_def  using(fd_id)
00341              join fiche_detail using(f_id)
00342              where ad_id=1 and frd_id=$1 order by 2 ";
00343 
00344         $Ret=$this->cn->exec_sql($sql,array($p_cat));
00345         if ( ($Max=Database::num_row($Ret)) == 0 )
00346             return null;
00347         $all[0]=new Fiche($this->cn);
00348 
00349         for ($i=0;$i<$Max;$i++)
00350         {
00351             $row=Database::fetch_array($Ret,$i);
00352             $t=new Fiche($this->cn,$row['f_id']);
00353             $t->getAttribut();
00354             $all[$i]=$t;
00355 
00356         }
00357         return $all;
00358     }
00359 
00360     /*!\brief list the card of a fd_id
00361      */
00362     function myList()
00363     {
00364         $this->Get();
00365         echo '<H2 class="info">'.$this->id." ".$this->label.'</H2>';
00366 
00367         $step=$_SESSION['g_pagesize'];
00368         $sql_limit="";
00369         $sql_offset="";
00370         $bar="";
00371         if ( $step != -1 )
00372         {
00373 
00374             $page=(isset($_GET['page']))?$_GET['page']:1;
00375             $offset=(isset($_GET['offset']))?$_GET['offset']:0;
00376             $max_line=$this->cn->count_sql("select f_id,ad_value  from
00377                                            fiche join fiche_detail using (f_id)
00378                                            where fd_id='".$this->id."' and ad_id=".ATTR_DEF_NAME." order by f_id");
00379             $sql_limit=" limit ".$step;
00380             $sql_offset=" offset ".$offset;
00381             $bar=navigation_bar($offset,$max_line,$step,$page);
00382         }
00383 
00384         // Get all name the cards of the select category
00385         // 1 for attr_def.ad_id is always the name
00386         $Res=$this->cn->exec_sql("select f_id,vw_name,quick_code  from ".
00387                                  " vw_fiche_attr ".
00388                                  " where fd_id='".$this->id.
00389                                  "' order by f_id $sql_offset $sql_limit ");
00390         $Max=Database::num_row($Res);
00391         echo $bar;
00392         $str="";
00393         // save the url
00394         // with offet &offset=15&step=15&page=2&size=15
00395         if ( $_SESSION['g_pagesize'] != -1)
00396         {
00397             $str=sprintf("&offset=%s&step=%s&page=%s&size=%s",
00398                          $offset,
00399                          $step,
00400                          $page,
00401                          $max_line);
00402         }
00403 
00404 
00405         echo '<FORM METHOD="POST" action="?p_action=fiche&action=vue'.$str.'">';
00406         echo HtmlInput::hidden('ac',$_REQUEST['ac']);
00407         echo dossier::hidden();
00408         echo HtmlInput::hidden("fiche",$this->id);
00409         echo HtmlInput::submit('add','Ajout fiche');
00410         echo '</FORM>';
00411         $str_dossier=dossier::get();
00412         echo '<table>';
00413         for ( $i = 0; $i < $Max; $i++)
00414         {
00415             $l_line=Database::fetch_array($Res,$i);
00416             if ( $i%2 == 0)
00417                 echo '<TR class="odd">';
00418             else
00419                 echo '<TR class="even">';
00420 
00421             $span_mod='<TD><A href="?p_action=fiche&'.$str_dossier.
00422                     '&action=detail&fiche_id='.$l_line['f_id'].$str.'&fiche='.
00423                     $_REQUEST['fiche'].'&ac='.$_REQUEST['ac'].'">'.$l_line['quick_code']
00424                     .'</A></TD>';
00425 
00426             echo $span_mod.'<TD>'.h($l_line['vw_name'])."</TD>";
00427             echo '</tr>';
00428         }
00429         echo '</table>';
00430         echo '<FORM METHOD="POST" action="?p_action=fiche&action=vue'.$str.'">';
00431         echo HtmlInput::hidden('ac',$_REQUEST['ac']);
00432         echo dossier::hidden();
00433         echo HtmlInput::hidden("fiche",$this->id);
00434         echo HtmlInput::submit('add','Ajout fiche');
00435         echo '</FORM>';
00436         echo $bar;
00437 
00438     }
00439     /*!\brief show input for the basic attribute : label, class_base, create_account
00440      * use only when we want to update
00441      *
00442      *\return HTML string with the form
00443      */
00444     function input_base()
00445     {
00446         $r="";
00447         $r.=_('Label');
00448         $label=new IText('label',$this->label);
00449         $r.=$label->input();
00450         $r.='<br>';
00451         /* the accounting item */
00452         $class_base=new IPoste('class_base',$this->class_base);
00453         $class_base->set_attribute('ipopup','ipop_account');
00454         $class_base->set_attribute('account','class_base');
00455         $class_base->set_attribute('label','acc_label');
00456         $r.=_('Poste Comptable de base').' : ';
00457         $r.=$class_base->input();
00458         $r.='<span id="acc_label"></span><br>';
00459         /* auto Create */
00460         $ck=new ICheckBox('create');
00461         $ck->selected=($this->create_account=='f')?false:true;
00462         $r.=_('Chaque fiche aura automatiquement son propre poste comptable : ');
00463         $r.=$ck->input();
00464         return $r;
00465     }
00466     /*!\brief Display all the attribut of the fiche_def
00467      *\param $str give the action possible values are remove, empty
00468      */
00469     function DisplayAttribut($str="")
00470     {
00471         if ( $this->id == 0 )
00472             return ;
00473            $this->cn->exec_sql('select fiche_attribut_synchro($1)',array($this->id));
00474 
00475                    $MaxLine=sizeof($this->attribut);
00476         $r="<TABLE>";
00477         $r.="<tr>".th('Nom attribut').th('').th('Ordre','style="text-align:right"').'</tr>';
00478         // Display each attribute
00479         $add_action="";
00480         for ($i=0;$i<$MaxLine;$i++)
00481         {
00482             $class="even";
00483             if ( $i % 2 == 0 )
00484                 $class="odd";
00485 
00486             $r.='<TR class="'.$class.'"><td>';
00487             // Can change the name
00488             if ( $this->attribut[$i]->ad_id == ATTR_DEF_NAME )
00489             {
00490                 continue;
00491             }
00492             else
00493             {
00494                 if ( $str == "remove" )
00495                 {
00496                     //Only for the not mandatory attribute (not defined in attr_min)
00497                     if ( $this->cn->count_sql("select * from attr_min where frd_id=".
00498                                               $this->fiche_def." and ad_id = ".$this->attribut[$i]->ad_id) == 0
00499                             && $this->attribut[$i]->ad_id != ATTR_DEF_QUICKCODE
00500                             && $this->attribut[$i]->ad_id != ATTR_DEF_ACCOUNT
00501                        )
00502                     {
00503                         $add_action=sprintf( '</TD><TD> Supprimer <input type="checkbox" name="chk_remove[]" value="%d">',
00504                                              $this->attribut[$i]->ad_id);
00505                     }
00506                     else
00507                         $add_action="</td><td>";
00508                 }
00509                 // The attribut.
00510                 $a=sprintf('%s ',  $this->attribut[$i]->ad_text);
00511                 $r.=$a.$add_action;
00512                 /*----------------------------------------  */
00513                 /*  ORDER OF THE CARD */
00514                 /*----------------------------------------  */
00515                 $order=new IText();
00516                 $order->name='jnt_order'.$this->attribut[$i]->ad_id;
00517                 $order->size=3;
00518                 $order->value=$this->attribut[$i]->jnt_order;
00519                 $r.='</td><td> '.$order->input();
00520             }
00521             $r.= '</td></tr>';
00522         }
00523 
00524         // Show the possible attribute which are not already attribute of the model
00525         // of card
00526         $Res=$this->cn->exec_sql("select ad_id,ad_text from attr_def
00527                                  where
00528                                  ad_id not in (select ad_id from fiche_def natural join jnt_fic_attr
00529                                  where fd_id=$1) order by ad_text",array($this->id) );
00530         $M=Database::num_row($Res);
00531 
00532         // Show the unused attribute
00533         $r.='<TR> <TD>';
00534         $r.= '<SELECT NAME="ad_id">';
00535         for ($i=0;$i<$M;$i++)
00536         {
00537             $l=Database::fetch_array($Res,$i);
00538             $a=sprintf('<OPTION VALUE="%s"> %s',
00539                        $l['ad_id'],$l['ad_text']);
00540             $r.=$a;
00541         }
00542         $r.='</SELECT>';
00543 
00544         $r.="</TABLE>";
00545         return $r;
00546     }
00547     /*!\brief Save the label of the fiche_def
00548      * \param $p_label label
00549      */
00550     function SaveLabel($p_label)
00551     {
00552         if ( $this->id == 0 ) return;
00553         $p_label=sql_string($p_label);
00554         if (strlen(trim ($p_label)) == 0 )
00555         {
00556             return;
00557         }
00558         $sql=sprintf("update   fiche_def set fd_label='%s' ".
00559                      "where                    fd_id=%d",
00560                      $p_label,$this->id);
00561         $Res=$this->cn->exec_sql($sql);
00562 
00563     }
00564     /*!\brief set the auto create accounting item for each card and
00565      * save it into the database
00566      * \param $p_label true or false
00567      */
00568     function set_autocreate($p_label)
00569     {
00570         if ( $this->id == 0 ) return;
00571         if ($p_label==true)
00572             $t='t';
00573         if ($p_label==false)
00574             $t='f';
00575 
00576         $sql="update   fiche_def set fd_create_account=$1 ".
00577              "where                    fd_id=$2";
00578 
00579         $Res=$this->cn->exec_sql($sql,array($t,$this->id));
00580 
00581     }
00582     /*!\brief Save the class base
00583      * \param $p_label label
00584      */
00585     function save_class_base($p_label)
00586     {
00587         if ( $this->id == 0 ) return;
00588         $p_label=sql_string($p_label);
00589 
00590         $sql="update   fiche_def set fd_class_base=$1 ".
00591              "where                    fd_id=$2";
00592 
00593         $Res=$this->cn->exec_sql($sql,array($p_label,$this->id));
00594     }
00595 
00596 
00597     /*!\brief insert a new attribut for this fiche_def
00598      * \param $p_ad_id id of the attribut
00599      */
00600     function InsertAttribut($p_ad_id)
00601     {
00602         if ( $this->id == 0 ) return;
00603         /* ORDER */
00604         $this->GetAttribut();
00605         $max=sizeof($this->attribut)*15;
00606         // Insert a new attribute for the model
00607         // it means insert a row in jnt_fic_attr
00608         $sql=sprintf("insert into jnt_fic_attr (fd_id,ad_id,jnt_order) values (%d,%d,%d)",
00609                      $this->id,$p_ad_id,$max);
00610         $Res=$this->cn->exec_sql($sql);
00611         // update all the existing card
00612 
00613     }
00614     /*!\brief remove an attribut for this fiche_def
00615      * \param array of ad_id to remove
00616      * \remark you can't remove the attribut defined in attr_min
00617      */
00618     function RemoveAttribut($array)
00619     {
00620         foreach ($array as $ch)
00621         {
00622             $this->cn->start();
00623             $sql="delete from jnt_fic_attr where fd_id=$1 ".
00624                  "   and ad_id=$2";
00625             $this->cn->exec_sql($sql,array($this->id,$ch));
00626 
00627             $sql="delete from fiche_detail  where jft_id in ( select ".
00628                  " jft_id from fiche_Detail ".
00629                  " join fiche using(f_id) ".
00630                  " where ".
00631                  "fd_id = $1 and ".
00632                  "ad_id=$2)";
00633             $this->cn->exec_sql($sql,array($this->id,$ch));
00634 
00635             $this->cn->commit();
00636         }
00637     }
00638 
00639     /*!\brief save the order of a card, update the column jnt_fic_attr.jnt_order
00640      *\param $p_array containing the order
00641      */
00642     function save_order($p_array)
00643     {
00644         extract($p_array);
00645         $this->GetAttribut();
00646         foreach ($this->attribut as $row)
00647         {
00648             if ( $row->ad_id == 1 ) continue;
00649             if ( ${'jnt_order'.$row->ad_id} <= 0 ) continue;
00650             $sql='update jnt_fic_attr set jnt_order=$1 where fd_id=$2 and ad_id=$3';
00651             $this->cn->exec_sql($sql,array(${'jnt_order'.$row->ad_id},
00652                                            $this->id,
00653                                            $row->ad_id));
00654 
00655         }
00656         /* correct the order */
00657         $this->cn->exec_sql('select attribute_correct_order()');
00658     }
00659 
00660 
00661     /*!\brief remove all the card from a categorie after having verify
00662      *that the card is not used and then remove also the category
00663      *\return the remains items, not equal to 0 if a card remains and
00664      *then the category is not removed
00665      */
00666     function remove()
00667     {
00668         $remain=0;
00669         /* get all the card */
00670         $aFiche=fiche::get_fiche_def($this->cn,$this->id);
00671         if ( $aFiche != null )
00672         {
00673             /* check if the card is used */
00674             foreach ($aFiche as $dfiche)
00675             {
00676               $fiche=new Fiche($this->cn,$dfiche['f_id']);
00677 
00678                 /* if the card is not used then remove it otherwise increment remains */
00679                 if ( $fiche->is_used() == false )
00680                 {
00681                     $fiche->delete();
00682                 }
00683                 else
00684                     $remain++;
00685             }
00686         }
00687         /* if remains == 0 then remove cat */
00688         if ( $remain == 0 )
00689         {
00690             $sql='delete from jnt_fic_attr where fd_id=$1';
00691             $this->cn->exec_sql($sql,array($this->id));
00692             $sql='delete from fiche_def where fd_id=$1';
00693             $this->cn->exec_sql($sql,array($this->id));
00694         }
00695 
00696         return $remain;
00697 
00698     }
00699     /*!
00700      * \brief  retrieve the mandatory field of the card model
00701      *
00702      * \param $p_fiche_def_ref
00703      * \return array of ad_id  (attr_min.ad_id) and  labels (attr_def.ad_text)
00704      */
00705     function get_attr_min($p_fiche_def_ref)
00706     {
00707 
00708         // find the min attr for the fiche_def_ref
00709         $Sql="select ad_id,ad_text from attr_min natural join attr_def
00710              natural join fiche_def_ref
00711              where
00712              frd_id= $1";
00713         $Res=$this->cn->exec_sql($Sql,array($p_fiche_def_ref));
00714         $Num=Database::num_row($Res);
00715 
00716         // test the number of returned rows
00717         if ($Num == 0 ) return null;
00718 
00719         // Get Results & Store them in a array
00720         for ($i=0;$i<$Num;$i++)
00721         {
00722             $f=Database::fetch_array($Res,$i);
00723             $array[$i]['ad_id']=$f['ad_id'];
00724             $array[$i]['ad_text']=$f['ad_text'];
00725         }
00726         return $array;
00727     }
00728     /*!\brief count the number of fiche_def (category) which has the frd_id (type of category)
00729      *\param $p_frd_id is the frd_id in constant.php the FICHE_TYPE_
00730      *\return the number of cat. of card of the given type
00731      *\see constant.php
00732      */
00733     function count_category($p_frd_id)
00734     {
00735         $ret=$this->cn->count_sql("select fd_id from fiche_def where frd_id=$1",array($p_frd_id));
00736         return $ret;
00737     }
00738         function input_detail()
00739         {
00740                 $r = "";
00741                 // Save the label
00742 
00743                 $this->Get();
00744                 $this->GetAttribut();
00745                 $r.= '<H2 class="info">' . $this->id . " " . h($this->label) . '</H2>';
00746                 $r.='<fieldset><legend>Données générales</legend>';
00747 
00748                 /* show the values label class_base and create account */
00749                 $r.='<form method="post">';
00750                 $r.=dossier::hidden();
00751                 $r.=HtmlInput::hidden("fd_id", $this->id);
00752                 $r.=HtmlInput::hidden("p_action", "fiche");
00753                 $r.= $this->input_base();
00754                 $r.='<hr>';
00755                 $r.=HtmlInput::submit('change_name', _('Sauver'));
00756                 $r.='</form>';
00757                 $r.='</fieldset>';
00758                 /* attributes */
00759                 $r.='<fieldset><legend>Détails</legend>';
00760 
00761                 $r.= '<FORM  method="POST">';
00762                 $r.=dossier::hidden();
00763                 $r.=HtmlInput::hidden("fd_id", $this->id);
00764                 $r.= $this->DisplayAttribut("remove");
00765                 $r.= HtmlInput::submit('add_line', _('Ajoutez cet élément'));
00766                 $r.= HtmlInput::submit("save_line", _("Sauvez"));
00767                 $r.=HtmlInput::submit('remove_cat', _('Effacer cette catégorie'), 'onclick="return confirm(\'' . _('Vous confirmez ?') . '\')"');
00768                 // if there is nothing to remove then hide the button
00769                 if (strpos($r, "chk_remove") != 0)
00770                 {
00771                         $r.=HtmlInput::submit('remove_line', _("Enleve les éléments cochés"), "onclick=\"return confirm('Vous confirmez?')\"");
00772                 }
00773                 $r.= "</form>";
00774                 $r.=" <p class=\"notice\"> " . _("Attention : il n'y aura pas de demande de confirmation pour enlever les
00775                                    attributs sélectionnés. Il ne sera pas possible de revenir en arrière") . "</p>";
00776                 $r.='</fieldset>';
00777                 return $r;
00778         }
00779         function input_new()
00780         {
00781                 $single=new Tool_Uos("dup");
00782                 echo '<form method="post" style="display:inline">';
00783                 echo $single->hidden();
00784                 echo HtmlInput::hidden("p_action","fiche");
00785                 echo dossier::hidden();
00786                 echo $this->input(); //    CreateCategory($cn,$search);
00787                 echo HtmlInput::submit("add_modele" ,"Sauve");
00788                 echo '</FORM>';
00789         }
00790 
00791 }
00792 ?>