示例#1
0
 public function minAccessLevel()
 {
     $min_access_query = SimpleQuery::create('SELECT MIN(access_level) as min_access_level FROM plt_access INNER JOIN plt_group ON plt_access.access_id = plt_group.access_id INNER JOIN plt_user_in_group ON plt_group.group_id = plt_user_in_group.group_id INNER JOIN plt_user ON plt_user_in_group.user_id = plt_user.user_id WHERE plt_user.user_id = ' . $this->id());
     $min_access_level = $min_access_query->fieldValue('min_access_level');
     if (!is_numeric($min_access_level)) {
         $min_access_level = 9999;
     }
     return $min_access_level;
 }
示例#2
0
 /**
  * This function gets the description of a table from the RDBMS and then constructs
  * an array of Fields and returns them as required
  */
 public function setupFields()
 {
     /*if(!$fields = Session::getRegistered($this->table_name.'_field_array'))
       {*/
     try {
         $fields = FieldDefinitions::current()->definitionsForTable($this->table_name);
     } catch (Exception $exc) {
         if (!file_exists($this->fieldCachePath())) {
             $query = SimpleQuery::create('describe ' . $this->table_name);
             $fields = array();
             while ($row = $query->next()) {
                 $type_array = preg_split('/\\(/', $row['Type']);
                 if ($row['Extra'] == 'auto_increment') {
                     $type = 'Field::T_AUTO';
                 } else {
                     if ($row['Key'] == 'PRI') {
                         $type = 'Field::T_PRIMARY_KEY_' . strtoupper($type_array[0]) . '';
                     } else {
                         $type = 'Field::T_' . strtoupper($type_array[0]) . '';
                     }
                 }
                 eval('$field_type = ' . $type . ';');
                 $field = new Field($this->table_name, $row['Field'], $field_type);
                 $fields[$row['Field']] = $field;
                 if ($row['Default'] || !$row['Default'] && is_numeric($row['Default'])) {
                     $field->setValue($row['Default']);
                 }
             }
             @unlink($this->fieldCachePath());
             file_put_contents($this->fieldCachePath(), serialize($fields));
             @chmod($this->fieldCachePath(), 0664);
         } else {
             if (file_exists($this->fieldCachePath())) {
                 $fields = unserialize(file_get_contents($this->fieldCachePath()));
             }
         }
         //Session::register($this->table_name.'_field_array',$fields);
         FieldDefinitions::current()->addDefinitionsForTable($this->table_name, $fields);
     }
     return $fields;
 }
 public function parseForm()
 {
     $reserved = array('form_name', 'return_url', 'mail_to', 'Submit');
     $form_name = Application::param('form_name');
     $mail_to = Application::param('mail_to');
     $return_url = Application::param('return_url');
     $submission_no_query = SimpleQuery::create('SELECT COUNT(DISTINCT submission_no) AS count FROM ant_antenna_data WHERE form_name=\'' . $form_name . '\'');
     $submission_no_results = $submission_no_query->next();
     $submission_no = $submission_no_results['count'];
     $form_data = array();
     foreach (Application::post() as $key => $value) {
         if (!in_array($key, $reserved)) {
             $data = new AntennaData();
             $data->construct('', $form_name, $submission_no, $key, $value, '0');
             $data->saveLater();
             $form_data[$key] = $value;
         }
     }
     $data->purge();
     $this->emailSubmission($form_name, $mail_to, $form_data, $submission_no[count]);
 }
 function systemSetup()
 {
     try {
         /*use the DbQuery's static 'connect' method to connect to the database*/
         Query::connect();
         if (constant('DEVELOPMENT')) {
             $query = SimpleQuery::create('SHOW TABLES FROM ' . constant('DB_NAME') . ' LIKE \'%_access\'');
             if (!$query->numRows()) {
                 Query::importFile('packages/pilot/pilot.sql');
             }
         }
     } catch (DbNotFoundException $exc) {
         if (constant('DB_NAME')) {
             if (mysql_query('CREATE DATABASE ' . constant('DB_NAME'))) {
                 Query::connect();
                 Query::importFile('packages/pilot/pilot.sql');
             } else {
                 throw new DbConnectException('I don\'t have permission to create your database for you, please create the database ' . constant('DB_NAME') . ' manually and then import the file packages/pilot/pilot.sql into it');
             }
         }
     }
 }
    protected function getParams()
    {
        // TOTO: Rewrite this as a select from a temporary table populated by a select on the ss_system_data table
        $key = Query::clean(Application::param('key'));
        $sql = <<<EOF

\t\t\tSELECT ss_system.key,
\t\t\tapi_key.value AS api_key,
\t\t\tmethod.value AS method,
\t\t\tuser_id.value AS user_id,
\t\t\tphotoset_id.value AS photoset_id,
\t\t\tdisplay.value AS display,
\t\t\ttags.value AS tags,
\t\t\ttag_mode.value AS tag_mode,
\t\t\tsort.value AS sort,
\t\t\textras.value AS extras,
\t\t\tper_page.value AS per_page,
\t\t\tformat.value AS format,
\t\t\twidth.value AS width,
\t\t\theight.value AS height,
\t\t\tfade_speed.value AS fade_speed,
\t\t\ttimeout_interval.value AS timeout_interval 
\t\t\tFROM ss_system 
\t\t\tJOIN ss_system_type ON ss_system.system_type_id=ss_system_type.system_type_id  
\t\t\tJOIN ss_system_data AS api_key ON api_key.key = 'api_key' AND ss_system.system_id=api_key.system_id 
\t\t\tJOIN ss_system_data AS method ON method.key='method'  AND ss_system.system_id=method.system_id 
\t\t\tJOIN ss_system_data AS user_id ON user_id.key='user_id'  AND ss_system.system_id=user_id.system_id 
\t\t\tJOIN ss_system_data AS photoset_id ON photoset_id.key='photoset_id'  AND ss_system.system_id=photoset_id.system_id 
\t\t\tJOIN ss_system_data AS display ON display.key='display'  AND ss_system.system_id= display.system_id 
\t\t\tJOIN ss_system_data AS tags ON tags.key='tags'  AND ss_system.system_id= tags.system_id 
\t\t\tJOIN ss_system_data AS tag_mode ON tag_mode.key='tag_mode'  AND ss_system.system_id= tag_mode.system_id 
\t\t\tJOIN ss_system_data AS sort ON sort.key='sort'  AND ss_system.system_id= sort.system_id 
\t\t\tJOIN ss_system_data AS extras ON extras.key='extras'  AND ss_system.system_id= extras.system_id 
\t\t\tJOIN ss_system_data AS per_page ON per_page.key='per_page'  AND ss_system.system_id= per_page.system_id 
\t\t\tJOIN ss_system_data AS format ON format.key='format'  AND ss_system.system_id= format.system_id 
\t\t\tJOIN ss_system_data AS width ON width.key='width'  AND ss_system.system_id= width.system_id 
\t\t\tJOIN ss_system_data AS height ON height.key='height' AND ss_system.system_id= height.system_id 
\t\t\tJOIN ss_system_data AS fade_speed ON fade_speed.key='fade_speed' AND ss_system.system_id= fade_speed.system_id 
\t\t\tJOIN ss_system_data AS timeout_interval ON timeout_interval.key='timeout_interval' AND ss_system.system_id= timeout_interval.system_id 
\t\t\tWHERE ss_system.key = '{$key}' LIMIT 0,1
EOF;
        //echo "<br />DEBUG \$sql == $sql<br />\n";
        $system_query = SimpleQuery::create($sql);
        // Set the params
        $params = array();
        //echo "<br />DEBUG api_key: ".$system_query->fieldValue('api_key')."<br />\n";
        if ($key = Query::clean(Application::param('key'))) {
            $params['key'] = $key;
        } else {
            $params['key'] = $system_query->fieldValue('key');
        }
        if ($api_key = Query::clean(Application::param('api_key'))) {
            $params['api_key'] = $api_key;
        } else {
            $params['api_key'] = $system_query->fieldValue('api_key');
        }
        if ($method = Query::clean(Application::param('method'))) {
            $params['method'] = $method;
        } else {
            $params['method'] = $system_query->fieldValue('method');
        }
        if ($user_id = Query::clean(Application::param('user_id'))) {
            $params['user_id'] = $user_id;
        } else {
            $params['user_id'] = $system_query->fieldValue('user_id');
        }
        if ($photoset_id = Query::clean(Application::param('photoset_id'))) {
            $params['photoset_id'] = $photoset_id;
        } else {
            $params['photoset_id'] = $system_query->fieldValue('photoset_id');
        }
        if ($display = Query::clean(Application::param('display'))) {
            $params['display'] = $display;
        } else {
            $params['display'] = $system_query->fieldValue('display');
        }
        if ($tags = Query::clean(Application::param('tags'))) {
            $params['tags'] = $tags;
        } else {
            $params['tags'] = $system_query->fieldValue('tags');
        }
        if ($tag_mode = Query::clean(Application::param('tag_mode'))) {
            $params['tag_mode'] = $tag_mode;
        } else {
            $params['tag_mode'] = $system_query->fieldValue('tag_mode');
        }
        if ($sort = Query::clean(Application::param('sort'))) {
            $params['sort'] = $sort;
        } else {
            $params['sort'] = $system_query->fieldValue('sort');
        }
        if ($extras = Query::clean(Application::param('extras'))) {
            $params['extras'] = $extras;
        } else {
            $params['extras'] = $system_query->fieldValue('extras');
        }
        if ($per_page = Query::clean(Application::param('per_page'))) {
            $params['per_page'] = $per_page;
        } else {
            $params['per_page'] = $system_query->fieldValue('per_page');
        }
        if ($format = Query::clean(Application::param('format'))) {
            $params['format'] = $format;
        } else {
            $params['format'] = $system_query->fieldValue('format');
        }
        if ($width = Query::clean(Application::param('width'))) {
            $params['width'] = $width;
        } else {
            $params['width'] = $system_query->fieldValue('width');
        }
        if ($height = Query::clean(Application::param('height'))) {
            $params['height'] = $height;
        } else {
            $params['height'] = $system_query->fieldValue('height');
        }
        if ($fade_speed = Query::clean(Application::param('fade_speed'))) {
            $params['fade_speed'] = $fade_speed;
        } else {
            $params['fade_speed'] = $system_query->fieldValue('fade_speed');
        }
        if ($timeout_interval = Query::clean(Application::param('timeout_interval'))) {
            $params['timeout_interval'] = $timeout_interval;
        } else {
            $params['timeout_interval'] = $system_query->fieldValue('timeout_interval');
        }
        return $params;
    }
 function getLimitedIds($table_name, $fields, $start, $num_per_page)
 {
     $field_names = array();
     foreach ($fields as $field) {
         $field_names[] = '`' . $table_name . '`.`' . $field->name() . '`';
     }
     $field_string = implode(',', $field_names);
     if ($aggs_string = $this->aggsString()) {
         $field_string .= ',' . $aggs_string;
     }
     $this->query_string = 'select SQL_CALC_FOUND_ROWS distinct ' . $field_string . ' ';
     if ($having_fields = $this->havingFields()) {
         $this->query_string .= ',' . $having_fields;
     }
     $this->query_string .= ' from ' . $this->from_clause;
     if ($this->db_clause) {
         $this->query_string .= $this->db_clause->toString();
     }
     if ($this->group_by) {
         $this->query_string .= ' group by ' . $this->group_by;
     }
     if ($having_string = $this->havingString()) {
         $this->query_string .= $having_string;
     }
     //' having('.$this->having.')';
     if (!empty($this->order_by)) {
         $this->query_string .= ' order by ' . implode(',', $this->order_by);
     }
     if ($this->descending) {
         $this->query_string .= ' DESC';
     }
     $this->query_string .= ' LIMIT ' . $start . ',' . $num_per_page;
     $this->execute();
     $ret = array();
     while ($row = $this->next()) {
         $ret[] = $row;
     }
     $query = SimpleQuery::create('SELECT FOUND_ROWS() as found_rows');
     $this->found_rows = $query->fieldValue('found_rows');
     return $ret;
 }
 /**
  * Makes sure that each of your db objects is able to 
  * be inserted, updated and deleted, inserts test data
  * of an appropriate type according to the type of db 
  * field and then inserts and reloads it to ensure the
  * data are identical when reloaded from the db. You
  * can also add a testData method to your db objects
  * that should return appropriate data to test each
  * field when passed the name of that field.
  */
 public function testBasicCrud()
 {
     $this->info('<h1>Testing Basic C.R.U.D</h1>');
     $ret = array();
     $list = directoryList('packages/' . str_replace('.', '/', $this->packageName()), true);
     foreach ($list as $dir => $files) {
         foreach ($files as $file) {
             if (strpos($file, '.class.php') !== FALSE && strpos($file, '.swp') === FALSE) {
                 $path = $dir . '/' . $file;
                 $res = exec('php -f ' . $path);
                 $classname = rsFileToClass($file);
                 if (strpos($res, 'Parse error') !== FALSE) {
                     echo $classname . ': ' . $res . "\n";
                 } else {
                     require_once $path;
                     if (strpos($classname, 'DbObject') === FALSE) {
                         @($obj = new $classname());
                         if ($obj instanceof DbObject) {
                             $ret[] = $classname;
                             echo '<h2>Testing: ' . $classname . '</h2>';
                             $obj->clearTable();
                             echo 'Cleared ' . $obj->tableName() . '<br />';
                             $this->insertNewObjectWithTestData($obj);
                             echo 'Saved: ' . $classname . ': ' . $obj->completeKey() . '<br />';
                             $obj = new $classname();
                             $obj->noForeign();
                             if ($loaded = $obj->fetchSingle()) {
                                 echo 'Loaded: ' . $classname . ': ' . $loaded->completeKey() . '<br />';
                                 foreach ($loaded->fields() as $field) {
                                     if (method_exists($obj, 'testData')) {
                                         $test_data = $obj->testData($field->name());
                                     } else {
                                         $test_data = $this->testData($field->getTypeAsString());
                                     }
                                     $loaded_data = $loaded->get($field->name());
                                     if ($test_data != $loaded_data && $test_data !== FALSE) {
                                         $this->dataModelError($field->name(), $test_data, $loaded_data, 'You need to check your fields in the table definition for: ' . $loaded->tableName());
                                     } else {
                                         $this->info('field: ' . $field->name() . ' is ' . $field->getTypeAsString());
                                     }
                                 }
                                 $obj = new $classname();
                                 try {
                                     $new = $this->insertNewObjectWithTestData($obj);
                                 } catch (DbCrudException $exc) {
                                     $error = 'tried to save a second object and got: ' . $exc->getMessage();
                                     $cause = 'make sure you have an auto increment key in your table if required';
                                     $this->error($error, $cause);
                                 }
                                 $this->info('Saved: ' . $classname . ': ' . $obj->completeKey() . ' (no duplicate keys)');
                                 $loaded->delete();
                                 $new->delete();
                                 $obj = new $classname();
                                 $obj->noForeign();
                                 if (!$obj->fetchSingle()) {
                                     $this->info('Deleted: ' . $classname . ', table is now empty');
                                 } else {
                                     $this->error('Could not delete: ' . $classname, 'Beats me. If you can load it you should be able to delete it! Do some testing. This might be a RocketSled bug');
                                 }
                                 SimpleQuery::create('TRUNCATE ' . $obj->tableName());
                                 $obj->clearQueryCache();
                             } else {
                                 $this->error('Failed to load: ' . $classname, 'This is probably something wrong with the table definition in your data model');
                             }
                         }
                     }
                 }
             }
         }
     }
     return $ret;
 }
 /**
  * Execute a simple query and return the resulting query object
  * 
  * Simple here does not actually mean 'simple', ie. the query 
  * executed here can be as complex as you like with a billion joins,
  * clauses and aggregate functions out the wazoo.
  *
  * The reason this is called simpleQuery is because it is for use
  * when a query is not going to change at runtime. So if you know 
  * that a database query is always going to be the same, every time
  * the script runs, and does not need to be built at runtime, 
  * (you can still substitute variables into the string), then the
  * simpleQuery method is ideal, however bear in mind that you cannot
  * use a Clause object with the simpleQuery method
  * @param string                    - The query string to be executed
  * @return {@link SimpleQuery}    - a Query object containing
  *                                    all the data resulting from the
  *                                    query string passed.
  * @access public
  */
 function create($query_string)
 {
     $query = new SimpleQuery($query_string);
     $query->doQuery();
     return $query;
 }
 public function publishLookupTables()
 {
     $ucv = new UserCanView();
     SimpleQuery::create('TRUNCATE ' . $ucv->tableName());
     $ucv->clearQueryCache();
     $user = new User();
     $cont = true;
     $num = 1;
     while ($cont) {
         try {
             $users = $user->page($num, 10)->objects();
             $num++;
             foreach ($users as $user) {
                 $entry = new Entry();
                 $cont_entry = true;
                 $entry_num = 1;
                 while ($cont_entry) {
                     try {
                         $entries = $entry->page($entry_num, 10)->objects();
                         $entry_num++;
                         foreach ($entries as $entry) {
                             if (LogbookAccess::userCanDoAction($user, $entry, LogbookAccess::VIEW)) {
                                 $ucv = new UserCanView();
                                 $ucv->construct($entry->get('author_id'), $entry->id(), $user->id());
                                 $ucv->save();
                             }
                         }
                     } catch (Exception $exc) {
                         $cont_entry = false;
                     }
                 }
             }
         } catch (Exception $exc) {
             $cont = false;
         }
     }
 }