diff --git a/htdocs/core/ajax/productonoff.php b/htdocs/core/ajax/productonoff.php new file mode 100644 index 0000000000000000000000000000000000000000..05eebc7a65fb59d79f0dd6939cc7179fc363f0ef --- /dev/null +++ b/htdocs/core/ajax/productonoff.php @@ -0,0 +1,52 @@ +<?php +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +/** + * \file htdocs/core/ajax/productonoff.php + * \brief File to set tosell and tobuy for product + */ + +if (! defined('NOTOKENRENEWAL')) define('NOTOKENRENEWAL','1'); // Disables token renewal +if (! defined('NOREQUIREMENU')) define('NOREQUIREMENU','1'); +if (! defined('NOREQUIREHTML')) define('NOREQUIREHTML','1'); +if (! defined('NOREQUIREAJAX')) define('NOREQUIREAJAX','1'); +if (! defined('NOREQUIRESOC')) define('NOREQUIRESOC','1'); +if (! defined('NOREQUIRETRAN')) define('NOREQUIRETRAN','1'); + +require '../../main.inc.php'; +require_once DOL_DOCUMENT_ROOT.'/core/class/genericobject.class.php'; + +$action=GETPOST('action','alpha'); +$id=GETPOST('id', 'int'); +$value=GETPOST('value', 'int'); + +$object = new GenericObject($db); + +/* + * View + */ + +top_httphead(); + +print '<!-- Ajax page called with url '.$_SERVER["PHP_SELF"].'?'.$_SERVER["QUERY_STRING"].' -->'."\n"; + +// Registering new values +if (! empty($action) && ! empty($id) && $user->rights->produit->creer) { + if ($action == 'setstatus') + $object->setValueFrom('tosell', $value, 'product', $id); + else if ($action == 'setstatus_buy') + $object->setValueFrom('tobuy', $value, 'product', $id); +} diff --git a/htdocs/core/lib/ajax.lib.php b/htdocs/core/lib/ajax.lib.php index 9081fba5550d04893190afad59683dd6e8dea09c..4bc6da083aa296e9b44988acfca1a581d2f27f37 100644 --- a/htdocs/core/lib/ajax.lib.php +++ b/htdocs/core/lib/ajax.lib.php @@ -459,3 +459,91 @@ function ajax_constantonoff($code, $input=array(), $entity=null, $revertonoff=0, return $out; } +/** + * On/off button for product tosell or tobuy + * + * @param int $id Id product to set + * @param string $code Name of constant : status or status_buy + * @param array $input Array of type->list of CSS element to switch. Example: array('disabled'=>array(0=>'cssid')) + * @return void + */ +function ajax_productonoff($id, $code, $input=array()) +{ + require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php'; + global $conf, $langs, $db; + + $object = new Product($db); + $object->fetch($id); + + $out= '<script type="text/javascript"> + $(function() { + var input = '.json_encode($input).'; + + // Set constant + $("#set_'.$code.'").click(function() { + $.get( "'.DOL_URL_ROOT.'/core/ajax/productonoff.php", { + action: \'set'.$code.'\', + value: \'1\', + id: \''.$id.'\' + }, + function() { + $("#set_'.$code.'").hide(); + $("#del_'.$code.'").show(); + // Enable another element + if (input.disabled && input.disabled.length > 0) { + $.each(input.disabled, function(key,value) { + $("#" + value).removeAttr("disabled"); + if ($("#" + value).hasClass("butActionRefused") == true) { + $("#" + value).removeClass("butActionRefused"); + $("#" + value).addClass("butAction"); + } + }); + // Show another element + } else if (input.showhide && input.showhide.length > 0) { + $.each(input.showhide, function(key,value) { + $("#" + value).show(); + }); + } + }); + }); + + // Del constant + $("#del_'.$code.'").click(function() { + $.get( "'.DOL_URL_ROOT.'/core/ajax/productonoff.php", { + action: \'set'.$code.'\', + value: \'0\', + id: \''.$id.'\' + }, + function() { + $("#del_'.$code.'").hide(); + $("#set_'.$code.'").show(); + // Disable another element + if (input.disabled && input.disabled.length > 0) { + $.each(input.disabled, function(key,value) { + $("#" + value).attr("disabled", true); + if ($("#" + value).hasClass("butAction") == true) { + $("#" + value).removeClass("butAction"); + $("#" + value).addClass("butActionRefused"); + } + }); + // Hide another element + } else if (input.showhide && input.showhide.length > 0) { + $.each(input.showhide, function(key,value) { + $("#" + value).hide(); + }); + } + }); + }); + }); + </script>'; + if ($code=='status') { + $out.= '<span id="set_'.$code.'" class="linkobject '.($object->$code==1?'hideobject':'').'">'.img_picto($langs->trans("ProductStatusNotOnSell"),'switch_off').'</span>'; + $out.= '<span id="del_'.$code.'" class="linkobject '.($object->$code==1?'':'hideobject').'">'.img_picto($langs->trans("ProductStatusOnSell"),'switch_on').'</span>'; + } + if ($code=='status_buy') { + $out.= '<span id="set_'.$code.'" class="linkobject '.($object->$code==1?'hideobject':'').'">'.img_picto($langs->trans("ProductStatusNotOnBuy"),'switch_off').'</span>'; + $out.= '<span id="del_'.$code.'" class="linkobject '.($object->$code==1?'':'hideobject').'">'.img_picto($langs->trans("ProductStatusOnBuy"),'switch_on').'</span>'; + } + return $out; +} + diff --git a/htdocs/product/card.php b/htdocs/product/card.php index a9f533ba40b4da4b90f7d687c7ed1e0e880f51dd..5644e41df1cc764928aef47e6aa5c417a2f47c75 100644 --- a/htdocs/product/card.php +++ b/htdocs/product/card.php @@ -1420,12 +1420,20 @@ else // Status (to sell) print '<tr><td>'.$langs->trans("Status").' ('.$langs->trans("Sell").')</td><td colspan="2">'; - print $object->getLibStatut(2,0); + if (! empty($conf->use_javascript_ajax) && $user->rights->produit->creer) { + print ajax_productonoff($object->id, 'status'); + } else { + print $object->getLibStatut(2,0); + } print '</td></tr>'; // Status (to buy) print '<tr><td>'.$langs->trans("Status").' ('.$langs->trans("Buy").')</td><td colspan="2">'; - print $object->getLibStatut(2,1); + if (! empty($conf->use_javascript_ajax) && $user->rights->produit->creer) { + print ajax_productonoff($object->id, 'status_buy'); + } else { + print $object->getLibStatut(2,1); + } print '</td></tr>'; // Batch number management (to batch)