Checkboxes Hierarchically checked. UserBasket.
authorGustavo Martin Morcuende <gu.martinm@gmail.com>
Mon, 10 Dec 2012 02:08:51 +0000 (03:08 +0100)
committerGustavo Martin Morcuende <gu.martinm@gmail.com>
Mon, 10 Dec 2012 02:08:51 +0000 (03:08 +0100)
apps/userfront/modules/category/actions/actions.class.php
apps/userfront/modules/category/templates/indexSuccess.php

index c59f661..7553bba 100644 (file)
@@ -83,8 +83,8 @@ class categoryActions extends sfActions
     //Get user Id
     $userId = $this->getUser()->getGuardUser()->getId();
 
-    //Get data from user
-    $checked = $request->getParameter('checked');
+    //Get data from user. Empty array by default.
+    $checked = $request->getParameter('checked', array());
 
     $uniqChecked =  array_unique($checked, SORT_NUMERIC);
 
@@ -96,16 +96,14 @@ class categoryActions extends sfActions
     $iterator = $userBaskets->getIterator();
     while ($userBasket = $iterator->current())
     {
-        if (!empty($uniqChecked))
+        foreach ($uniqChecked as $index => $value)
         {
-            foreach ($uniqChecked as $index => $value)
+            if ($userBasket->getGeneralCategId() == $value)
             {
-                if ($userBasket->getGeneralCategId() == $value)
-                {
-                    unset($uniqChecked[$index]);
-                    $iterator->next();
-                    continue 2;
-                }
+                unset($uniqChecked[$index]);
+                $iterator->next();
+                //I know this sucks, but I am in a hurry.
+                continue 2;
             }
         }
         $userBaskets->remove($iterator->key());
@@ -118,6 +116,10 @@ class categoryActions extends sfActions
         {
             //Never trust in data coming from users... Performance vs security.
             $generalCategory = GeneralCategoryTable::getInstance()->findOneById($value);
+            //TODO: some evil person could send the data without using my nice JavaScript code
+            //Here I should check if the node has child nodes and add them always, even if the user
+            //did not send them because she/he is not using my nice JavaScript code. My JavaScript code
+            //always checks the child nodes' checkbox following the hierarchy structure.
             if ($generalCategory != null)
             {
                 $userBasket = new UserBasket();
index 67c0f2e..cebfd7c 100644 (file)
         }, "json");
     };
 </script>
+<script type="text/javascript">
+    function hierarchyCheck(node, checkedInput) {
+        $("table.treeTable tbody tr.child-of-" + node[0].id).each(function(data){
+            $(this).children('td').eq(1).find('input').prop('checked', checkedInput);
+            $(this).children('td').eq(1).find('input').prop('disabled', checkedInput);
+            hierarchyCheck($(this), checkedInput);
+        });
+
+    }
+    $(document).ready(function(){
+        $('[id^=node]').change(function(){
+            var checkedInput = false;
+            if($(this).children('td').eq(1).find('input').is(':checked')) {
+                checkedInput = true;
+            }
+            hierarchyCheck($(this), checkedInput);
+        });
+    });
+</script>
+