function __construct($locations = NULL)
 {
     $dbdir = any($locations['tmp'], 'tmp');
     if (class_exists("Outlet")) {
         Outlet::init(array('connection' => array('type' => 'datamanager'), 'classes' => array()));
         $this->outlet =& Outlet::getInstance();
     }
 }
 public function getChildren($obj)
 {
     $outlet = Outlet::getInstance();
     $cls = $this->cls;
     $left = $this->left;
     $right = $this->right;
     // qualifiers
     $w = '';
     foreach ($this->qualifiers as $field) {
         $w .= "{n.{$field}} = " . $outlet->quote($obj->{$field}) . " AND ";
     }
     $children = $outlet->from("{$cls} n")->leftJoin("{" . "{$cls} parent} ON ({parent.{$left}} < {n.{$left}} AND {parent.{$right}} > {n.{$right}})")->where("\n\t\t\t\t\t{$w}\n\t\t\t\t\t{n.{$left}} > ?\n\t\t\t\t\tAND {n.{$right}} < ?\n\t\t\t\t\tAND {parent.{$left}} >= ?\n\t\t\t\t\t\n\t\t\t\t", array($obj->Left, $obj->Right, $obj->Left))->select("COUNT({parent.{$left}})")->groupBy("{n.{$left}}, {n.{$right}}")->having("COUNT({parent.{$left}}) = 1")->find();
     return $children;
 }
 function testMap()
 {
     $outlet = Outlet::getInstance();
     $p = new Project();
     $p->setName('Project 1');
     $outlet->save($p);
     $p_map = $outlet->select('Project', 'where {Project.ProjectID} = ?', array($p->getProjectID()));
     $p_map = $p_map[0];
     $this->assertTrue($p === $p_map, 'Diferent object on identity map');
     $outlet->clearCache();
     $p_map = $outlet->select('Project', 'where {Project.ProjectID} = ?', array($p->getProjectID()));
     $p_map = $p_map[0];
     $this->assertTrue($p !== $p_map, 'Equal object on identity map');
 }
 function testUpdateAfterRelationshipUpdate()
 {
     $outlet = Outlet::getInstance();
     $p = new Project();
     $p->setName('Name 1');
     $b = new Bug();
     $b->Title = 'Test Bug';
     $p->addBug($b);
     $outlet->save($p);
     $projectid = $p->getProjectID();
     $p->setName('Name 2');
     $outlet->save($p);
     $p = $outlet->load('Project', $projectid);
     $this->assertEquals($p->getName(), 'Name 2');
 }
 function controller_playlist($args, $output = "inline")
 {
     $vars["args"] = $args;
     $mpdplaylistnames = $this->playlist->getPlaylists();
     $vars["currentplaylist"] = $this->playlist->getPlaylistInfo();
     $status = $this->playlist->getStatus();
     if (!empty($mpdplaylistnames)) {
         $outlet = Outlet::getInstance();
         $dbplaylists = $outlet->select("AudioPlaylist", "ORDER BY {AudioPlaylist.last_listened} DESC");
         foreach ($dbplaylists as $playlist) {
             $vars["playlists"][] = $playlist;
         }
     } else {
         print_pre("ERROR: MPD has no playlists!");
     }
     return $this->GetTemplate("./playlist.tpl", $vars);
 }
Exemple #6
0
 function controller_orm($args)
 {
     /*
         if (!User::authorized("orm"))
           throw new Exception("not allowed");
     */
     $ret = $this->GetComponentResponse("./orm.tpl");
     /*
     $user = User::singleton();
     if (!($user->isLoggedIn() && $user->HasRole("ORM"))) // display the access violation message
       $ret->SetTemplate("access_violation.tpl");
     */
     if (!empty($args["ormaction"]) && !empty($args["model"])) {
         $ret["model"] = $args["model"];
         $ret["ormcfg"] = new OrmModel($args["model"]);
         if ($args["ormaction"] == "create" && !empty($args["classname"])) {
             $ormclass = new OrmClass($ret["ormcfg"]->classes->{$args["classname"]});
             //$sql = $ormclass->getCreateSql();
             $sqlkey = "db." . $ormclass->table . ".create:nocache";
             /*
                     print_pre($ormclass->table);
                     print_pre($ormclass->getColumns());
             */
             $outlet = Outlet::getInstance();
             $pdo = $outlet->getConnection()->getPDO();
             if ($pdo) {
                 $ret["sql"] = $ormclass->getCreateSQL();
                 try {
                     $pdo->query($ret["sql"]);
                     $ret["success"] = "Table '{$ormclass->table}' created successfully";
                 } catch (Exception $e) {
                     $ret["error"] = $e->getMessage();
                 }
             }
             /*
             $data = DataManager::singleton();
             $data->QueryCreate($sqlkey, $ormclass->table, $ormclass->getColumns($sql));
             */
         }
     }
     return $ret;
 }
Exemple #7
0
 function importCategories()
 {
     $catfile = file_get_contents("/home/bai/yelp-categories.txt");
     $catstruct = array('', 'what');
     $levels = array('0' => 0, '#' => 1, '*' => 2, 'o' => 3, '+' => 4);
     $lastsymbol = '#';
     foreach (explode("\n", $catfile) as $line) {
         if (preg_match("/^\\s*?([#*o+])\\s+(.*?)\\s+\\(([^)]*?)\\)\\s*\$/", $line, $m)) {
             $diff = $levels[$m[1]] - $levels[$lastsymbol];
             #print "(" . $1 . " vs " . $lastsymbol . ") " . $levels{$1} . " > " . $levels{$lastsymbol} ." ? $diff\n";
             if ($diff > 0) {
                 array_push($catstruct, $m[3]);
                 #print "go down a level\n";
             } else {
                 if ($diff < 0) {
                     for ($i = 0; $i > $diff; $i--) {
                         #print "POP ";
                         array_pop($catstruct);
                     }
                     #      print "go up a level\n";
                     $catstruct[count($catstruct) - 1] = $m[3];
                 } else {
                     $catstruct[count($catstruct) - 1] = $m[3];
                     #      print "SET IT TO $3";
                 }
             }
             print "send {$m['3']} for '{$m['2']}' (parent is " . $catstruct[count($catstruct) - 2] . ")\n";
             $category = new NavigationLocationCategory();
             $category->categoryid = $m[3];
             $category->name = $m[2];
             $category->parent = $catstruct[count($catstruct) - 2];
             $outlet = Outlet::getInstance();
             $outlet->Save($category);
             $lastsymbol = $m[1];
         }
     }
 }
 function setUp()
 {
     // create database
     $pdo = Outlet::getInstance()->getConnection();
     switch (DATABASE_DRIVER) {
         case 'mysql':
             OutletTestSetup::createMySQLTables($pdo);
             break;
         case 'pgsql':
             OutletTestSetup::createPostgresTables($pdo);
             break;
         case 'sqlite':
         default:
             OutletTestSetup::createSQLiteTables($pdo);
     }
     $pdo->exec('DELETE FROM projects');
     $pdo->exec('DELETE FROM addresses');
     $pdo->exec('DELETE FROM bugs');
     $pdo->exec('DELETE FROM machines');
     $pdo->exec('DELETE FROM users');
     $pdo->exec('DELETE FROM watchers');
     $pdo->exec('DELETE FROM profiles');
     parent::setUp();
 }
Exemple #9
0
<?php

// set this to mysql, mssql, or sqlite
define('DATABASE_DRIVER', 'sqlite');
set_include_path(dirname(__FILE__) . '/../classes' . PATH_SEPARATOR . get_include_path());
// outlet
require_once 'outlet/Outlet.php';
require_once 'entities.php';
// basic setup
switch (DATABASE_DRIVER) {
    case 'sqlite':
        $conf = (include 'outlet-config-sqlite.php');
        break;
    case 'mysql':
        $conf = (include 'outlet-config-mysql.php');
        break;
    case 'pgsql':
        $conf = (include 'outlet-config-pgsql.php');
        break;
    default:
        throw new Exception('Unsupported database driver: ' . DATABASE_DRIVER);
}
Outlet::init($conf);
Outlet::getInstance()->createProxies();
Outlet::getInstance()->getConnection()->getPDO()->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
Exemple #10
0
 /**
  * @return array
  */
 function find()
 {
     $outlet = Outlet::getInstance();
     // get the 'from'
     $tmp = explode(' ', $this->from);
     $from = $tmp[0];
     $from_aliased = count($tmp) > 1 ? $tmp[1] : $tmp[0];
     $config = Outlet::getInstance()->getConfig();
     $entity_config = $config->getEntity($from);
     $props = $entity_config->getProperties();
     $from_props = $props;
     $select_cols = array();
     foreach ($props as $key => $p) {
         $select_cols[] = "\n{" . $from_aliased . '.' . $key . '} as ' . $from_aliased . '_' . $key;
     }
     // get the include entities
     $with = array();
     $with_aliased = array();
     $join_q = '';
     foreach ($this->with as $with_key => $j) {
         $tmp = explode(' ', $j);
         $with[$with_key] = $tmp[0];
         $with_aliased[$with_key] = count($tmp) > 1 ? $tmp[1] : $tmp[0];
         $assoc = $entity_config->getAssociation($with[$with_key]);
         if (!$assoc) {
             throw new OutletException('No association found with entity or alias [' . $with[$with_key] . ']');
         }
         $props = $config->getEntity($assoc->getForeign())->getProperties();
         foreach ($props as $key => $p) {
             $select_cols[] = "\n{" . $with_aliased[$with_key] . '.' . $key . '} as ' . $with_aliased[$with_key] . '_' . $key;
         }
         $aliased_join = $with_aliased[$with_key];
         $join_q .= "LEFT JOIN {" . $assoc->getForeign() . " " . $aliased_join . "} ON {" . $from_aliased . '.' . $assoc->getKey() . "} = {" . $with_aliased[$with_key] . '.' . $assoc->getRefKey() . "} \n";
     }
     $q = "SELECT " . implode(', ', $select_cols) . " \n";
     if ($this->select) {
         $q .= ", " . $this->select;
     }
     $q .= " FROM {" . $this->from . "} \n";
     $q .= $join_q;
     $q .= implode("\n", $this->joins);
     if ($this->query) {
         $q .= 'WHERE ' . $this->query . "\n";
     }
     if ($this->groupBy) {
         $q .= 'GROUP BY ' . $this->groupBy . "\n";
     }
     if ($this->orderby) {
         $q .= 'ORDER BY ' . $this->orderby . "\n";
     }
     if ($this->having) {
         $q .= 'HAVING ' . $this->having . "\n";
     }
     // TODO: Make it work on MS SQL
     //       In SQL Server 2005 http://www.singingeels.com/Articles/Pagination_In_SQL_Server_2005.aspx
     if ($this->limit) {
         $q .= 'LIMIT ' . $this->limit;
         if ($this->offset) {
             $q .= ' OFFSET ' . $this->offset;
         }
     }
     $stmt = $outlet->query($q, $this->params);
     $res = array();
     while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
         $data = array();
         // Postgres returns columns as lowercase
         // TODO: Maybe everything should be converted to lower in query creation / processing to avoid this
         if ($outlet->getConnection()->getDialect() == 'pgsql') {
             foreach ($from_props as $key => $p) {
                 $data[$p[0]] = $row[strtolower($from_aliased) . '_' . strtolower($key)];
             }
         } else {
             foreach ($from_props as $key => $p) {
                 $data[$p[0]] = $row[$from_aliased . '_' . $key];
             }
         }
         $obj = $outlet->getEntityForRow($from, $data);
         foreach ($with as $with_key => $w) {
             $a = $entity_config->getAssociation($w);
             if ($a) {
                 $data = array();
                 $setter = $a->getSetter();
                 $foreign = $a->getForeign();
                 $with_entity = $config->getEntity($foreign);
                 if ($a instanceof OutletOneToManyConfig) {
                     // TODO: Implement...
                 } elseif ($a instanceof OutletManyToManyConfig) {
                     // TODO: Implement...
                 } else {
                     // Postgres returns columns as lowercase
                     // TODO: Maybe everything should be converted to lower in query creation / processing to avoid this
                     if ($outlet->getConnection()->getDialect() == 'pgsql') {
                         foreach ($with_entity->getProperties() as $key => $p) {
                             $data[$p[0]] = $row[strtolower($with_aliased[$with_key] . '_' . $key)];
                         }
                     } else {
                         foreach ($with_entity->getProperties() as $key => $p) {
                             $data[$p[0]] = $row[$with_aliased[$with_key] . '_' . $key];
                         }
                     }
                     $f = $with_entity->getPkFields();
                     // check to see if we found any data for the related entity
                     // using the pk
                     $data_returned = false;
                     $pk_values = array();
                     foreach ($f as $k) {
                         if (isset($data[$k])) {
                             $data_returned = true;
                             break;
                         }
                     }
                     // only fill object if there was data returned
                     if ($data_returned) {
                         $obj->{$setter}($outlet->getEntityForRow($foreign, $data));
                     }
                 }
             }
         }
         $res[] = $obj;
     }
     return $res;
 }
 function __construct()
 {
     Outlet::init(array('connection' => array('type' => 'datamanager'), 'classes' => array()));
     $this->outlet =& Outlet::getInstance();
 }
 function getUser()
 {
     if (is_null($this->UserID)) {
         return parent::getUser();
     }
     if (is_null(parent::getUser()) && $this->UserID) {
         parent::setUser(Outlet::getInstance()->load('User', $this->UserID));
     }
     return parent::getUser();
 }
 function testPagination()
 {
     $outlet = Outlet::getInstance();
     $totalRecords = 25;
     for ($i = 0; $i < $totalRecords; $i++) {
         $project = new Project();
         $project->setName('Test Project ' . $i);
         $outlet->save($project);
     }
     $this->assertEquals(10, count($outlet->from('Project')->limit(10)->find()));
     $this->assertEquals(5, count($outlet->from('Project')->limit(10)->offset(20)->find()));
 }
 function controller_location_categories($args, $output = "inline")
 {
     $targetid = !empty($args["targetid"]) ? $args["targetid"] : "index_content";
     if (isset($args["showcategory"])) {
         if (is_array($args["showcategory"])) {
             $_SESSION["navigation"]["location"]["categories"] = array();
             foreach ($args["showcategory"] as $cat => $enabled) {
                 if ($enabled) {
                     $_SESSION["navigation"]["location"]["categories"][] = $cat;
                 }
             }
         } else {
             if (!empty($args["showcategory"])) {
                 $_SESSION["navigation"]["location"]["categories"] = explode(" ", $args["showcategory"]);
             } else {
                 unset($_SESSION["navigation"]["location"]["categories"]);
             }
         }
     }
     if (!empty($args["root"])) {
         $vars["toplevel"] = false;
         $vars["root"] = $args["root"];
     } else {
         $outlet = Outlet::getInstance();
         $categories = $outlet->select("NavigationLocationCategory");
         $vars["toplevel"] = true;
         $vars["categories"] = NULL;
         $vars["root"]->children = array();
         for ($i = 0; $i < count($categories); $i++) {
             if (!isset($categories[$i]->children)) {
                 $categories[$i]->children = array();
             }
             $vars["categories"][$categories[$i]->categoryid] =& $categories[$i];
             if (!empty($categories[$i]->parent)) {
                 $vars["categories"][$categories[$i]->parent]->children[] =& $categories[$i];
             } else {
                 $vars["root"]->children[] =& $categories[$i];
             }
             if (!empty($_SESSION["navigation"]["location"]["categories"])) {
                 $categories[$i]->enabled = in_array($categories[$i]->categoryid, $_SESSION["navigation"]["location"]["categories"]);
             }
         }
     }
     $page = $this->GetTemplate("./location_categories.tpl", $vars);
     if ($output == "ajax") {
         $ret[$targetid] = $page;
     } else {
         $ret = $page;
     }
     return $ret;
 }
 static function toArray($entity)
 {
     if (!$entity) {
         throw new OutletException('You must pass an entity');
     }
     $class = self::getEntityClass($entity);
     $arr = array();
     foreach (Outlet::getInstance()->getConfig()->getEntity($class)->getProperties() as $key => $p) {
         $arr[$key] = self::toSqlValue($p, self::getProp($entity, $key));
     }
     return $arr;
 }
Exemple #16
0
<?php

// set this to mysql, mssql, or sqlite
define('DATABASE_DRIVER', 'sqlite');
set_include_path(dirname(__FILE__) . '/../classes' . PATH_SEPARATOR . get_include_path());
// outlet
require_once 'outlet/Outlet.php';
require_once 'entities.php';
// basic setup
switch (DATABASE_DRIVER) {
    case 'sqlite':
        $conf = (include 'outlet-config-sqlite.php');
        break;
    case 'mysql':
        $conf = (include 'outlet-config-mysql.php');
        break;
    case 'pgsql':
        $conf = (include 'outlet-config-pgsql.php');
        break;
    default:
        throw new Exception('Unsupported database driver: ' . DATABASE_DRIVER);
}
Outlet::init($conf);
Outlet::getInstance()->createProxies();
 function testDbFunctions()
 {
     $outlet = Outlet::getInstance();
     $p1 = new Project();
     $p1->setName('AAAA');
     $outlet->save($p1);
     $p2 = new Project();
     $p2->setName('BBBB');
     $outlet->save($p2);
     $stmt = $outlet->query('SELECT MAX({p.Name}) as max_project FROM {Project p}');
     $data = $stmt->fetchAll(PDO::FETCH_ASSOC);
     $this->assertEquals($data[0]['max_project'], 'BBBB');
 }
Exemple #18
0
<?php

set_include_path(dirname(__FILE__) . '/..' . PATH_SEPARATOR . get_include_path());
require 'outlet/Outlet.php';
require 'outlet/OutletProxyGenerator.php';
Outlet::init(require $_SERVER['argv'][1]);
$conf = (require $_SERVER['argv'][1]);
$gen = new OutletProxyGenerator(Outlet::getInstance()->getConfig());
$s = "<?php\n";
$s .= $gen->generate();
file_put_contents('outlet-proxies.php', $s);