Fix feature to drag and drop rows of table (not generic)

This commit is contained in:
Laurent Destailleur 2018-11-15 19:58:06 +01:00
parent 7d5c9959bc
commit 6d640ab83c
9 changed files with 77 additions and 33 deletions

View File

@ -457,9 +457,9 @@ if ($object->id > 0 && (empty($action) || ($action != 'edit' && $action != 'crea
print '<div class="clearboth"></div><br>';
// Operations
print '<table class="border centpercent">';
print '<table id="tablelines" class="noborder noshadow">';
print '<tr class="liste_titre">';
print '<td>'.$langs->trans("EmailcollectorOperations").'</td><td></td><td></td>';
print '<td>'.$langs->trans("EmailcollectorOperations").'</td><td></td><td></td><td></td>';
print '</tr>';
// Add operation
print '<tr class="oddeven">';
@ -470,15 +470,20 @@ if ($object->id > 0 && (empty($action) || ($action != 'edit' && $action != 'crea
print '</td><td>';
print '<input type="text" name="operationparam">';
print '</td>';
print '<td></td>';
print '<td align="right"><input type="submit" name="addoperation" id="addoperation" class="flat button" value="'.$langs->trans("Add").'"></td>';
print '</tr>';
// List operations
$nboflines = count($object->actions);
$table_element_line = 'emailcollector_emailcollectoraction';
$fk_element='position';
$i=0;
foreach($object->actions as $ruleaction)
{
$ruleactionobj=new EmailcollectorAction($db);
$ruleactionobj->fetch($ruleaction['id']);
print '<tr class="oddeven">';
print '<tr class="drag drop oddeven" id="row-'.$ruleaction['id'].'">';
print '<td>';
print $langs->trans($arrayoftypes[$ruleaction['type']]);
print '</td>';
@ -487,12 +492,26 @@ if ($object->id > 0 && (empty($action) || ($action != 'edit' && $action != 'crea
//print $ruleactionobj->getLibStatut(3);
print ' <a href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&action=deleteoperation&operationid='.$ruleaction['id'].'">'.img_delete().'</a>';
print '</td>';
print '<td class="center linecolmove tdlineupdown">';
if ($i > 0)
{
print '<a class="lineupdown" href="'.$_SERVER['PHP_SELF'].'?action=up&amp;rowid='.$ruleaction['id'].'">'.img_up('default', 0, 'imgupforline').'</a>';
}
if ($i < count($object->actions)-1) {
print '<a class="lineupdown" href="'.$_SERVER['PHP_SELF'].'?action=down&amp;rowid='.$ruleaction['id'].'">'.img_down('default', 0, 'imgdownforline').'</a>';
}
print '</td>';
print '</tr>';
$i++;
}
print '</tr>';
print '</table>';
if (! empty($conf->use_javascript_ajax)) {
include DOL_DOCUMENT_ROOT . '/core/tpl/ajaxrow.tpl.php';
}
print '</form>';
print '</div>';

View File

@ -42,8 +42,8 @@ top_httphead();
print '<!-- Ajax page called with url '.dol_escape_htmltag($_SERVER["PHP_SELF"]).'?'.dol_escape_htmltag($_SERVER["QUERY_STRING"]).' -->'."\n";
// Registering the location of boxes
if ((isset($_POST['roworder']) && ! empty($_POST['roworder'])) && (isset($_POST['table_element_line']) && ! empty($_POST['table_element_line']))
&& (isset($_POST['fk_element']) && ! empty($_POST['fk_element'])) && (isset($_POST['element_id']) && ! empty($_POST['element_id'])) )
if ((! empty($_POST['roworder'])) && (! empty($_POST['table_element_line']))
&& (! empty($_POST['fk_element'])) && (! empty($_POST['element_id'])))
{
$roworder=GETPOST('roworder','alpha',2);
$table_element_line=GETPOST('table_element_line','alpha',2);
@ -53,6 +53,7 @@ if ((isset($_POST['roworder']) && ! empty($_POST['roworder'])) && (isset($_POST[
dol_syslog("AjaxRow roworder=".$roworder." table_element_line=".$table_element_line." fk_element=".$fk_element." element_id=".$element_id, LOG_DEBUG);
$rowordertab = explode(',',$roworder);
$newrowordertab = array();
foreach($rowordertab as $value)
{
if (! empty($value)) $newrowordertab[] = $value;
@ -62,6 +63,7 @@ if ((isset($_POST['roworder']) && ! empty($_POST['roworder'])) && (isset($_POST[
$row->table_element_line = $table_element_line;
$row->fk_element = $fk_element;
$row->id = $element_id;
$row->line_ajaxorder($newrowordertab); // This update field rank or position in table row->table_element_line
// Reorder line to have position of children lines sharing same counter than parent lines

View File

@ -2379,7 +2379,7 @@ abstract class CommonObject
function updateRangOfLine($rowid,$rang)
{
$fieldposition = 'rang';
if ($this->table_element_line == 'ecm_files') $fieldposition = 'position';
if (in_array($this->table_element_line, array('ecm_files', 'emailcollector_emailcollectoraction'))) $fieldposition = 'position';
$sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element_line.' SET '.$fieldposition.' = '.$rang;
$sql.= ' WHERE rowid = '.$rowid;
@ -2417,14 +2417,17 @@ abstract class CommonObject
*/
function updateLineUp($rowid,$rang)
{
if ($rang > 1 )
if ($rang > 1)
{
$sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element_line.' SET rang = '.$rang ;
$fieldposition = 'rang';
if (in_array($this->table_element_line, array('ecm_files', 'emailcollector_emailcollectoraction'))) $fieldposition = 'position';
$sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element_line.' SET '.$fieldposition.' = '.$rang ;
$sql.= ' WHERE '.$this->fk_element.' = '.$this->id;
$sql.= ' AND rang = '.($rang - 1);
if ($this->db->query($sql) )
{
$sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element_line.' SET rang = '.($rang - 1);
$sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element_line.' SET '.$fieldposition.' = '.($rang - 1);
$sql.= ' WHERE rowid = '.$rowid;
if (! $this->db->query($sql) )
{
@ -2450,12 +2453,15 @@ abstract class CommonObject
{
if ($rang < $max)
{
$sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element_line.' SET rang = '.$rang;
$fieldposition = 'rang';
if (in_array($this->table_element_line, array('ecm_files', 'emailcollector_emailcollectoraction'))) $fieldposition = 'position';
$sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element_line.' SET '.$fieldposition.' = '.$rang;
$sql.= ' WHERE '.$this->fk_element.' = '.$this->id;
$sql.= ' AND rang = '.($rang+1);
if ($this->db->query($sql) )
{
$sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element_line.' SET rang = '.($rang+1);
$sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element_line.' SET '.$fieldposition.' = '.($rang+1);
$sql.= ' WHERE rowid = '.$rowid;
if (! $this->db->query($sql) )
{

View File

@ -16,8 +16,13 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Javascript code to activate drag and drop on lines
* You can use this if you want to be abale to drag and drop rows of a table.
* You must add id="tablelines" on table level tag and have ($nboflines or count($object->lines) or count($taskarray) > 0)
* You can use this if you want to be able to drag and drop rows of a table.
* You must add id="tablelines" on table level tag
* and $object and $object->id is defined
* and $object->fk_element or $fk_element is defined
* and have ($nboflines or count($object->lines) or count($taskarray) > 0)
* and have $table_element_line = 'tablename' or $object->table_element_line with line to move
*
*/
// Protection to avoid direct call of template
@ -32,7 +37,7 @@ if (empty($object) || ! is_object($object))
<!-- BEGIN PHP TEMPLATE AJAXROW.TPL.PHP - Script to enable drag and drop on lines of a table -->
<?php
$id=$object->id;
$fk_element=$object->fk_element;
$fk_element=empty($object->fk_element)?$fk_element:$object->fk_element;
$table_element_line=(empty($table_element_line)?$object->table_element_line:$table_element_line);
$nboflines=(isset($object->lines)?count($object->lines):(isset($tasksarray)?count($tasksarray):(empty($nboflines)?0:$nboflines)));
$forcereloadpage=empty($conf->global->MAIN_FORCE_RELOAD_PAGE)?0:1;

View File

@ -602,7 +602,10 @@ class EmailCollector extends CommonObject
{
$this->filters = array();
$sql='SELECT rowid, type, rulevalue, status FROM '.MAIN_DB_PREFIX.'emailcollector_emailcollectorfilter WHERE fk_emailcollector = '.$this->id;
$sql = 'SELECT rowid, type, rulevalue, status';
$sql.= ' FROM '.MAIN_DB_PREFIX.'emailcollector_emailcollectorfilter';
$sql.= ' WHERE fk_emailcollector = '.$this->id;
//$sql.= ' ORDER BY position';
$resql = $this->db->query($sql);
if ($resql)
@ -631,7 +634,10 @@ class EmailCollector extends CommonObject
{
$this->actions = array();
$sql='SELECT rowid, type, actionparam, status FROM '.MAIN_DB_PREFIX.'emailcollector_emailcollectoraction WHERE fk_emailcollector = '.$this->id;
$sql = 'SELECT rowid, type, actionparam, status';
$sql.= ' FROM '.MAIN_DB_PREFIX.'emailcollector_emailcollectoraction';
$sql.= ' WHERE fk_emailcollector = '.$this->id;
$sql.= ' ORDER BY position';
$resql = $this->db->query($sql);
if ($resql)

View File

@ -58,6 +58,9 @@ class EmailCollectorAction extends CommonObject
public $picto = 'emailcollectoraction@emailcollector';
public $fk_element = 'fk_emailcollector';
/**
* 'type' if the field format.
* 'label' the translation key.
@ -90,6 +93,7 @@ class EmailCollectorAction extends CommonObject
'tms' => array('type'=>'timestamp', 'label'=>'DateModification', 'enabled'=>1, 'visible'=>-2, 'position'=>501, 'notnull'=>1,),
'fk_user_creat' => array('type'=>'integer', 'label'=>'UserAuthor', 'enabled'=>1, 'visible'=>-2, 'position'=>510, 'notnull'=>1, 'foreignkey'=>'llx_user.rowid',),
'fk_user_modif' => array('type'=>'integer', 'label'=>'UserModif', 'enabled'=>1, 'visible'=>-2, 'position'=>511, 'notnull'=>-1,),
'position' => array('type'=>'integer', 'label'=>'Position', 'enabled'=>1, 'visible'=>1, 'position'=>600, 'default'=>'0',),
'import_key' => array('type'=>'varchar(14)', 'label'=>'ImportId', 'enabled'=>1, 'visible'=>-2, 'position'=>1000, 'notnull'=>-1,),
'status' => array('type'=>'integer', 'label'=>'Status', 'enabled'=>1, 'visible'=>1, 'position'=>1000, 'notnull'=>1, 'default'=>1, 'arrayofkeyval'=>array('0'=>'Disabled', '1'=>'Enabled')),
);

View File

@ -180,21 +180,6 @@ ALTER TABLE llx_emailcollector_emailcollector ADD INDEX idx_emailcollector_entit
ALTER TABLE llx_emailcollector_emailcollector ADD INDEX idx_emailcollector_status (status);
CREATE TABLE llx_emailcollector_emailcollectoraction(
-- BEGIN MODULEBUILDER FIELDS
rowid integer AUTO_INCREMENT PRIMARY KEY NOT NULL,
fk_emailcollector INTEGER NOT NULL,
type varchar(128) NOT NULL,
actionparam varchar(255) NULL,
date_creation datetime NOT NULL,
tms timestamp NOT NULL,
fk_user_creat integer NOT NULL,
fk_user_modif integer,
import_key varchar(14),
status integer NOT NULL
-- END MODULEBUILDER FIELDS
) ENGINE=innodb;
CREATE TABLE llx_emailcollector_emailcollectorfilter(
-- BEGIN MODULEBUILDER FIELDS
rowid integer AUTO_INCREMENT PRIMARY KEY NOT NULL,
@ -210,6 +195,22 @@ CREATE TABLE llx_emailcollector_emailcollectorfilter(
-- END MODULEBUILDER FIELDS
) ENGINE=innodb;
CREATE TABLE llx_emailcollector_emailcollectoraction(
-- BEGIN MODULEBUILDER FIELDS
rowid integer AUTO_INCREMENT PRIMARY KEY NOT NULL,
fk_emailcollector INTEGER NOT NULL,
type varchar(128) NOT NULL,
actionparam varchar(255) NULL,
date_creation datetime NOT NULL,
tms timestamp NOT NULL,
fk_user_creat integer NOT NULL,
fk_user_modif integer,
position integer DEFAULT 0,
import_key varchar(14),
status integer NOT NULL
-- END MODULEBUILDER FIELDS
) ENGINE=innodb;
ALTER TABLE llx_emailcollector_emailcollectorfilter ADD INDEX idx_emailcollector_fk_emailcollector (fk_emailcollector);
ALTER TABLE llx_emailcollector_emailcollectorfilter ADD CONSTRAINT fk_emailcollectorfilter_fk_emailcollector FOREIGN KEY (fk_emailcollector) REFERENCES llx_emailcollector_emailcollector(rowid);

View File

@ -24,7 +24,8 @@ CREATE TABLE llx_emailcollector_emailcollectoraction(
tms timestamp NOT NULL,
fk_user_creat integer NOT NULL,
fk_user_modif integer,
import_key varchar(14),
position integer DEFAULT 0,
import_key varchar(14),
status integer NOT NULL
-- END MODULEBUILDER FIELDS
) ENGINE=innodb;

View File

@ -130,7 +130,7 @@ $forcereloadpage=empty($conf->global->MAIN_FORCE_RELOAD_PAGE)?0:1;
<a href="card.php?id=<?php echo $attribute->id ?>&action=edit"><?php echo img_edit() ?></a>
<a href="card.php?id=<?php echo $attribute->id ?>&action=delete"><?php echo img_delete() ?></a>
</td>
<td align="center" class="linecolmove tdlineupdown">
<td class="center linecolmove tdlineupdown">
<?php if ($key > 0): ?>
<a class="lineupdown"
href="<?php echo $_SERVER['PHP_SELF'] ?>?action=up&amp;rowid=<?php echo $attribute->id ?>"><?php echo img_up('default', 0, 'imgupforline'); ?></a>