/** * Méthode de suppression d'une fonction/traduction * @param type $params */ public static function delete($params = []) { $id = false; if (isset($params[0]) && isset($params[1])) { $type = $params[0]; $id = $params[1]; } $user = CodeController::getUser(); if ($type == 'structure') { if ($id && $user) { $structure = \Root\Src\Model\StructureModel::loadStructureById($id); if ($structure->getId() != Null) { if ($structure->getOwnerId() != $user->getId()) { AppController::setMsg('error', "La structure n'appartient pas à votre compte. Vous ne pouvez pas la modifier."); } else { $translation = new \Root\Src\Model\TranslationModel(); $translation->setLayoutId($structure->getId()); if (!$translation->delete()) { AppController::setMsg("error", "Une erreur est survenue lors de la suppression d'une des traductions."); } if (!$structure->delete()) { AppController::setMsg("error", "Une erreur est survenue lors de la suppression de la structure de traduction."); } else { AppController::setMsg('success', "La structure de traduction a bien été supprimée"); } } } else { AppController::setMsg("warning", "La structure n'existe pas ou a déjà été supprimée"); } } } else { if ($type == 'function') { if ($id && $user) { $function = \Root\Src\Model\AlgorithmModel::loadFunctionById($id); if ($function) { if ($function->getOwnerId() == $user->getId()) { if ($function->delete()) { AppController::setMsg("success", "La fonction a bien été supprimée."); } else { AppController::setMsg("error", "Un problème est survenu lors de la suppression. Veuillez reessayer plus tard."); } } else { AppController::setMsg("error", "La fonction n'appartient pas à votre compte, vous ne pouvez pas la supprimer"); } } else { AppController::setMsg("warning", "La fonction n'existe pas ou a déjà été supprimée."); } } else { AppController::setMsg("warning", "Vous ne pouvez pas supprimer la ressource demandée."); } } } self::render(); }
/** * Traduit le pseudoCode dans un langage avec une liste de structure privée donnée * @param type $pseudoCode * @param type $language * @param type $structures * @return type */ public static function translate($pseudoCode, $language, $structures = []) { $result = $pseudoCode; $translations = \Root\Src\Model\TranslationModel::loadTranslationByLanguage($language); $publicStructures = \Root\Src\Model\StructureModel::loadStructureByUser(\Root\Src\Model\AlgorithmModel::PUBLIC_USER); foreach ($publicStructures as $publicStructure) { if (!in_array($publicStructure, $structures)) { array_push($structures, $publicStructure); } } foreach ($structures as $structure) { if (isset($translations[$structure->getId()])) { $result = preg_replace(self::interpret($structure->getCode()), $translations[$structure->getId()]->getCode(), $result); } else { $result = preg_replace(self::interpret($structure->getCode()), 'ERR', $result); } } return $result; }
/** * Méthode de chargement d'une structure * @param type $params */ public static function load($params = []) { if (isset($params[0])) { $_POST['idStructure'] = $params[0]; $structure = \Root\Src\Model\StructureModel::loadStructureById($_POST['idStructure']); $user = AppController::getUser(); if ($structure && $user && $structure->getOwnerId() == $user->getId()) { $_POST['idStructure'] = $structure->getId(); $_POST['structure'] = $structure->getCode(); $tranlsationToC = \Root\Src\Model\TranslationModel::loadTranslationByIdAndLanguage($structure->getId(), 'c'); if ($tranlsationToC) { $_POST['translationToC'] = $tranlsationToC->getCode(); } $tranlsationToPhp = \Root\Src\Model\TranslationModel::loadTranslationByIdAndLanguage($structure->getId(), 'php'); if ($tranlsationToPhp) { $_POST['translationToPhp'] = $tranlsationToPhp->getCode(); } $tranlsationToJava = \Root\Src\Model\TranslationModel::loadTranslationByIdAndLanguage($structure->getId(), 'java'); if ($tranlsationToJava) { $_POST['translationToJava'] = $tranlsationToJava->getCode(); } $tranlsationToJavascript = \Root\Src\Model\TranslationModel::loadTranslationByIdAndLanguage($structure->getId(), 'javascript'); if ($tranlsationToJavascript) { $_POST['translationToJavascript'] = $tranlsationToJavascript->getCode(); } $tranlsationToPython = \Root\Src\Model\TranslationModel::loadTranslationByIdAndLanguage($structure->getId(), 'python'); if ($tranlsationToPython) { $_POST['translationToPython'] = $tranlsationToPython->getCode(); } else { unset($_POST['idStructure']); } } } self::render(); }
/** * Rendu de la page de traduction */ public static function translate() { $language = $_POST['language']; $pseudoCode = ""; $privateFunctionIds = []; $selectedUserStructures = []; if (isset($_POST['selectedUserStructures'])) { $structureIds = $_POST['selectedUserStructures']; foreach ($structureIds as $id) { array_push($selectedUserStructures, \Root\Src\Model\StructureModel::loadStructureById($id)); } } if (isset($_POST['privateFunctions'])) { $privateFunctionIds = $_POST['privateFunctions']; } $privateFunctions = []; foreach ($privateFunctionIds as $id) { $currentFunction = \Root\Src\Model\AlgorithmModel::loadFunctionById($id); if ($currentFunction) { array_push($privateFunctions, $currentFunction); } } $publicFunctionIds = []; if (isset($_POST['publicFunctions'])) { $publicFunctionIds = $_POST['publicFunctions']; } $publicFunctions = []; foreach ($publicFunctionIds as $id) { $currentFunction = \Root\Src\Model\AlgorithmModel::loadFunctionById($id); array_push($publicFunctions, $currentFunction); } if (isset($_POST['pseudoCode'])) { $pseudoCode = $_POST['pseudoCode']; } $id = $_POST['id']; parent::getHeader(); self::call('translate', ['id' => $id, 'selectedLanguage' => $language, 'pseudoCode' => htmlentities($pseudoCode), 'privateFunctions' => $privateFunctions, 'publicFunctions' => $publicFunctions, 'selectedStructures' => $selectedUserStructures]); parent::getFooter(); }