Skip to content

Commit

Permalink
Avoid flush by category selector
Browse files Browse the repository at this point in the history
| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets |
| Refs tickets  | #1561
| License       | MIT
| Doc PR        |
  • Loading branch information
rallek committed Feb 12, 2014
1 parent 64f0cd8 commit a2b3e3a
Showing 1 changed file with 30 additions and 22 deletions.
52 changes: 30 additions & 22 deletions src/lib/Zikula/Form/Plugin/CategorySelector.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class Zikula_Form_Plugin_CategorySelector extends Zikula_Form_Plugin_DropdownLis
*
* @return string
*/
public function getFilename()
function getFilename()
{
return __FILE__;
}
Expand All @@ -97,7 +97,7 @@ public function getFilename()
*
* @return void
*/
public static function loadParameters(&$list, $includeEmptyElement, $params)
static function loadParameters(&$list, $includeEmptyElement, $params)
{
$all = isset($params['all']) ? $params['all'] : false;
$lang = isset($params['lang']) ? $params['lang'] : ZLanguage::getLanguageCode();
Expand Down Expand Up @@ -163,12 +163,12 @@ public static function loadParameters(&$list, $includeEmptyElement, $params)
/**
* Load event handler.
*
* @param Zikula_Form_View $view Reference to Form render object.
* @param Zikula_Form_View $view Reference to Form render object.
* @param array &$params Parameters passed from the Smarty plugin function.
*
* @return void
*/
public function load(Zikula_Form_View $view, &$params)
function load(Zikula_Form_View $view, &$params)
{
$this->includeEmptyElement = (isset($params['includeEmptyElement']) ? $params['includeEmptyElement'] : false);
$this->enableDBUtil = (isset($params['enableDBUtil']) ? $params['enableDBUtil'] : false);
Expand All @@ -186,7 +186,7 @@ public function load(Zikula_Form_View $view, &$params)
*
* @return string The rendered output
*/
public function render(Zikula_Form_View $view)
function render(Zikula_Form_View $view)
{
$result = parent::render($view);

Expand All @@ -204,12 +204,12 @@ public function render(Zikula_Form_View $view)
* Called by the render when doing $render->getValues()
* Uses the group parameter to decide where to store data.
*
* @param Zikula_Form_View $view Reference to Form render object.
* @param Zikula_Form_View $view Reference to Form render object.
* @param array &$data Data object.
*
* @return void
*/
public function saveValue(Zikula_Form_View $view, &$data)
function saveValue(Zikula_Form_View $view, &$data)
{
if ($this->enableDBUtil && $this->dataBased) {
if ($this->group == null) {
Expand All @@ -220,7 +220,7 @@ public function saveValue(Zikula_Form_View $view, &$data)
}
$data[$this->group]['__CATEGORIES__'][$this->dataField] = $this->getSelectedValue();
}
} elseif ($this->enableDoctrine && $this->dataBased) {
} else if ($this->enableDoctrine && $this->dataBased) {
if ($this->group == null) {
$data['Categories'][$this->dataField] = array('category_id' => $this->getSelectedValue(),
'reg_property' => $this->dataField);
Expand All @@ -231,7 +231,7 @@ public function saveValue(Zikula_Form_View $view, &$data)
$data[$this->group]['Categories'][$this->dataField] = array('category_id' => $this->getSelectedValue(),
'reg_property' => $this->dataField);
}
} elseif ($this->doctrine2) {
} else if ($this->doctrine2) {
$entity = $view->get_template_vars($this->group);

// load category from db
Expand All @@ -247,21 +247,29 @@ public function saveValue(Zikula_Form_View $view, &$data)
}


if (is_array($this->getSelectedValue())) {
if(is_array($this->getSelectedValue())) {
$selectedValues = $this->getSelectedValue();
} else {
$selectedValues[] = $this->getSelectedValue();
}
$selectedValues = array_combine($selectedValues, $selectedValues);

foreach ($collection->getKeys() as $key) {
$categoryId = $collection->get($key)->getCategoryRegistryId();
if ($categoryId == $this->registryId) {
$collection->remove($key);
}
}
foreach ($collection->getKeys() as $key) {
$entityCategory = $collection->get($key);

if ($entityCategory->getCategoryRegistryId() == $this->registryId) {
$categoryId = $entityCategory->getCategory()->getId();

$em->flush();
if (isset($selectedValues[$categoryId])) {
unset($selectedValues[$categoryId]);
} else {
$collection->remove($key);
}
}
}

// we do NOT flush here, as the calling module is responsible for that (Guite)
//$em->flush();

foreach ($selectedValues as $selectedValue) {

Expand All @@ -280,12 +288,12 @@ public function saveValue(Zikula_Form_View $view, &$data)
* Called internally by the plugin itself to load values from the render.
* Can also by called when some one is calling the render object's Zikula_Form_View::setValues.
*
* @param Zikula_Form_View $view Reference to Zikula_Form_View render object.
* @param Zikula_Form_View $view Reference to Zikula_Form_View render object.
* @param array &$values Values to load.
*
* @return void
*/
public function loadValue(Zikula_Form_View $view, &$values)
function loadValue(Zikula_Form_View $view, &$values)
{
if ($this->enableDBUtil && $this->dataBased) {
$items = null;
Expand Down Expand Up @@ -316,7 +324,7 @@ public function loadValue(Zikula_Form_View $view, &$values)

$this->setSelectedValue($value);

} elseif ($this->enableDoctrine && $this->dataBased) {
} else if ($this->enableDoctrine && $this->dataBased) {
$items = null;
$value = null;

Expand Down Expand Up @@ -345,13 +353,13 @@ public function loadValue(Zikula_Form_View $view, &$values)

$this->setSelectedValue($value);

} elseif ($this->doctrine2) {
} else if ($this->doctrine2) {
if (isset($values[$this->group])) {
$entity = $values[$this->group];
if (isset($entity[$this->dataField])) {
$collection = $entity[$this->dataField];
$selectedValues = array();
foreach ($collection as $c) {
foreach($collection as $c) {
$categoryId = $c->getCategoryRegistryId();
if ($categoryId == $this->registryId) {
$selectedValues[] = $c->getCategory()->getId();
Expand Down

0 comments on commit a2b3e3a

Please sign in to comment.