Skip to content

Commit

Permalink
fixed: deleteing repeat groups. Now the original repeat group pk values
Browse files Browse the repository at this point in the history
are stored in the form. Upon form submission any deleted group row's pk
value must be within this set of repeat group pk values. This fixes
issues FabrikGH-1254, and also the issue where records in the joined table
which were not shown due to a prefilter were being incorrectly deleted
  • Loading branch information
pollen8 committed Jul 29, 2014
1 parent 30d9f32 commit ae5b456
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 23 deletions.
20 changes: 10 additions & 10 deletions components/com_fabrik/models/form.php
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,7 @@ public function getFormCss()
$qs .= '&view=' . $v;
$qs .= '&rowid=' . $this->getRowId();
}

$tmplPath = 'templates/' . $app->getTemplate() . '/html/com_fabrik/' . $view . '/' . $tmpl . '/template_css.php' . $qs;

if (!FabrikHelperHTML::stylesheetFromPath($tmplPath))
Expand Down Expand Up @@ -1991,8 +1991,8 @@ public function addEncrytedVarsToArray(&$post)
// $$$ rob urldecode when posting from ajax form
$encrypted = urldecode($encrypted);
$v = empty($encrypted) ? '' : $crypt->decrypt($encrypted);
/*

/*
* $$$ hugh - things like elementlist elements (radios, etc) seem to use
* their JSON data for encrypted read only vals, need to decode.
*/
Expand Down Expand Up @@ -5339,24 +5339,24 @@ public function jsKey()

return $key;
}

/**
* Get a subset of the model's data with non accessible values removed
*
*
* @param string $view View
*
*
* @return array data
*/
public function accessibleData($view = 'form')
{
$accessibleData = $this->data;

$groups = $this->getGroupsHiarachy();

foreach ($groups as $groupModel)
{
$elementModels = $groupModel->getPublishedElements();

foreach ($elementModels as $elementModel)
{
switch ($view)
Expand All @@ -5371,7 +5371,7 @@ public function accessibleData($view = 'form')
$accessible = $elementModel->canView('list');
break;
}

if (!$accessible)
{
$name = $elementModel->getFullName(true, false);
Expand Down
70 changes: 57 additions & 13 deletions components/com_fabrik/models/group.php
Original file line number Diff line number Diff line change
Expand Up @@ -1278,10 +1278,62 @@ public function process()
}

// Delete any removed groups
$this->deleteRepeatGroups($usedKeys);

// Reset the list's table name
$list->db_table_name = $tblName;
$list->db_primary_key = $tblPk;
}

/**
* When storing a joined group. Delete any deselected repeating joined records
*
* @param array $usedKeys Keys saved in store()
*
* @return bool
*/
private function deleteRepeatGroups($usedKeys = array())
{
if (!$this->canRepeat())
{
/*
* If the group can not be repeated then the user could not have deleted a
* repeat group.
*/
return true;
}

$input = JFactory::getApplication()->input;
$listModel = $this->getListModel();
$list = $listModel->getTable();
$joinModel = $this->getJoinModel();
$join = $joinModel->getJoin();
$db = $listModel->getDb();
$query = $db->getQuery(true);
$masterInsertId = $this->masterInsertId();
$query->delete($list->db_table_name);
$query->delete($db->qn($list->db_table_name));
$pk = $join->params->get('pk');

/*
* Get the original row ids. We can ONLY delete from within this set. This
* allows us to respect and prefilter that was applied to the list's data.
*/
$groupid = $this->getId();
$origGroupRowsIds = $input->get('fabrik_group_rowids', array(), 'array');
$origGroupRowsIds = JArrayHelper::getValue($origGroupRowsIds, $groupid, array());
$origGroupRowsIds = json_decode($origGroupRowsIds);

/*
* Find out which keys were origionally in the form, but were not submitted
* i.e. those keys whose records were removed
*/
$keysToDelete = array_diff($origGroupRowsIds, $usedKeys);

// Nothing to delete - return
if (empty($keysToDelete))
{
return true;
}

if (is_array($masterInsertId))
{
Expand All @@ -1295,25 +1347,17 @@ public function process()
$mid = $db->quote($mid);
}

$query->where($join->table_join_key . ' IN (' . implode(', ', $masterInsertId) . ')');
$query->where($db->qn($join->table_join_key) . ' IN (' . implode(', ', $masterInsertId) . ')');
}
else
{
$query->where($join->table_join_key . ' = ' . $db->quote($masterInsertId));
}

if (!empty($usedKeys))
{
$pk = $join->params->get('pk');
$query->where('!(' . $pk . 'IN (' . implode(',', $usedKeys) . ')) ');
$query->where($db->qn($join->table_join_key) . ' = ' . $db->quote($masterInsertId));
}

$query->where($pk . 'IN (' . implode(',', $db->q($keysToDelete)) . ') ');
$db->setQuery($query);
$db->execute();

// Reset the list's table name
$list->db_table_name = $tblName;
$list->db_primary_key = $tblPk;
return $db->execute();
}

/**
Expand Down
10 changes: 10 additions & 0 deletions components/com_fabrik/views/form/view.base.php
Original file line number Diff line number Diff line change
Expand Up @@ -930,6 +930,16 @@ protected function _loadTmplBottom(&$form)

foreach ($groups as $groupModel)
{
if ($groupModel->isJoin())
{
$groupPk = $groupModel->getJoinModel()->getForeignId();
$groupRowIds = (array) JArrayHelper::getValue($this->data, $groupPk, array());
$groupRowIds = htmlentities(json_encode($groupRowIds));

// Used to check against in group process(), when deleting removed repeat groups
$fields[] = '<input type="hidden" name="fabrik_group_rowids[' . $groupModel->getId() . ']" value="' . $groupRowIds . '" />';
}

$group = $groupModel->getGroup();
$c = $groupModel->repeatTotal;

Expand Down

0 comments on commit ae5b456

Please sign in to comment.