Skip to content

Commit

Permalink
Fix feature to drag and drop rows of table (not generic)
Browse files Browse the repository at this point in the history
  • Loading branch information
eldy committed Nov 15, 2018
1 parent 7d5c995 commit 6d640ab
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 22 deletions.
25 changes: 22 additions & 3 deletions htdocs/admin/emailcollector_card.php
Original file line number Diff line number Diff line change
Expand Up @@ -457,9 +457,9 @@ function init_myfunc()
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">';
Expand All @@ -470,15 +470,20 @@ function init_myfunc()
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>';
Expand All @@ -487,12 +492,26 @@ function init_myfunc()
//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>';
Expand Down
6 changes: 4 additions & 2 deletions htdocs/core/ajax/row.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@
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);
Expand All @@ -53,6 +53,7 @@
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;
Expand All @@ -62,6 +63,7 @@
$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
Expand Down
18 changes: 12 additions & 6 deletions htdocs/core/class/commonobject.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -2379,7 +2379,7 @@ function line_down($rowid, $fk_parent_line=true)
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;
Expand Down Expand Up @@ -2417,14 +2417,17 @@ function line_ajaxorder($rows)
*/
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) )
{
Expand All @@ -2450,12 +2453,15 @@ function updateLineDown($rowid,$rang,$max)
{
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) )
{
Expand Down
11 changes: 8 additions & 3 deletions htdocs/core/tpl/ajaxrow.tpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -32,7 +37,7 @@
<!-- 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;
Expand Down
10 changes: 8 additions & 2 deletions htdocs/emailcollector/class/emailcollector.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,10 @@ public function fetchFilters()
{
$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)
Expand Down Expand Up @@ -631,7 +634,10 @@ public function fetchActions()
{
$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)
Expand Down
4 changes: 4 additions & 0 deletions htdocs/emailcollector/class/emailcollectoraction.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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')),
);
Expand Down
9 changes: 5 additions & 4 deletions htdocs/install/mysql/migration/8.0.0-9.0.0.sql
Original file line number Diff line number Diff line change
Expand Up @@ -180,12 +180,12 @@ 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(
CREATE TABLE llx_emailcollector_emailcollectorfilter(
-- 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,
rulevalue varchar(255) NULL,
date_creation datetime NOT NULL,
tms timestamp NOT NULL,
fk_user_creat integer NOT NULL,
Expand All @@ -195,16 +195,17 @@ CREATE TABLE llx_emailcollector_emailcollectoraction(
-- END MODULEBUILDER FIELDS
) ENGINE=innodb;

CREATE TABLE llx_emailcollector_emailcollectorfilter(
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,
rulevalue varchar(255) 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
2 changes: 1 addition & 1 deletion htdocs/variants/list.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ function( intIndex ) {
<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>
Expand Down

0 comments on commit 6d640ab

Please sign in to comment.