diff --git a/html/ajax_misc.php b/html/ajax_misc.php index a2a4362d7..984ebb915 100644 --- a/html/ajax_misc.php +++ b/html/ajax_misc.php @@ -211,7 +211,9 @@ $path = array( // delete operation 'delete_search_operation'=>'ajax_search_filter', // template category of card - 'template_cat_card'=>'ajax_template_cat_card' + 'template_cat_card'=>'ajax_template_cat_card', + // Attribute for category of card + 'template_cat_category'=>'ajax_template_cat_category' ) ; if (array_key_exists($op, $path)) { diff --git a/html/js/ajax_fiche.js b/html/js/ajax_fiche.js index 28e7afb0b..ac6033c8b 100644 --- a/html/js/ajax_fiche.js +++ b/html/js/ajax_fiche.js @@ -250,4 +250,72 @@ function ajax_get_failure(request,json) } +var category_card={}; + +/** + * Add an attribute selected in "sel"+p_object_name into the list (id:p_object_name+"_list") + * this attribut will have the ID:p_object_name+"_elt"+ad_id (ad_id = attr_def.ad_id) + * @param int p_dossier dossier nb + * @param int p_fiche_def_ref is the frd_id + * @param string p_object_name , name of the prefix for id + */ +category_card.add_attribut=function (p_dossier,p_fiche_def_ref,p_object_name) { + var select=$("sel"+p_object_name); + var selected_attr=select.value; + new Ajax.Request("ajax_misc.php",{ + method:"post", + parameters:{"gDossier":p_dossier, + "objname":p_object_name, + "op":"template_cat_category", + "action":"add_attribute", + "frd_id":p_fiche_def_ref, + "ad_id":selected_attr + }, + onSuccess:function(req) { + var answer=req.responseText.evalJSON(); + if ( answer.status == 'OK') { + var newli=document.createElement("li") + newli.setAttribute("id",p_object_name+"_elt"+selected_attr); + newli.innerHTML=answer.content + $(p_object_name+"_list").append(newli); + select.remove(select.selectedIndex); + } else { + smoke.alert(answer.message); + } + } + }); +}; +/** + * Remove an attribute (id:p_object_name+"_elt"+ad_id (ad_id = attr_def.ad_id)) + * from the list (id:p_object_name+"list") + * @param int p_dossier dossier nb + * @param string p_object_name , name of the prefix for id + * @param int p_fiche_def_ref is the frd_id + * @param {type} p_attribute_id + */ +category_card.remove_attribut=function (p_dossier,p_fiche_def_ref,p_object_name,p_attribute_id) { + new Ajax.Request("ajax_misc.php",{ + method:"post", + parameters:{"gDossier":p_dossier, + "objname":p_object_name, + "op":"template_cat_category", + "action":"remove_attribute", + "frd_id":p_fiche_def_ref, + "ad_id":p_attribute_id + }, + onSuccess:function(req) { + var answer=req.responseText.evalJSON(); + if ( answer.status == 'OK') { + $(p_object_name+"_elt"+p_attribute_id).remove(); + var option=document.createElement("option"); + option.text=answer['content']; + option.value=p_attribute_id; + $('sel'+p_object_name).add(option); + } else { + smoke.alert(answer.message); + } + } + }); +}; + //--> diff --git a/include/ajax/ajax_template_cat_category.php b/include/ajax/ajax_template_cat_category.php new file mode 100644 index 000000000..32f22d9e2 --- /dev/null +++ b/include/ajax/ajax_template_cat_category.php @@ -0,0 +1,120 @@ + + +if (!defined('ALLOWED')) + die('Appel direct ne sont pas permis'); + +/** + * @file + * @brief manage attribut of a Template of Category of card. The answer must be + * in json + */ +$answer=[]; +$answer['status']="NOK"; +$answer['content']=""; +$answer['message']=_("Commande inconnue"); + + +/** + * security + */ +try +{ + if ($g_user->check_module("CFGCARDCAT")==0) + throw new Exception(_("Accès non autorisé")); + $http=new HttpInput(); + $action=$http->request("action"); + $ad_id=$http->request("ad_id", "number"); + $frd_id=$http->request("frd_id", "number"); + $objname=$http->request("objname"); +} +catch (Exception $ex) +{ + $answer['message']=_("Accès non autorisé"); + header("Content-type: text/json; charset: utf8", true); + echo json_encode($answer, + JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES|JSON_NUMERIC_CHECK); + return; +} +switch ($action) +{ + case "add_attribute": + try + { + if ($cn->get_value("select count(*) from attr_min where frd_id=$1 and ad_id=$2", + [$frd_id, $ad_id])>0) + throw new Exception(_("Attribut déjà ajouté")); + $cn->exec_sql("insert into attr_min (frd_id,ad_id) values ($1,$2)", + [$frd_id, $ad_id]); + $answer['status']="OK"; + $answer['message']=""; + $js=sprintf("category_card.remove_attribut('%s','%s','%s',%d)", + Dossier::id(), $frd_id, $objname, $ad_id); + $answer['content']=$cn->get_value("select ad_text from attr_def where ad_id=$1", + [$ad_id]). + HtmlInput::anchor(SMALLX, "javascript:void(0)", $js, + ' class="smallbutton" style="padding:0px;display:inline" '); + } + catch (Exception $exc) + { + echo $exc->getMessage(); + error_log($exc->getTraceAsString()); + $answer['message']=$exc->getMessage(); + } + + + break; + case "remove_attribute": + try + { + if ($cn->get_value("select count(*) from jnt_fic_attr + join fiche_def using (fd_id) + where frd_id=$1 and ad_id=$2", + [$frd_id, $ad_id])>0) + throw new Exception(_("Attribut déjà utilisé")); + if (in_array($ad_id, [ATTR_DEF_NAME,ATTR_DEF_QUICKCODE]) ) + { + throw new Exception(_("Attribut obligatoire")); + } + $answer['content']=$cn->get_value("select ad_text from attr_def where ad_id=$1", + [$ad_id]); + $answer['status']="OK"; + $answer['message']=""; + $cn->exec_sql("delete from attr_min where frd_id=$1 and ad_id=$2", + [$frd_id,$ad_id]); + } + catch (Exception $exc) + { + echo $exc->getMessage(); + error_log($exc->getTraceAsString()); + $answer['message']=$exc->getMessage(); + } + break; + + default: + break; +} + + +header("Content-type: text/json; charset: utf8", true); +echo json_encode($answer, + JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES|JSON_NUMERIC_CHECK); +return; diff --git a/include/class/template_card_category.class.php b/include/class/template_card_category.class.php index 81b6f6fe5..afaaf493f 100644 --- a/include/class/template_card_category.class.php +++ b/include/class/template_card_category.class.php @@ -33,6 +33,7 @@ require_once NOALYSS_INCLUDE.'/database/fiche_def_ref_sql.class.php'; */ class Template_Card_Category extends Manage_Table_SQL { + function __construct(Fiche_def_ref_SQL $p_table) { $this->table=$p_table; @@ -43,17 +44,21 @@ class Template_Card_Category extends Manage_Table_SQL $this->set_col_label("frd_id", _("ID")); // Cannot update frd_id $this->set_property_updatable("frd_id", FALSE); - $this->a_order=["frd_id","frd_text","frd_class_base"]; + $this->a_order=["frd_id", "frd_text", "frd_class_base"]; } + function delete() { $cn=Dossier::connect(); - - if ( $cn->get_value("select count(*) from fiche_def where frd_id=$1",[$this->table->frd_id])>0) + + if ($cn->get_value("select count(*) from fiche_def where frd_id=$1", + [$this->table->frd_id])>0) { throw new Exception(_("Effacement impossible : catégorie utilisée")); } + $this->table->delete(); } + /** * Check before inserting or updating, return TRUE if ok otherwise FALSE. * @return boolean @@ -62,23 +67,116 @@ class Template_Card_Category extends Manage_Table_SQL { $cn=Dossier::connect(); $error=0; - if ( trim($this->table->frd_text) == "" ) { - $this->set_error("frd_text",_("Le nom ne peut pas être vide")); + if (trim($this->table->frd_text)=="") + { + $this->set_error("frd_text", _("Le nom ne peut pas être vide")); $error++; } - if ( trim($this->table->frd_class_base) != "" ) { + if (trim($this->table->frd_class_base)!="") + { $cnt=$cn->get_value("select count(*) from tmp_pcmn where pcm_val=$1" - ,[$this->table->frd_class_base]); - if ($cnt == 0) { - $this->set_error("frd_class_base",_("Poste comptable n'existe pas")); + , [$this->table->frd_class_base]); + if ($cnt==0) + { + $this->set_error("frd_class_base", + _("Poste comptable n'existe pas")); $error++; } } - - if ( $error != 0) { + + if ($error!=0) + { return false; } return true; - } -} \ No newline at end of file + + /** + * @brief display into a dialog box the datarow in order + * to be appended or modified. Can be override if you need + * a more complex form + */ + function input() + { + echo "
"; + echo _("Attention, ne pas changer la signification de ce poste."); + echo hi(_("par exemple ne pas changer Client par fournisseur"))."
"; + echo _("sinon le programme fonctionnera mal, ". + "utiliser uniquement des chiffres pour la classe de base ou rien")."
"; + parent::input(); + + /** + * Add / Remove attribut Minimum + */ + if ($this->table->frd_id!=-1) + { + echo h2(_("Attribut minimum pour les catégories de fiches")); + $cn=Dossier::connect(); + $dossier_id=Dossier::id(); + $objname=$this->get_object_name(); + $a_attribut=$cn->get_array("select ad_id,ad_text,ad_type from attr_min join attr_def using (ad_id) where frd_id=$1 order by 2", + [$this->table->frd_id]); + $nb_attribut=count($a_attribut); + printf(''; + // Add some attribute if not used + if ($used==0) + { + $sel_attribut=new ISelect("sel".$this->get_object_name()); + $sel_attribut->value=$cn->make_array("select ad_id,ad_text + from attr_def + where + not exists (select 1 + from + attr_min + where + frd_id=$1 and ad_id=attr_def.ad_id)", NULL, + [$this->table->frd_id]); + echo _("Attribut à ajouter"); + echo $sel_attribut->input(); + $js_script=sprintf("category_card.add_attribut('%s','%s','%s')", + $dossier_id, $this->table->frd_id, $objname); + echo HtmlInput::button_image($js_script, uniqid(), + 'class="smallbutton image_search"', + "image/bouton-plus.png"); + } + } + } + + /** + * When adding a template of category of card, the minimum is the name + * and the quickcode, which must be added into attr_min + */ + function add_mandatory_attr() + { + $cn=Dossier::connect(); + $frd_id=$this->table->frd_id; + $cn->exec_sql("insert into attr_min (frd_id,ad_id) values ($1,$2)", + [$frd_id, ATTR_DEF_NAME]); + $cn->exec_sql("insert into attr_min (frd_id,ad_id) values ($1,$2)", + [$frd_id, ATTR_DEF_QUICKCODE]); + } + +}