protected function setUp() { date_default_timezone_set("Europe/Paris"); $this->valid_config = array("settings" => array("dinkly_version" => Dinkly::getConfigValue('dinkly_version', 'global')), "apps" => array("frontend" => array("default_app" => true, "default_module" => "landing", "base_href" => "/", "enabled" => true, "app_name" => "Dinkly Frontend", "app_description" => "The humblest little MVC Framework"), "admin" => array("default_module" => "home", "base_href" => "/admin", "enabled" => true, "app_name" => "Dinkly Admin", "app_description" => "Admin for the humblest little MVC", "copyright" => "Dinkly"), "api" => array("base_href" => "/api", "enabled" => true, "default_module" => "api", "app_name" => "Dinkly API"), "doc" => array("app_name" => "Dinkly Documentation", "base_href" => "/doc", "default_module" => "home", "enabled" => true))); $this->bad_config = array("settings" => array("dinkly_version" => Dinkly::getConfigValue('dinkly_version', 'global')), "apps" => array()); $this->valid_modules = array("home", "login", "user"); $context = null; $this->valid_context = array('current_app_name' => 'admin', 'module' => 'home', 'view' => 'default', 'parameters' => array('id' => 1)); }
function sqlToYml($sql, $model_name, $database_name) { $output = ""; $model_name_camel = Dinkly::convertToCamelCase($model_name, 1); // Output the table name $output .= "table_name: " . $model_name . "\nregistry:\n"; // Loop over the result set foreach ($sql as $row) { if (in_array($row["Field"], array("id", "created_at", "updated_at"))) { $output .= " - " . $row["Field"] . "\n"; } else { //Separate the type and length $type = explode("(", $row["Type"]); /* Output the row/collection indicator */ //add the name $output .= " - " . $row["Field"] . ":"; //add the type $output .= " { type: " . $type[0]; //if the length contains a comma (for decimals) quote the length if ($type[1] and strpos($type[1], ",") != false) { $output .= ", length: " . str_replace(")", "", $type[1]) . "'"; } elseif ($type[1]) { $output .= ", length: " . str_replace(")", "", $type[1]); } //set whether null is allowable $output .= ", allow_null: "; if ($row["Null"] == "Yes") { $output .= "true"; } else { $output .= "false"; } //add a line break $output .= " }\n"; } } //Set directory path to keep things cleaner $dir = dirname(__FILE__) . "/../config/schemas/" . $database_name; $yml_file = $model_name_camel . ".yml"; //Create directory if it doesn"t exist if (!file_exists($dir)) { shell_exec("mkdir {$dir}"); } //If file exists move it to a backup if (file_exists("{$dir}/{$yml_file}")) { shell_exec("mv {$dir}/{$yml_file} {$dir}/{$yml_file}~"); } //Write to the yml file $handle = fopen("{$dir}/{$yml_file}", "w") or die("Cannot open file: " . $yml_file); fwrite($handle, $output); echo "\nYML file created...\n"; }
/** * Default Constructor * */ public function __construct() { //Let's make this accessible across the admin for display of all dates $this->date_format = null; //We use this for the profile modal $this->logged_user = null; $this->db = DinklyDataConnector::fetchDB(); if (DinklyUser::isLoggedIn()) { $this->logged_user = new DinklyUser(); $this->logged_user->init(DinklyUser::getAuthSessionValue('logged_id')); $this->date_format = $this->date_format = $this->logged_user->getDateFormat() . ' ' . $this->logged_user->getTimeFormat(); return false; } else { if (Dinkly::getCurrentModule() != 'login') { $this->loadModule('admin', 'login', 'default', true, true); } } return true; }
echo Dinkly::getCurrentModule() == 'home' ? 'class="active"' : ''; ?> > <a href="/admin">Home</a> </li> <?php if (DinklyUser::isMemberOf('admin')) { ?> <li <?php echo Dinkly::getCurrentModule() == 'user' ? 'class="active"' : ''; ?> > <a href="/admin/user/">Users</a> </li> <li <?php echo Dinkly::getCurrentModule() == 'group' ? 'class="active"' : ''; ?> > <a href="/admin/group/">Groups</a> </li> <?php } ?> </ul> <ul class="nav navbar-nav pull-right dinkly-admin-user-menu"> <?php if (DinklyUser::isLoggedIn()) { ?> <li> <div class="btn-group"> <button type="button" class="btn btn-sm btn-primary dropdown-toggle" data-toggle="dropdown">
</span> </h3> <?php } ?> <div class="navbar navbar-inverse navbar-fixed-top" role="navigation"> <div class="container"> <div class="navbar-header"> <a class="navbar-brand" href="/admin"> <?php echo Dinkly::getConfigValue('app_name', 'admin'); ?> </a> </div> <div class="navbar-collapse collapse"> <ul class="nav navbar-nav"> <li <?php echo Dinkly::getCurrentModule() == 'home' ? 'class="active"' : ''; ?> > <a href="/doc">Home</a> </li> </ul> </div> </div> </div> </div> <div class="container"><!-- Primary Container --> <?php include 'nav.php';
<div class="row page-wrapper"> <div class="col-md-12"> <div class="jumbotron"> <div> <h1> <?php echo Dinkly::translate('Welcome to Poetry Out Loud!'); ?> </h1> <p> <?php echo Dinkly::getConfigValue('app_description'); ?> </p> <h5> Brought to you by: <a href="https://github.com/bigSiebs">bigSiebs</a>. </h5> </div> </div> </div> </div>
/** * Load desired module and redirect if necessary * * * @param string $app_name name of app we are trying to load * @param string $module_name string of desired module to load * @param string $view string if passed goes to specified view otherwise default * @param bool $redirect default false, make true to redirect to different view * @param bool $draw_layout default true to get module view * @param array $parameters Array of parameters that can be used to populate views * * @return bool true if app loaded currectly else false and sent to default app */ public function loadModule($app_name, $module_name = null, $view_name = 'default', $redirect = false, $draw_layout = true, $parameters = null) { //If nested, prevent output from doubling if (headers_sent()) { return false; } //If the app_name is not passed, assume whichever is set as the default in config.yml if (!$app_name) { $app_name = Dinkly::getDefaultApp(true); } //Validate passed app if (!in_array($app_name, Dinkly::getValidApps($app_name))) { throw new Exception('App "' . $app_name . '" cannot be loaded.'); return false; } if (!Dinkly::isAppEnabled($app_name)) { $message = "The requested app (" . $app_name . ") is currently disabled."; error_log($message); if (self::isDevMode()) { echo $message; } return false; } //Set the current app to match whatever was passed $_SESSION['dinkly']['current_app_name'] = $app_name; //If no view is passed, look for one called 'default' if (!$view_name) { $view_name = 'default'; } //Redirect page if true if ($redirect) { $base_href = Dinkly::getConfigValue('base_href', $app_name); if ($base_href == '/') { $base_href = null; } $path = $base_href . '/' . $module_name . '/' . $view_name; //Deal with parameters if ($parameters) { foreach ($parameters as $key => $value) { $path .= '/' . $key . '/' . $value; } } header("Location: " . $path); die; } //Switch out paths later based on whether this is a plugin or not $is_plugin = false; if (Dinkly::getConfigValue('is_plugin', $app_name)) { $is_plugin = true; } //Load the app controller, if one exists $camel_app_controller_name = self::convertToCamelCase($app_name, true) . "Controller"; if ($is_plugin) { $plugin_name = Dinkly::getConfigValue('plugin_name', $app_name); $app_controller_file = $_SERVER['APPLICATION_ROOT'] . '/plugins/' . $plugin_name . '/apps/' . $app_name . '/' . $camel_app_controller_name . '.php'; } else { $app_controller_file = $_SERVER['APPLICATION_ROOT'] . '/apps/' . $app_name . '/' . $camel_app_controller_name . '.php'; } $has_app_controller = false; if (file_exists($app_controller_file)) { //Instantiate controller object require_once $app_controller_file; $has_app_controller = true; } //Get module controller $camel_module_name = self::convertToCamelCase($app_name, true) . self::convertToCamelCase($module_name, true) . "Controller"; if ($is_plugin) { $controller_file = $_SERVER['APPLICATION_ROOT'] . '/plugins/' . $plugin_name . '/apps/' . $app_name . '/modules/' . $module_name . '/' . $camel_module_name . '.php'; } else { $controller_file = $_SERVER['APPLICATION_ROOT'] . '/apps/' . $app_name . '/modules/' . $module_name . '/' . $camel_module_name . '.php'; } //Save these on the object so they can be retrieved as needed in controllers or views $this->view = $view_name; $this->module = $module_name; $this->parameters = $parameters; //If the controller doesn't exist, load 404 error page if (!file_exists($controller_file)) { //If there's an app controller, we instantiate that, in case it has overrides if ($has_app_controller) { $app_controller = new $camel_app_controller_name(); $this->loadError($app_name, $camel_module_name, $view_name); return false; } else { $this->loadError($app_name, $camel_module_name, $view_name); return false; } } //Instantiate controller object require_once $controller_file; $controller = new $camel_module_name(); //Migrate current dinkly variables over to our new controller $vars = get_object_vars($this); foreach ($vars as $name => $value) { $controller->{$name} = $value; } //Get this view's function $view_controller_name = self::convertToCamelCase($view_name, true); $view_function = "load" . $view_controller_name; if (method_exists($controller, $view_function)) { //If controller function returns false, don't draw layout wrapper if ($controller->{$view_function}($parameters)) { $draw_layout = true; } else { $draw_layout = false; } if (!in_array($module_name, Dinkly::getValidModules($app_name))) { throw new Exception('Module "' . $module_name . '" cannot be loaded.'); return false; } //Migrate the scope of the declared variables to be local to the view $vars = get_object_vars($controller); foreach ($vars as $name => $value) { ${$name} = $value; } //Draw headers if ($draw_layout) { if ($is_plugin) { $base_module_header_path = $_SERVER['APPLICATION_ROOT'] . '/plugins/' . $plugin_name . '/apps/' . $app_name . '/modules/' . $module_name . '/views/header'; } else { $base_module_header_path = $_SERVER['APPLICATION_ROOT'] . '/apps/' . $app_name . '/modules/' . $module_name . "/views/header"; } if (file_exists($base_module_header_path . '.php') || file_exists($base_module_header_path . '.md')) { ob_start(); if (file_exists($base_module_header_path . '.php')) { include $base_module_header_path . '.php'; } else { if (file_exists($base_module_header_path . '.md')) { include $base_module_header_path . '.md'; } } $header = ob_get_contents(); ob_end_clean(); $this->setModuleHeader($header); } //Set the powered-by header if the version number is in the config if ($version = self::getConfigValue('dinkly_version', 'global')) { header('X-Powered-By: DINKLY/' . $version); } if ($is_plugin) { $app_header_path = $_SERVER['APPLICATION_ROOT'] . '/plugins/' . $plugin_name . '/apps/' . $app_name . '/layout/header.php'; } else { $app_header_path = $_SERVER['APPLICATION_ROOT'] . '/apps/' . $app_name . '/layout/header.php'; } if (file_exists($app_header_path)) { include $app_header_path; } } //Draw view if ($is_plugin) { $base_view_path = $_SERVER['APPLICATION_ROOT'] . '/plugins/' . $plugin_name . '/apps/' . $app_name . '/modules/' . $module_name . '/views/' . $view_name; } else { $base_view_path = $_SERVER['APPLICATION_ROOT'] . '/apps/' . $app_name . '/modules/' . $module_name . '/views/' . $view_name; } if (file_exists($base_view_path . '.php')) { include $base_view_path . '.php'; } else { if (file_exists($base_view_path . '.md')) { $markdown_template = file_get_contents($base_view_path . '.md'); $Parsedown = new Parsedown(); echo $Parsedown->text($markdown_template); } } //Draw footer if ($draw_layout) { if ($is_plugin) { $base_module_footer_path = $_SERVER['APPLICATION_ROOT'] . '/plugins/' . $plugin_name . '/apps/' . $app_name . '/modules/' . $module_name . "/views/footer"; } else { $base_module_footer_path = $_SERVER['APPLICATION_ROOT'] . '/apps/' . $app_name . '/modules/' . $module_name . "/views/footer"; } if (file_exists($base_module_footer_path . '.php') || file_exists($base_module_footer_path . '.md')) { ob_start(); if (file_exists($base_module_footer_path . '.php')) { include $base_module_footer_path . '.php'; } else { if (file_exists($base_module_footer_path . '.md')) { include $base_module_footer_path . '.md'; } } $header = ob_get_contents(); ob_end_clean(); $this->setModuleHeader($header); } if ($is_plugin) { $app_footer_path = $_SERVER['APPLICATION_ROOT'] . '/apps/' . $app_name . '/layout/footer.php'; } else { $app_footer_path = $_SERVER['APPLICATION_ROOT'] . '/apps/' . $app_name . '/layout/footer.php'; } if (file_exists($app_footer_path)) { include $app_footer_path; } } } else { $this->loadError($app_name, $camel_module_name, $view_name); return false; } return $draw_layout; }
<?php //rename_app require_once 'config/bootstrap.php'; $options = getopt("a:d:"); if (!isset($options['a']) || !isset($options['d'])) { echo "\nPlease use the -a flag and -d flag to indicate the source application name and the destination application name to use.\nExample: php tools/copy_app.php -a=admin -d=my_admin\n"; } else { echo "\nCopying " . $options['a'] . " app to " . $options['d'] . " app \n"; $source = $_SERVER['APPLICATION_ROOT'] . 'apps/' . $options['a']; $dest = $_SERVER['APPLICATION_ROOT'] . 'apps/' . $options['d']; $source_prefix = Dinkly::convertToCamelCase($options['a'], true); $destination_prefix = Dinkly::convertToCamelCase($options['d'], true); if (mkdir($dest)) { echo "\nCreated directory for " . $options['d'] . " app \n"; } echo "\nCopying files for " . $options['d'] . " app:\n"; foreach ($iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($source, \RecursiveDirectoryIterator::SKIP_DOTS), \RecursiveIteratorIterator::SELF_FIRST) as $item) { if ($item->isDir()) { mkdir($dest . DIRECTORY_SEPARATOR . $iterator->getSubPathName()); } else { $dest_file = $dest . DIRECTORY_SEPARATOR . str_replace($source_prefix, $destination_prefix, $iterator->getSubPathName()); copy($item, $dest_file); $contents = file_get_contents($dest_file); $contents = str_replace($options['a'], $options['d'], $contents); $contents = str_replace($source_prefix, $destination_prefix, $contents); file_put_contents($dest_file, $contents); echo $dest_file . "\n"; } } echo "\n" . $options['d'] . " app succesfully create!\n\n";
public function __construct() { parent::__construct(); }
<?php /** * Used for upgrade from v2.04 or lower to prefix the app name to the class name * and file name for controllers */ require_once 'config/bootstrap.php'; $apps = array_filter(glob($_SERVER['APPLICATION_ROOT'] . 'apps/*'), 'is_dir'); foreach ($apps as $app) { $app_path_parts = explode('/', $app); $app_name = end($app_path_parts); $source_prefix = Dinkly::convertToCamelCase($app_name, true); echo "\nUpdateing " . $app_name . " app\n"; foreach ($iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($app, \RecursiveDirectoryIterator::SKIP_DOTS), \RecursiveIteratorIterator::SELF_FIRST) as $item) { if (!$item->isDir()) { $name_parts = explode('/', $iterator->getSubPathName()); $file_name = end($name_parts); if (strstr($file_name, 'Controller') && !strstr($file_name, $source_prefix . "Controller") && !strstr($file_name, $source_prefix)) { $controller_name = str_replace('.php', '', $file_name); $new_file_name = $source_prefix . $file_name; $new_controller_name = $source_prefix . $controller_name; $source = $app . DIRECTORY_SEPARATOR . $iterator->getSubPathName(); $dest = $app . DIRECTORY_SEPARATOR . str_replace($file_name, $new_file_name, $iterator->getSubPathName()); echo $file_name . " changes to " . $new_file_name . "\n"; echo $controller_name . " changes to " . $new_controller_name . "\n"; rename($source, $dest); $contents = file_get_contents($dest); $contents = str_replace($controller_name, $new_controller_name, $contents); file_put_contents($dest, $contents); } }
/** * Retrieve all objects matching array of passed property/value pairs * * @param PDO object $db Optional PDO object for recycling existing connections * * @param array $properties Array of class property names and values to filter on * * @param array $order Array of class property names to order results by * * @param array $direction String Order 'asc' or 'desc' - Only does something if an array * was passed for $order parameter. * * @param array $limit Array containing one or two elements. If a single element is passed, * it will return a dataset of that size or less. If two elements are * set, the first will be an offset and the second a range * examples: The following would return a thousand records following the first 100: * array(0 => 100, 1 => 1000) * * The following would simply return 700 records: * array(0 => 700) * * * @return Array of matching objects or false if not found */ public static function getWith($db = null, $properties, $order = array(), $direction = 'asc', $limit = array()) { //Dynamically find class name and ensure it exists $peer_class = preg_replace('/Collection$/', '', get_called_class()); if (class_exists($peer_class) && $properties != array()) { //Build the basic select query $peer_object = new $peer_class(); if ($db == null) { $db = self::fetchDB(); } $cols = array(); foreach ($properties as $property => $value) { $col_name = Dinkly::convertFromCamelCase($property); if (array_key_exists($col_name, $peer_object->registry)) { $cols[$col_name] = $value; } } $where = ''; $is_valid = false; foreach ($cols as $col => $value) { if (is_array($value) && count($value) > 1) { $in_string = ''; foreach ($value as $temp) { $in_string .= $db->quote($temp) . ','; } $in_string = trim($in_string, ','); $where .= ' AND `' . $col . '` IN (' . $in_string . ')'; $is_valid = true; } elseif (is_array($value) && count($value) === 1) { $where .= ' AND `' . $col . '` = ' . $db->quote($value[0]); $is_valid = true; } elseif (!is_array($value)) { $where .= ' AND `' . $col . '` = ' . $db->quote($value); $is_valid = true; } } if ($is_valid) { $where = ' where ' . trim($where, ' AND'); } //Enforce an order on the results $is_valid = false; if ($order != array()) { $chunk = ' order by '; foreach ($order as $p) { $col_name = Dinkly::convertFromCamelCase($p); if (array_key_exists($col_name, $peer_object->registry)) { $chunk .= $col_name . ', '; $is_valid = true; } } if ($is_valid) { $where .= rtrim($chunk, ', '); if ($direction == 'asc') { $where .= ' ASC'; } else { if ($direction == 'desc') { $where .= ' DESC'; } } } } //Enforce a limit on the results $is_valid = false; if ($limit != array()) { $chunk = " limit "; if (is_numeric($limit[0])) { $chunk .= $limit[0]; $is_valid = true; } if (isset($limit[1])) { if (is_numeric($limit[1])) { $chunk .= ', ' . $limit[1]; } } if ($is_valid) { $where .= $chunk; } } return self::getCollection($peer_object, $peer_object->getSelectQuery() . $where, $db); } }
</div><!-- Primary Container --> <?php echo $this->getModuleFooter(); ?> <div id="footer" class="container-fluid"> <div class="container"> <footer> <p>Copyright © <?php echo Dinkly::getConfigValue('app_name') . ' ' . date('Y'); ?> </p> </footer> </div> </div> </body> </html>
/** * Clear Dinkly session variables to log out user * * * */ public static function logout() { $_SESSION['dinkly']['auth'][Dinkly::getCurrentAppName()] = null; }
?> <?php if (Dinkly::getCurrentView() == 'default') { ?> <li><?php echo ucwords(str_replace('_', ' ', Dinkly::getCurrentModule())); ?> </li> <?php } else { ?> <li><a href="/doc/<?php echo Dinkly::getCurrentModule(); ?> "><?php echo ucwords(str_replace('_', ' ', Dinkly::getCurrentModule())); ?> </a></li> <li><?php echo ucwords(str_replace('_', ' ', Dinkly::getCurrentView())); ?> </li> <?php } ?> <?php } ?> </ol> <?php }
/** * Update an existing database object * * @return bool true on success of query or no changes to be made or false on failed DB execution * @throws Exception if unable to perform update of object */ protected function update() { if (!$this->db) { throw new Exception("Unable to perform update without a database object"); } $reg = $this->getRegistry(); $is_valid = false; $query = "update " . $this->getDBTable() . " set "; $i = 1; foreach ($this->getColumns() as $col) { if (array_key_exists($col, $this->regDirty)) { if ($i < sizeof($this->regDirty)) { $query .= $col . "=" . $this->db->quote($this->{$reg}[$col]) . ", "; $is_valid = true; } else { if (sizeof($this->regDirty) == 1 || $i == sizeof($this->regDirty)) { $query .= $col . "=" . $this->db->quote($this->{$reg}[$col]); $is_valid = true; } } $i++; } } $primaryKey = $this->getPrimaryKey(); $query .= " where " . $primaryKey . "='" . $this->{Dinkly::convertToCamelCase($primaryKey, true)} . "'"; if ($is_valid) { return $this->db->exec($query); } else { return false; } }
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"> </script> <![endif]--> <script type="text/javascript" src="/js/jquery-1.11.2.min.js"></script> <script type="text/javascript" src="/js/bootstrap.min.js"></script> <script type="text/javascript" src="/js/typeahead.bundle.js"></script> <?php echo $this->getModuleHeader(); ?> </head> <body> <div class="container"><!-- Primary Container --> <?php if (Dinkly::getCurrentEnvironment() == 'dev') { ?> <!-- Handy Dev Mode Info Label --> <h3 class="dev-mode-indicator-label"> <span class="label label-warning"> Dev Mode </span> </h3> <h3 class="dev-mode-info-label"> <span class="label label-info"> <?php echo $this->getCurrentModule(); ?> -> <?php echo $this->getCurrentView(); ?>
/** * Load a specific fixture to populate DB table * * @param string $set: folder name of fixtures you would like to load * @param string $model_name: name model fixture to be parsed * @param bool $truncate (optional): truncate the table if set to true, or append records if false * @param bool $verbose_output (optional): how chatty would you like the build to be? * @param string $override_database_name (optional): if passed, this will override the name of the database as it appears in config/db.yml * * @return bool true if loaded successfully, false if load fails */ public static function loadFixture($set, $model_name, $plugin_name = null, $truncate = true, $verbose_output = true, $override_database_name = null) { //Use the proper DB credentials, or apply a passed-in override $creds = DinklyDataConfig::getDBCreds(); if ($override_database_name) { $creds['name'] = $override_database_name; DinklyDataConfig::setActiveConnection($creds); } //Create database if it doesn't exist $db = self::fetchDB($creds); $file_path = null; if ($plugin_name) { $file_path = $_SERVER['APPLICATION_ROOT'] . "plugins/" . $plugin_name . "/config/fixtures/" . $set . "/" . $model_name . ".yml"; } else { $file_path = $_SERVER['APPLICATION_ROOT'] . "config/fixtures/" . $set . "/" . $model_name . ".yml"; } if ($verbose_output) { echo "Attempting to parse '" . $model_name . "' fixture yaml..."; } $fixture = Yaml::parse($file_path); $model_name = $collection_name = null; if (isset($fixture['table_name'])) { $model_name = Dinkly::convertToCamelCase($fixture['table_name'], true); if ($verbose_output) { echo "success!\n"; } } else { return false; } if (isset($fixture['records'])) { if ($truncate) { if ($verbose_output) { echo "Truncating '" . $fixture['table_name'] . "'..."; } $db->exec("truncate table " . $fixture['table_name']); if ($verbose_output) { echo "success!\n"; } } $count = sizeof($fixture['records']); if ($verbose_output) { echo "Inserting " . $count . " record(s) into table '" . $fixture['table_name'] . "'"; } foreach ($fixture['records'] as $pos => $record) { if ($verbose_output) { echo "..."; } $model = new $model_name(); foreach ($record as $col_name => $value) { //Automatically set created date if none was passed if ($col_name == 'created_at' && $value == "") { $value = date('Y-m-d G:i:s'); } $set_field = 'set' . Dinkly::convertToCamelCase($col_name, true); $model->{$set_field}($value); } $model->save(); } if ($verbose_output) { echo "success!\n"; } return true; } }