$this->redirect('category/edit?id='.$general_category->getId());
}
}
+
+ public function executeChoose($request)
+ {
+ //Get user Id
+ $userId = $this->getUser()->getGuardUser()->getId();
+
+ //Get data from user
+ $checked = $request->getParameter('checked');
+
+ //We retrieve a Doctrine_Collection
+ $userBaskets = UserBasketTable::getInstance()->findByUserId($userId);
+
+
+ //Using Doctrine_Collection_Iterator
+ $iterator = $userBaskets->getIterator();
+ while ($userBasket = $iterator->current())
+ {
+ if (!empty($checked))
+ {
+ foreach ($checked as $index => $value)
+ {
+ if ($userBasket->getGeneralCategId() == $value)
+ {
+ unset($checked[$index]);
+ $iterator->next();
+ continue 2;
+ }
+ }
+ }
+ $userBaskets->remove($iterator->key());
+ $iterator->next();
+ }
+
+ if (!empty($checked))
+ {
+ foreach ($checked as $index => $value)
+ {
+ //Never trust in data coming from users... Performance vs security.
+ $generalCategory = GeneralCategoryTable::getInstance()->findOneById($value);
+ if ($generalCategory != null)
+ {
+ $userBasket = new UserBasket();
+ $userBasket->general_categ_id = $generalCategory->getId();
+ $userBasket->user_id = $userId;
+ $userBaskets->add($userBasket);
+ }
+ }
+ }
+
+ //The Doctrine_Collection should just insert/remove in the database in this point (never before)
+ //This feature is really nice (if it works as intended) I have no time for checking out its behaviour...
+ $userBaskets->save();
+
+ //set content type HTTP field with the right value (we are going to use a JSON response)
+ $this->getResponse()->setContentType('application/json');
+
+ //Bypass completely the view layer and set the response code directly from this action.
+ //In this way the user may know if the data were updated
+ return $this->renderText(json_encode($checked));
+ }
}
});
</script>
<script type="text/javascript">
- $.fn.UpdateCategories = function() {
- var allVals = [];
+ $.fn.CheckCategories = function() {
+ var checked = [];
$('#rounded-corner :checked').each(function() {
- allVals.push($(this).val());
+ checked.push($(this).val());
});
- alert(allVals);
- $.post('prueba.html', {'choices': allVals}, function(data){ alert(data);} ,"json");
+ $.post('<?php echo url_for('category/choose') ?>', {'checked[]': checked},
+ function(data){
+ alert("hola");
+ }, "json");
};
</script>
<thead>
<tr>
<th scope="col" class="rounded-company"><?php echo __('General Category Name') ?></th>
- <th scope="col" class="rounded"><?php echo __('Selected') ?></th>
- <th scope="col" class="rounded-q4"></th>
+ <th scope="col" class="rounded-q4"><?php echo __('Selected') ?></th>
</tr>
</thead>
<tbody>
}
?>>
<td><a><?php echo $category ?></a></td>
- <td><input type="checkbox" id="chosen" value="<?php echo $category->getId() ?>"
+ <td><input type="checkbox" id="chosen" class="NFCheck" value="<?php echo $category->getId() ?>"
<?php foreach ($category->getUserBaskets() as $userBasket): ?>
<?php if ($userBasket->getUserId() == $userId): ?>
checked
</tbody>
</table>
-<input type="button" value="Update" onClick="$(this).UpdateCategories();"/>
+<input type="button" class="NFButtonToRight" value="<?php echo __('Select') ?>" onClick="$(this).CheckCategories();"/>
*/
public function getGeneralCategoriesByLftQuery()
{
- return $this->createQuery('gc')->orderBy('gc.lft');
+ return $this->createQuery('gc')->where('gc.id != 1')
+ ->orderBy('gc.lft');
}
}
.NFCheck {cursor:pointer; position:absolute;width:20px; margin:0px 0 0 0; height:20px; border:1px solid transparent; background:url(img/checkbox.gif) no-repeat 0 0; z-index:2;}
/*Buttons*/
.NFButton {width:auto; height:34px; padding:0 15px; font-weight:bold; background:url(/images/inadminpanel/img/button.gif) repeat-x 0 0; cursor:pointer; border:none;color:#FFFFFF; text-shadow:1px 1px #45add8;vertical-align:middle;}
+.NFButtonToRight {width:auto; height:34px; padding:0 15px; font-weight:bold; background:url(/images/inadminpanel/img/button.gif) repeat-x 0 0; cursor:pointer; border:none;color:#FFFFFF; text-shadow:1px 1px #45add8;vertical-align:middle; float:right}
.NFButtonLeft, .NFButtonRight {width:10px; height:34px; vertical-align:middle;}
.NFButtonLeft {background:url(/images/inadminpanel/img/button-left.gif) no-repeat 0 0;}
.NFButtonRight {background:url(/images/inadminpanel/img/button-right.gif) no-repeat 0 0;}