示例#1
0
 /**
  * test for searching a product
  * NOTE: By changing the product "Cappuccino" in the database or adding a product
  *      with a similar name, this test will no longer work unless $expectedResult is
  *      changed based on AdminSearch::makeProductSearch's new output
  */
 public function testMakeProductSearch()
 {
     // Arrange
     $adminSearch = new AdminSearch();
     $connection = open_database_connection();
     $expectedResult = '<h3>Search results for Cappuccino</h3><form id="productResult13" action= "/adminEditProduct" method="get">
 <p><span class="search" onclick="document.getElementById(\'productResult13\').submit();">Cappuccino</span> - This creamy, delicious, rich flavoured coffee specialty is made from 1/3 Espresso, 1/3 steamed milk and 1/3 foamed milk and garnished with your choice of Cinnamon or powder chocolate. We use  high altitude(above 3000 ft) grown Arabica coffee beans.</p><input type="hidden" name="product_ID" value="13" /><input type="hidden" name="product_name" value="Cappuccino" /><input type="hidden" name="product_image_URL" value="cappuccino.png" /><input type="hidden" name="product_description" value="This creamy, delicious, rich flavoured coffee specialty is made from 1/3 Espresso, 1/3 steamed milk and 1/3 foamed milk and garnished with your choice of Cinnamon or powder chocolate. We use  high altitude(above 3000 ft) grown Arabica coffee beans." /><input type="hidden" name="product_calories" value="115" /><input type="hidden" name="product_allergy_info" value="no allergens are present on this drink product but consideration for those with low toleration to milk products should be given." /><input type="hidden" name="product_price" value="3.45" /><input type="hidden" name="category_ID" value="3" /><input type="hidden" name="category_name" value="Drinks" /><input type="hidden" name="category_summary" value="Quench your thirst with our selection of freshly made drinks or warm yourself up with a selection of hot drinks that we offer!" /><input type="hidden" name="productImageURL1" value="" /><input type="hidden" name="productImageURL2" value="cappuccino.png" /></form>';
     // Assert
     $result = $adminSearch->makeProductSearch($connection, "Cappuccino");
     close_database_connection($connection);
     // Act
     $this->assertEquals($result, $expectedResult);
 }
示例#2
0
 /**
  * Search action for the admin pages. This function handles all of the possible
  * types of searches that the user might search for.
  *
  * Action for route: /adminSearch
  *
  * @param Request $request
  * @param Application $app
  * @return \Symfony\Component\HttpFoundation\RedirectResponse
  */
 public function searchAction(Request $request, Application $app)
 {
     // test if 'username' stored in session ...
     $username = getAuthenticatedUserName($app);
     // check we are authenticated --------
     $isAuthenticated = null != $username;
     if (!$isAuthenticated) {
         // not authenticated, so redirect to LOGIN page
         return $app->redirect('/login');
     }
     $connection = open_database_connection();
     $searchType = $_GET['search_type'];
     if ($searchType == "new") {
         $searchType = "product";
     }
     $searchObject = new AdminSearch();
     $searchResults = "";
     if ($searchType == "category") {
         if (!empty($_GET['keywords'])) {
             if (isset($_GET['name'])) {
                 if ($_GET['name'] == "ascending") {
                     $searchResults = $searchObject->sortCategoryBySearch($connection, "ASC", "CategoryName", $_GET['keywords']);
                 } else {
                     if ($_GET['name'] == "descending") {
                         $searchResults = $searchObject->sortCategoryBySearch($connection, "DESC", "CategoryName", $_GET['keywords']);
                     }
                 }
             } else {
                 $searchResults = $searchObject->makeCategorySearch($connection, $_GET['keywords']);
             }
         } else {
             if (isset($_GET['name'])) {
                 if ($_GET['name'] == "ascending") {
                     $searchResults = $searchObject->sortCategory($connection, "CategoryName", "ASC");
                 } else {
                     $searchResults = $searchObject->sortCategory($connection, "CategoryName", "DESC");
                 }
             } else {
                 if (isset($_GET['summary'])) {
                     if ($_GET['summary'] == "ascending") {
                         $searchResults = $searchObject->sortCategory($connection, "CategorySummary", "ASC");
                     } else {
                         $searchResults = $searchObject->sortCategory($connection, "CategorySummary", "DESC");
                     }
                 }
             }
         }
     } else {
         if ($searchType == "product-category") {
             if (isset($_GET['search'])) {
                 if (isset($_GET['name'])) {
                     if ($_GET['name'] == "ascending") {
                         $searchResults = $searchObject->sortCategoryBySearch($connection, "ASC", "CategoryName", $_GET['keywords']);
                     } else {
                         if ($_GET['name'] == "descending") {
                             $searchResults = $searchObject->sortCategoryBySearch($connection, "DESC", "CategoryName", $_GET['keywords']);
                         }
                     }
                 } else {
                     $searchResults = $searchObject->makeCategorySearch($connection, $_GET['keywords']);
                 }
             } else {
                 if (isset($_GET['name'])) {
                     if ($_GET['name'] == "ascending") {
                         $searchResults = $searchObject->sortCategory($connection, "CategoryName", "ASC");
                     } else {
                         $searchResults = $searchObject->sortCategory($connection, "CategoryName", "DESC");
                     }
                 } else {
                     if ($_GET['summary'] == "ascending") {
                         $searchResults = $searchObject->sortCategory($connection, "CategorySummary", "ASC");
                     } else {
                         $searchResults = $searchObject->sortCategory($connection, "CategorySummary", "DESC");
                     }
                 }
             }
         } else {
             if ($searchType == "product") {
                 if (isset($_GET['keywords'])) {
                     if (isset($_GET['name'])) {
                         if ($_GET['name'] == "ascending") {
                             $searchResults = $searchObject->sortProductBySearch($connection, "ASC", "ProductName", $_GET['keywords']);
                         } else {
                             if ($_GET['name'] == "descending") {
                                 $searchResults = $searchObject->sortProductBySearch($connection, "DESC", "ProductName", $_GET['keywords']);
                             }
                         }
                     } else {
                         if (isset($_GET['calories'])) {
                             if ($_GET['calories'] == "ascending") {
                                 $searchResults = $searchObject->sortProductBySearch($connection, "ASC", "ProductCalories", $_GET['keywords']);
                             } else {
                                 if ($_GET['calories'] == "descending") {
                                     $searchResults = $searchObject->sortProductBySearch($connection, "DESC", "ProductCalories", $_GET['keywords']);
                                 }
                             }
                         } else {
                             if (isset($_GET['price'])) {
                                 if ($_GET['price'] == "ascending") {
                                     $searchResults = $searchObject->sortProductBySearch($connection, "ASC", "ProductPrice", $_GET['keywords']);
                                 } else {
                                     if ($_GET['price'] == "descending") {
                                         $searchResults = $searchObject->sortProductBySearch($connection, "DESC", "ProductPrice", $_GET['keywords']);
                                     }
                                 }
                             } else {
                                 if (isset($_GET['description'])) {
                                     if ($_GET['description'] == "ascending") {
                                         $searchResults = $searchObject->sortProductBySearch($connection, "ASC", "ProductDescription", $_GET['keywords']);
                                     } else {
                                         if ($_GET['description'] == "descending") {
                                             $searchResults = $searchObject->sortProductBySearch($connection, "DESC", "ProductDescription", $_GET['keywords']);
                                         }
                                     }
                                 } else {
                                     $searchResults = $searchObject->makeProductSearch($connection, $_GET['keywords']);
                                 }
                             }
                         }
                     }
                 } else {
                     if (isset($_GET['name'])) {
                         if ($_GET['name'] == "ascending") {
                             $searchResults = $searchObject->sortProduct($connection, "ProductName", "ASC");
                         } else {
                             $searchResults = $searchObject->sortProduct($connection, "ProductName", "DESC");
                         }
                     } else {
                         if (isset($_GET['calories'])) {
                             if ($_GET['calories'] == "ascending") {
                                 $searchResults = $searchObject->sortProduct($connection, "ProductCalories", "ASC");
                             } else {
                                 $searchResults = $searchObject->sortProduct($connection, "ProductCalories", "DESC");
                             }
                         } else {
                             if (isset($_GET['description'])) {
                                 if ($_GET['description'] == "ascending") {
                                     $searchResults = $searchObject->sortProduct($connection, "ProductDescription", "ASC");
                                 } else {
                                     $searchResults = $searchObject->sortProduct($connection, "ProductDescription", "DESC");
                                 }
                             } else {
                                 if (isset($_GET['price'])) {
                                     if ($_GET['price'] == "ascending") {
                                         $searchResults = $searchObject->sortProduct($connection, "ProductPrice", "ASC");
                                     } else {
                                         $searchResults = $searchObject->sortProduct($connection, "ProductPrice", "DESC");
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
     close_database_connection($connection);
     // store username into args array
     $argsArray = array('title' => $_GET['keywords'] . " search results", 'username' => $username, 'searchType' => $searchType, 'searchResults' => $searchResults, 'search' => $_GET['keywords']);
     // render (draw) template
     // ------------
     $templateName = 'admin/adminSearch';
     return $app['twig']->render($templateName . '.html.twig', $argsArray);
 }