Web page for users: link to general categories.
authorGustavo Martin Morcuende <gu.martinm@gmail.com>
Tue, 22 May 2012 20:06:29 +0000 (22:06 +0200)
committerGustavo Martin Morcuende <gu.martinm@gmail.com>
Tue, 22 May 2012 20:06:29 +0000 (22:06 +0200)
apps/userfront/modules/category/actions/actions.class.php
apps/userfront/modules/category/templates/indexSuccess.php
lib/model/doctrine/GeneralCategoryTable.class.php
web/css/inadminpanel/niceforms-default.css

index aedc677..3a2fecc 100644 (file)
@@ -77,4 +77,64 @@ class categoryActions extends sfActions
       $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));
+  }
 }
index 7475768..228997b 100644 (file)
@@ -7,13 +7,15 @@
         });
 </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>
 
@@ -23,8 +25,7 @@
   <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>
@@ -38,7 +39,7 @@
       }
       ?>>
       <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
@@ -51,4 +52,4 @@
   </tbody>
 </table>
 
-<input type="button" value="Update" onClick="$(this).UpdateCategories();"/>
+<input type="button" class="NFButtonToRight" value="<?php echo __('Select') ?>" onClick="$(this).CheckCategories();"/>
index 864b0ae..70019dd 100644 (file)
@@ -25,6 +25,7 @@ class GeneralCategoryTable extends Doctrine_Table
     */
     public function getGeneralCategoriesByLftQuery()
     {
-        return $this->createQuery('gc')->orderBy('gc.lft');
+        return $this->createQuery('gc')->where('gc.id != 1')
+                                       ->orderBy('gc.lft');
     }
 }
index 7d8f64c..3be17d9 100644 (file)
@@ -37,6 +37,7 @@ line-height:32px;
 .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;}