Exemplo n.º 1
0
    public function generateCRUDRoutes()
    {
        $template = '$_ROUTES[] = array(
	"list" => array("url" => "/:uc_name:/", "controller" => "[NAME]", "action" => "index"),
	"create" => array("url" => "/:uc_name:/create", "controller" => "[NAME]", "action" => "create", "via" => "post"),
	"new" => array("url" => "/:uc_name:/new", "controller" => "[NAME]", "action" => "new"),
	"retrieve" => array("url" => "/:uc_name:/:id", "controller" => "[NAME]", "action" => "show", "via" => "get"),
	"update" => array("url" => "/:uc_name:/:id/edit", "controller" => "[NAME]", "action" => "edit"),
	"delete" => array("url" => "/:uc_name:/:id/delete", "controller" => "[NAME]", "action" => "delete", "via" => "post")
	); ?>';
        $template = str_replace(":uc_name:", Makiavelo::camel_to_underscore($this->en_name), $template);
        $template = str_replace("[NAME]", $this->en_name, $template);
        $routes_file = ROOT_PATH . Makiavelo::APP_CONFIG_FOLDER . "/routes.php";
        $fp = fopen($routes_file, "r");
        if ($fp) {
            $routes = fread($fp, filesize($routes_file));
            $routes = str_replace("?>", $template, $routes);
            fclose($fp);
            $fp = fopen($routes_file, "w");
            if ($fp) {
                fwrite($fp, $routes);
                fclose($fp);
            }
        }
    }
Exemplo n.º 2
0
function __autoload_tasks($classname)
{
    $fname = dirname(__FILE__) . "/lib/tasks/" . Makiavelo::camel_to_underscore($classname) . ".php";
    if (file_exists($fname)) {
        include_once $fname;
    }
}
Exemplo n.º 3
0
 public function generateSQL()
 {
     Makiavelo::info("Generating SQL statements...");
     $sql_gen = new SQLGenerator($this->en_name, $this->en_attributes);
     $create_database_sql = $sql_gen->create_database_sql();
     $create_db_file = ROOT_PATH . Makiavelo::SQL_CREATE_TABLES_FOLDER . "/create_db.sql";
     if (!file_exists($create_db_file)) {
         $fp = fopen($create_db_file, "w");
         if ($fp) {
             Makiavelo::info("Create db saved");
             fwrite($fp, $create_database_sql);
             fclose($fp);
         }
     }
     $create_sql = $sql_gen->create_sql();
     $create_table_file = ROOT_PATH . Makiavelo::SQL_CREATE_TABLES_FOLDER . "/" . $this->en_name . ".sql";
     if (!file_exists($create_table_file)) {
         $fp = fopen($create_table_file, "w");
         if ($fp) {
             Makiavelo::info("Create table saved");
             fwrite($fp, $create_sql);
             fclose($fp);
         }
     }
     $crud_sql = $sql_gen->crud_sql();
     Makiavelo::info("Generating SQL CRUD: ");
     Makiavelo::info(print_r($crud_sql, true));
     $template_helper_file = ROOT_PATH . Makiavelo::TEMPLATES_FOLDER . "/sql_helper_templates.php";
     $fp = fopen($template_helper_file, "r");
     if ($fp) {
         $template_code = fread($fp, filesize($template_helper_file));
         $code = str_replace("[NAME]", $this->en_name, $template_code);
         $code = str_replace("[UC_NAME]", Makiavelo::camel_to_underscore($this->en_name), $code);
         foreach ($crud_sql as $action => $query) {
             $code = str_replace("[" . strtoupper($action) . "_SQL]", $query, $code);
         }
         fclose($fp);
         //Saving template
         $helper_file = ROOT_PATH . Makiavelo::SQL_HELPERS_FOLDER . "/" . $this->en_name . ".php";
         if (!file_exists($helper_file)) {
             $fp = fopen($helper_file, "w");
             if ($fp) {
                 Makiavelo::info("Helper sql file saved");
                 fwrite($fp, $code);
                 fclose($fp);
             }
         }
     }
     Makiavelo::info("CRUD SQL created: " . print_r($crud_sql, true));
 }
Exemplo n.º 4
0
 public function __call($method, $text)
 {
     $method_type = Makiavelo::camel_to_underscore($method);
     $method_parts = explode("_", $method_type);
     if ($method_parts[0] == "get") {
         $key = '__flash_' . $method_parts[1];
         if (isset($_SESSION[$key])) {
             $txt = $_SESSION[$key];
             unset($_SESSION[$key]);
         } else {
             $txt = "";
         }
         return $txt;
     } else {
         $_SESSION["__flash_" . $method_parts[1]] = $text[0];
     }
 }
Exemplo n.º 5
0
 public function execute($params)
 {
     $name = str_replace("Controller", "", $params[0]);
     //in case the dev adds the word...
     if (isset($params[1])) {
         $views_option = $params[1];
     } else {
         $views_option = "";
     }
     $this->controller_name = $name;
     //Make sure the name is rightly formatted
     $this->uc_controller_name = Makiavelo::camel_to_underscore($name);
     Makiavelo::info("Generating controller: {$this->controller_name}");
     $this->generateControllerFile();
     if ($views_option != "no-views") {
         Makiavelo::info("With views...");
         $this->generateViewFiles();
     } else {
         Makiavelo::info("Without views...");
     }
 }
Exemplo n.º 6
0
 public function generateCRUDViewsFor($name, $attributes = array())
 {
     Makiavelo::info("Generating VIEWS...");
     $views_folder = ROOT_PATH . Makiavelo::VIEWS_FOLDER . $name;
     @mkdir($views_folder);
     //Index file:
     $index_file_path = ROOT_PATH . Makiavelo::ABM_VIEWS_TEMPLATE_FOLDER . "index.html.php";
     $template_lines = file($index_file_path);
     if (count($template_lines) > 0) {
         foreach ($template_lines as $i => $template_code) {
             #$template_code = fread($fp, filesize($index_file_path));
             $template_lines[$i] = str_replace("[NAME]", $name, $template_code);
             //Headers for the list
             preg_match("/\\[HEADERS: (.*)\\]/", $template_code, $match);
             if (isset($match[0]) && $match[0] != "") {
                 $headers_template_tag = $match[0];
                 $header_tag = $match[1];
                 $html_headers = "";
                 foreach ($attributes as $attr_name => $type) {
                     $html_headers .= str_replace("{attr_name}", $attr_name, "{$header_tag}") . "\n";
                 }
                 $html_headers = str_replace("\\t", "\t", $html_headers);
                 $template_code = preg_replace("/(\t+)\\[/", "[", $template_code);
                 $template_lines[$i] = str_replace($headers_template_tag, $html_headers, $template_code);
             }
             #Values for the list
             preg_match("/\\[ATTRIBUTE_VALUE: (.*)\\]/", $template_code, $match);
             if (isset($match[0]) && $match[0] != "") {
                 $headers_template_tag = $match[0];
                 $header_tag = $match[1];
                 $html_headers = "";
                 foreach ($attributes as $attr_name => $type) {
                     $html_headers .= str_replace("{attr_name}", $attr_name, "{$header_tag}") . "\n";
                 }
                 $html_headers = str_replace("\\t", "\t", $html_headers);
                 $template_code = preg_replace("/(\t+)\\[/", "[", $template_code);
                 $template_lines[$i] = str_replace($headers_template_tag, $html_headers, $template_code);
             }
             $template_lines[$i] = str_replace("[UC_NAME]", Makiavelo::camel_to_underscore($name), $template_lines[$i]);
         }
         $destination_index = $views_folder . "/index.html.php";
         if (file_exists($destination_index)) {
             Makiavelo::info("view/index file already exists, not saving...");
         } else {
             $fp = fopen($destination_index, "w");
             if ($fp) {
                 //Save the file
                 fwrite($fp, implode("", $template_lines));
                 fclose($fp);
             }
         }
     }
     //New file:
     $new_file_path = ROOT_PATH . Makiavelo::ABM_VIEWS_TEMPLATE_FOLDER . "new.html.php";
     $fp = fopen($new_file_path, "r");
     if ($fp) {
         $template_code = fread($fp, filesize($new_file_path));
         $template_code = str_replace("[NAME]", $name, $template_code);
     }
     $destination_new = $views_folder . "/new.html.php";
     if (file_exists($destination_new)) {
         Makiavelo::info("view/new file already exists, not saving...");
     } else {
         $fp = fopen($destination_new, "w");
         if ($fp) {
             //Save the file
             fwrite($fp, $template_code);
             fclose($fp);
         }
     }
     //Show file:
     $show_file_path = ROOT_PATH . Makiavelo::ABM_VIEWS_TEMPLATE_FOLDER . "show.html.php";
     $fp = fopen($show_file_path, "r");
     if ($fp) {
         $template_code = fread($fp, filesize($show_file_path));
         $template_code = str_replace("[NAME]", $name, $template_code);
         #Values for the list
         preg_match("/\\[ATTRIBUTE: (.*)\\]/", $template_code, $match);
         if (isset($match[0]) && $match[0] != "") {
             $attribute_template_tag = $match[0];
             $attribute_tag = $match[1];
             $attribute_list = "";
             foreach ($attributes as $attr_name => $type) {
                 $attribute_list .= str_replace("{attr_name}", $attr_name, "{$attribute_tag}") . "\n";
             }
             $template_code = str_replace($attribute_template_tag, $attribute_list, $template_code);
         }
     }
     $destination_show = $views_folder . "/show.html.php";
     if (file_exists($destination_show)) {
         Makiavelo::info("view/show file already exists, not saving...");
     } else {
         $fp = fopen($destination_show, "w");
         if ($fp) {
             //Save the file
             fwrite($fp, $template_code);
             fclose($fp);
         }
     }
     //Edit file:
     $edit_file_path = ROOT_PATH . Makiavelo::ABM_VIEWS_TEMPLATE_FOLDER . "edit.html.php";
     $fp = fopen($edit_file_path, "r");
     if ($fp) {
         $template_code = fread($fp, filesize($edit_file_path));
         $template_code = str_replace("[NAME]", $name, $template_code);
     }
     $destination_edit = $views_folder . "/edit.html.php";
     if (file_exists($destination_edit)) {
         Makiavelo::info("view/edit file already exists, not saving...");
     } else {
         $fp = fopen($destination_edit, "w");
         if ($fp) {
             //Save the file
             fwrite($fp, $template_code);
             fclose($fp);
         }
     }
     //_form file:
     $form_file_path = ROOT_PATH . Makiavelo::ABM_VIEWS_TEMPLATE_FOLDER . "_form.html.php";
     $form_file_lines = file($form_file_path);
     Makiavelo::info("Reading '{$form_file_path}'...");
     if (count($form_file_lines) > 0) {
         Makiavelo::info(count($form_file_lines) . " lines ...");
         foreach ($form_file_lines as $i => $template_code) {
             //List of form fields
             preg_match("/\\[FORM_FIELD: (.*)\\]/", $template_code, $match);
             if (isset($match[0]) && $match[0] != "") {
                 $form_template_tag = $match[0];
                 $form_tag = $match[1];
                 $html_form_fields = "";
                 foreach ($attributes as $attr_name => $type) {
                     $tmp = str_replace("{type}", $this->type_mapping[$type], "{$form_tag}") . "\n";
                     $tmp = str_replace("{attr_name}", $attr_name, "{$tmp}") . "\n";
                     $html_form_fields .= $tmp;
                 }
                 $html_form_fields = str_replace("\\t", "\t", $html_form_fields);
                 $template_code = preg_replace("/(\t+)\\[/", "[", $template_code);
                 $form_file_lines[$i] = str_replace($form_template_tag, $html_form_fields, $template_code);
             }
         }
     }
     $destination_form = $views_folder . "/_form.html.php";
     if (file_exists($destination_form)) {
         Makiavelo::info("view/_form file already exists, not saving...");
     } else {
         $fp = fopen($destination_form, "w");
         if ($fp) {
             //Save the file
             fwrite($fp, implode("", $form_file_lines));
             fclose($fp);
         }
     }
 }
Exemplo n.º 7
0
 public function __get_entity_name()
 {
     return Makiavelo::camel_to_underscore(get_class($this));
 }
Exemplo n.º 8
0
    public static function generateRoutesHelpers()
    {
        $general_routes_file = ROOT_PATH . Makiavelo::APP_CONFIG_FOLDER . "/routes.php";
        include $general_routes_file;
        foreach ($_ROUTES as $entity_routes) {
            foreach ($entity_routes as $action => $data) {
                Makiavelo::info("Creating function helper: " . Makiavelo::camel_to_underscore($data['controller']) . "_" . $action . '_path ()');
                $fnc = 'function ' . Makiavelo::camel_to_underscore($data['controller']) . "_" . $action . '_path ($params = array()) {

						$url = "' . $data['url'] . '";
						$args = func_get_args();

						preg_match_all("/:([a-zA-Z_0-9]*)/", $url, $matches);
						foreach($matches[1] as $i => $attr) {
							if(is_object($args[$i])) {
								$value = $args[$i]->$attr;
							} else {
								$value = $args[$i];
							}
							$url = str_replace(":$attr", $value, $url);
						}

							if(is_array($params) && count($params) > 0) {
								$url .= "?";
								$query_params = array();
								foreach($params as $k => $v) {
									$query_params[] = $k . "=" . $v;
								}
								$url .= implode("&", $query_params);
							}
							return $url;
						}';
                eval($fnc);
            }
        }
    }
Exemplo n.º 9
0
 private function sanitize_string($str)
 {
     return strtolower(str_replace(" ", "_", Makiavelo::camel_to_underscore($str)));
 }