/** * This method loads the key translations if necessary, * and replaces all text-keys with the chosen language translation. */ protected function handleTranslations($template) { // if translations aren't loaded yet, do it once if (!isset($_SESSION[Session::TRANSLATIONS])) { LanguageHelper::loadTranslations(); } // $matches[0] contains the results of the complete pattern // $matches[1] contains the results which matched the pattern inside the brackets (-> see regex pattern) preg_match_all(LanguageHelper::REGEX_TRANSLATION_KEYS, $template, $matches); foreach ($matches[1] as $match) { $template = str_replace("?{" . $match . "}", LanguageHelper::getTranslatedValue($match), $template); } return $template; }
public function handleRequestInMain() { $this->checkAccess(); if ($_SERVER["REQUEST_METHOD"] == "POST") { // get classification, its type and price from POST array $classification = $_POST["name-classification"]; $type = $_POST["name-type--" . $classification]; $namedQuery = new NamedQuery($this->QUERY_INSERT_PRODUCT); $namedQuery->addParam(QueryParam::TYPE_STRING, $classification); $namedQuery->addParam(QueryParam::TYPE_STRING, $type); $namedQuery->addParam(QueryParam::TYPE_STRING, $_POST["name-price"]); // insert those information into DB and get the product's ID $insertId = CRUDService::getInstance()->executeNamedQuery($namedQuery); // now, we can generate the new file name for the uploaded image $fileName = $_FILES["name-image"]["name"]; $lastDot = strrpos($fileName, "."); $imgType = substr($fileName, $lastDot); // example : ".jpg" // image name is like : <productType>-<id>.<imageType> // for example : robot-42.png $imgName = strtolower($type) . "-" . $insertId . $imgType; // define the upload directory's relative path $uploadDir = "./images/products/" . $classification . "/" . strtolower($type) . "/"; // move the uploaded file to the correct image directory move_uploaded_file($_FILES["name-image"]["tmp_name"], $uploadDir . $imgName); // now, update the database to set the image name $nq2 = new NamedQuery($this->QUERY_SET_IMAGE); $nq2->addParam(QueryParam::TYPE_STRING, $imgName); $nq2->addParam(QueryParam::TYPE_INTEGER, $insertId); CRUDService::getInstance()->executeNamedQuery($nq2); // keys to write : product.<id>.name.<lang> , product.<id>.description.<lang> $data = 'product.' . $insertId . '.name.de = "' . $_POST["name-name-de"] . '"' . PHP_EOL; $data .= 'product.' . $insertId . '.name.en = "' . $_POST["name-name-en"] . '"' . PHP_EOL; $data .= 'product.' . $insertId . '.name.fr = "' . $_POST["name-name-fr"] . '"' . PHP_EOL; $data .= 'product.' . $insertId . '.description.de = "' . $_POST["name-description-de"] . '"' . PHP_EOL; $data .= 'product.' . $insertId . '.description.en = "' . $_POST["name-description-en"] . '"' . PHP_EOL; $data .= 'product.' . $insertId . '.description.fr = "' . $_POST["name-description-fr"] . '"' . PHP_EOL; // write titles and descriptions into the products.ini file file_put_contents(Config::DEFAULT_PRODUCT_FILE, $data, FILE_APPEND); // finally, update the session with the new products.ini file content LanguageHelper::loadTranslations(); } }
/** * Overwrite the abstract function from Superclass. * If an admin POST-ed a product change, update the database if necessary, * and also update the key values (language translations) for this product * in the products.ini file. */ public function handleRequestInMain() { $this->checkAccess(); if ($_SERVER["REQUEST_METHOD"] == "POST") { $id = intval($_POST["name-id"]); $_SESSION[Session::PRODUCT_TRANSLATIONS]["product." . $id . ".name.de"] = $_POST["name-name-de"]; $_SESSION[Session::PRODUCT_TRANSLATIONS]["product." . $id . ".name.en"] = $_POST["name-name-en"]; $_SESSION[Session::PRODUCT_TRANSLATIONS]["product." . $id . ".name.fr"] = $_POST["name-name-fr"]; $_SESSION[Session::PRODUCT_TRANSLATIONS]["product." . $id . ".description.de"] = $_POST["name-description-de"]; $_SESSION[Session::PRODUCT_TRANSLATIONS]["product." . $id . ".description.en"] = $_POST["name-description-en"]; $_SESSION[Session::PRODUCT_TRANSLATIONS]["product." . $id . ".description.fr"] = $_POST["name-description-fr"]; // write the edited content from session back into the product's file file_put_contents(Config::DEFAULT_PRODUCT_FILE, $this->productArrayToString()); LanguageHelper::loadTranslations(); // update the database to set the new price $nq = new NamedQuery($this->QUERY_SET_PRICE); $nq->addParam(QueryParam::TYPE_DOUBLE, doubleval($_POST["name-price"])); $nq->addParam(QueryParam::TYPE_INTEGER, $id); CRUDService::getInstance()->executeNamedQuery($nq); // redirect back to the delete.php page $this->redirect("delete.php"); } }