mirror of
https://github.com/FH-Complete/FHC-Core.git
synced 2026-07-24 02:12:17 +00:00
Merge branch 'ci' of https://github.com/FH-Complete/FHC-Core into ci
This commit is contained in:
@@ -58,8 +58,13 @@ $autoload['packages'] = array();
|
||||
|
|
||||
| $autoload['libraries'] = array('user_agent' => 'ua');
|
||||
*/
|
||||
$autoload['libraries'] = array();
|
||||
//$autoload['libraries'] = array('session');
|
||||
|
||||
//$autoload['libraries'] = array();
|
||||
$autoload['libraries'] = array('session', 'FHC_Auth');
|
||||
|
||||
//$autoload['libraries'] = array();
|
||||
$autoload['libraries'] = array('session');
|
||||
|
||||
|
||||
/*
|
||||
| -------------------------------------------------------------------
|
||||
|
||||
@@ -508,7 +508,7 @@ $config['proxy_ips'] = '';
|
||||
| Autoload Custom Controllers
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
*/
|
||||
Don't work so sometime delete this*/
|
||||
function __autoload($class)
|
||||
{
|
||||
if (substr($class,0,3) !== 'CI_' && substr($class,0,4) !== 'FHC_')
|
||||
|
||||
@@ -110,7 +110,7 @@ $config['rest_realm'] = 'FHC REST API';
|
||||
| authorization key
|
||||
|
|
||||
*/
|
||||
$config['rest_auth'] = ' basic';
|
||||
$config['rest_auth'] = 'basic';
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
<?php
|
||||
|
||||
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
|
||||
if (! defined('BASEPATH'))
|
||||
exit('No direct script access allowed');
|
||||
|
||||
/**
|
||||
* Database Class
|
||||
@@ -15,28 +16,27 @@ class DBTools extends FHC_Controller
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $_seed_path = APPPATH.'seeds/';
|
||||
protected $seed_path = APPPATH.'seeds/';
|
||||
|
||||
/**
|
||||
* Seed basename regex
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $_seed_regex = '/^\d{3}_(\w+)$/';
|
||||
protected $seed_regex = '/^\d{3}_(\w+)$/';
|
||||
|
||||
/**
|
||||
* Initialize DB-Tools Class
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
|
||||
function __construct()
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
|
||||
if ($this->input->is_cli_request())
|
||||
{
|
||||
$cli=true;
|
||||
$cli = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -45,16 +45,15 @@ class DBTools extends FHC_Controller
|
||||
}
|
||||
|
||||
// can only be run in the development environment
|
||||
if (ENVIRONMENT !== 'development') {
|
||||
if (ENVIRONMENT !== 'development')
|
||||
exit('Wowsers! You don\'t want to do that!');
|
||||
}
|
||||
$this->load->database('system'); //Use the system-Connection for DB-Manipulation
|
||||
$this->load->library('migration');
|
||||
|
||||
// If not set, set it
|
||||
//$this->_seed_path !== '' OR $this->_seed_path = APPPATH.'seeds/';
|
||||
//$this->seed_path !== '' OR $this->seed_path = APPPATH.'seeds/';
|
||||
// Add trailing slash if not set
|
||||
//$this->_seed_path = rtrim($this->_seed_path, '/').'/';
|
||||
//$this->seed_path = rtrim($this->seed_path, '/').'/';
|
||||
|
||||
// Load seed language
|
||||
$this->lang->load('seed');
|
||||
@@ -68,43 +67,51 @@ class DBTools extends FHC_Controller
|
||||
log_message('info', 'DB-Tools Controller Initialized');
|
||||
}
|
||||
|
||||
public function index()
|
||||
/**
|
||||
* Main function index as help
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$result = "The following are the available command line interface commands\n\n";
|
||||
$result .= "php index.ci.php DBTools migrate [\"version_number\"] Run all migrations. The version number is optional.\n";
|
||||
$result .= "php index.ci.php DBTools migrate [\"version_number\"] Run all migrations. ";
|
||||
$result .= "The version number is optional.\n";
|
||||
$result .= "php index.ci.php DBTools seed \"file_name\" Run the specified seed file.\n";
|
||||
|
||||
echo $result . PHP_EOL;
|
||||
echo $result.PHP_EOL;
|
||||
}
|
||||
|
||||
/**
|
||||
* Migrate to latest or current version
|
||||
*
|
||||
* @param string $version One of either "latest" or "current"
|
||||
* @param string $version [optional] One of either "latest" or "current"
|
||||
* @return void
|
||||
*/
|
||||
public function migrate($version = 'latest')
|
||||
{
|
||||
|
||||
if ($this->cli && $this->migration->current() === FALSE)
|
||||
if ($this->cli && $this->migration->current() === false)
|
||||
{
|
||||
show_error($this->migration->error_string());
|
||||
}
|
||||
elseif ($version != 'latest' && $version != 'current')
|
||||
{
|
||||
$this->_failed('Migration version must be either latest or current');
|
||||
$this->__failed('Migration version must be either latest or current');
|
||||
}
|
||||
|
||||
if (!$this->migration->$version())
|
||||
{
|
||||
$this->_failed();
|
||||
$this->__failed();
|
||||
}
|
||||
|
||||
$this->_succeeded();
|
||||
$this->__succeeded();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Migrate to a specific version
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function version()
|
||||
{
|
||||
@@ -116,36 +123,39 @@ class DBTools extends FHC_Controller
|
||||
|
||||
if (!$this->migrate->version($version))
|
||||
{
|
||||
$this->_failed();
|
||||
$this->__failed();
|
||||
}
|
||||
|
||||
$this->_succeeded();
|
||||
$this->__succeeded();
|
||||
}
|
||||
|
||||
/**
|
||||
* Roll-back to the last version before current
|
||||
*
|
||||
* @param int $version The migration to rollback to, defaults to previous
|
||||
* @return void
|
||||
*/
|
||||
public function rollback($version = null)
|
||||
{
|
||||
if (is_null($version))
|
||||
{
|
||||
$version = $this->_get_version() ?: 1;
|
||||
$version = $this->__getVersion() ?: 1;
|
||||
$version--;
|
||||
}
|
||||
|
||||
// Check it's definitely false, we could be rolling back to v0
|
||||
if (false === $this->migration->version($version))
|
||||
{
|
||||
$this->_failed();
|
||||
$this->__failed();
|
||||
}
|
||||
|
||||
$this->_succeeded('rolled back');
|
||||
$this->__succeeded('rolled back');
|
||||
}
|
||||
|
||||
/**
|
||||
* ROLLBACK ALL THE THINGS!
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function uninstall()
|
||||
{
|
||||
@@ -155,17 +165,17 @@ class DBTools extends FHC_Controller
|
||||
/**
|
||||
* Seeds DB with Testdata
|
||||
*
|
||||
* @param string $name
|
||||
* @param string $name Name of the seed file.
|
||||
* @return bool
|
||||
*/
|
||||
function seed($name = null)
|
||||
public function seed($name = null)
|
||||
{
|
||||
$seeds = $this->find_seeds();
|
||||
$seeds = $this->findSeeds();
|
||||
|
||||
if (empty($seeds))
|
||||
{
|
||||
$this->_error_string = $this->lang->line('seed_none_found');
|
||||
return FALSE;
|
||||
return false;
|
||||
}
|
||||
|
||||
$method = 'seed';
|
||||
@@ -173,21 +183,21 @@ class DBTools extends FHC_Controller
|
||||
foreach ($seeds as $number => $file)
|
||||
{
|
||||
include_once($file);
|
||||
$class = 'Seed_'.ucfirst(strtolower($this->_get_seed_name(basename($file, '.php'))));
|
||||
$class = 'Seed_'.ucfirst(strtolower($this->_getSeedName(basename($file, '.php'))));
|
||||
|
||||
// Validate the seed file structure
|
||||
if ( ! class_exists($class, FALSE))
|
||||
if (! class_exists($class, false))
|
||||
{
|
||||
$this->_error_string = sprintf($this->lang->line('seed_class_doesnt_exist'), $class);
|
||||
return FALSE;
|
||||
return false;
|
||||
}
|
||||
// method_exists() returns true for non-public methods,
|
||||
// while is_callable() can't be used without instantiating.
|
||||
// Only get_class_methods() satisfies both conditions.
|
||||
elseif ( ! in_array($method, array_map('strtolower', get_class_methods($class))))
|
||||
elseif (! in_array($method, array_map('strtolower', get_class_methods($class))))
|
||||
{
|
||||
$this->_error_string = sprintf($this->lang->line('seed_missing_'.$method.'_method'), $class);
|
||||
return FALSE;
|
||||
return false;
|
||||
}
|
||||
|
||||
$pending[$number] = array($class, $method);
|
||||
@@ -207,19 +217,19 @@ class DBTools extends FHC_Controller
|
||||
*
|
||||
* @return array list of seed file paths sorted by version
|
||||
*/
|
||||
public function find_seeds()
|
||||
public function findSeeds()
|
||||
{
|
||||
$seeds = array();
|
||||
|
||||
// Load all *_*.php files in the seeds path
|
||||
foreach (glob($this->_seed_path.'*_*.php') as $file)
|
||||
foreach (glob($this->seed_path.'*_*.php') as $file)
|
||||
{
|
||||
$name = basename($file, '.php');
|
||||
|
||||
// Filter out non-seed files
|
||||
if (preg_match($this->_seed_regex, $name))
|
||||
if (preg_match($this->seed_regex, $name))
|
||||
{
|
||||
$number = $this->_get_seed_number($name);
|
||||
$number = $this->_getSeedNumber($name);
|
||||
|
||||
// There cannot be duplicate seed numbers
|
||||
if (isset($seeds[$number]))
|
||||
@@ -239,10 +249,10 @@ class DBTools extends FHC_Controller
|
||||
/**
|
||||
* Extracts the seed number from a filename
|
||||
*
|
||||
* @param string $seed
|
||||
* @param string $seed Filename of the seed.
|
||||
* @return string Numeric portion of a seed filename
|
||||
*/
|
||||
protected function _get_seed_number($seed)
|
||||
protected function _getSeedNumber($seed)
|
||||
{
|
||||
return sscanf($seed, '%[0-9]+', $number)
|
||||
? $number : '0';
|
||||
@@ -250,10 +260,10 @@ class DBTools extends FHC_Controller
|
||||
/**
|
||||
* Extracts the seed class name from a filename
|
||||
*
|
||||
* @param string $seed
|
||||
* @param string $seed Filename of the seed.
|
||||
* @return string text portion of a migration filename
|
||||
*/
|
||||
protected function _get_seed_name($seed)
|
||||
protected function _getSeedName($seed)
|
||||
{
|
||||
$parts = explode('_', $seed);
|
||||
array_shift($parts);
|
||||
@@ -264,10 +274,11 @@ class DBTools extends FHC_Controller
|
||||
* Yay, it worked! Tell the user.
|
||||
*
|
||||
* @param string $task What did we just do? We...
|
||||
* @return void
|
||||
*/
|
||||
private function _succeeded($task = 'migrated')
|
||||
private function __succeeded($task = 'migrated')
|
||||
{
|
||||
$version = $this->_get_version();
|
||||
$version = $this->__getVersion();
|
||||
exit('Successfully '.$task.' to version '.$version);
|
||||
}
|
||||
|
||||
@@ -275,19 +286,20 @@ class DBTools extends FHC_Controller
|
||||
* Output an error message when it all goes tits up
|
||||
*
|
||||
* @param string $message Error to output (default to CI's migration error)
|
||||
* @return void
|
||||
*/
|
||||
private function _failed($message = null)
|
||||
private function __failed($message = null)
|
||||
{
|
||||
$message = $message ?: $this->migration->error_string();
|
||||
show_error($message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Carbon copy of parent::_get_version, but that's protected.
|
||||
* Carbon copy of parent::__getVersion, but that's protected.
|
||||
*
|
||||
* @return int Currently installed migration number
|
||||
*/
|
||||
private function _get_version()
|
||||
private function __getVersion()
|
||||
{
|
||||
$row = $this->db->get('ci_migrations')->row();
|
||||
return $row ? $row->version : 0;
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
|
||||
<?php if (! defined('BASEPATH')) exit('No direct script access allowed');
|
||||
|
||||
class Examples extends CI_Controller {
|
||||
class Examples extends CI_Controller
|
||||
{
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
@@ -14,7 +15,7 @@ class Examples extends CI_Controller {
|
||||
|
||||
public function _example_output($output = null)
|
||||
{
|
||||
$this->load->view('example.php',$output);
|
||||
$this->load->view('example.php', $output);
|
||||
}
|
||||
|
||||
public function offices()
|
||||
@@ -38,12 +39,11 @@ class Examples extends CI_Controller {
|
||||
$crud->set_table('offices');
|
||||
$crud->set_subject('Office');
|
||||
$crud->required_fields('city');
|
||||
$crud->columns('city','country','phone','addressLine1','postalCode');
|
||||
$crud->columns('city', 'country', 'phone', 'addressLine1', 'postalCode');
|
||||
|
||||
$output = $crud->render();
|
||||
|
||||
$this->_example_output($output);
|
||||
|
||||
}catch(Exception $e){
|
||||
show_error($e->getMessage().' --- '.$e->getTraceAsString());
|
||||
}
|
||||
@@ -55,13 +55,13 @@ class Examples extends CI_Controller {
|
||||
|
||||
$crud->set_theme('datatables');
|
||||
$crud->set_table('employees');
|
||||
$crud->set_relation('officeCode','offices','city');
|
||||
$crud->display_as('officeCode','Office City');
|
||||
$crud->set_relation('officeCode', 'offices', 'city');
|
||||
$crud->display_as('officeCode', 'Office City');
|
||||
$crud->set_subject('Employee');
|
||||
|
||||
$crud->required_fields('lastName');
|
||||
|
||||
$crud->set_field_upload('file_url','assets/uploads/files');
|
||||
$crud->set_field_upload('file_url', 'assets/uploads/files');
|
||||
|
||||
$output = $crud->render();
|
||||
|
||||
@@ -73,12 +73,12 @@ class Examples extends CI_Controller {
|
||||
$crud = new grocery_CRUD();
|
||||
|
||||
$crud->set_table('customers');
|
||||
$crud->columns('customername','contactlastname','phone','city','country','salesrepemployeenumber','creditlimit');
|
||||
$crud->display_as('salesrepemployeenumber','from Employeer')
|
||||
->display_as('customername','Name')
|
||||
->display_as('contactlastname','Last Name');
|
||||
$crud->columns('customername', 'contactlastname', 'phone', 'city', 'country', 'salesrepemployeenumber', 'creditlimit');
|
||||
$crud->display_as('salesrepemployeenumber', 'from Employeer')
|
||||
->display_as('customername', 'Name')
|
||||
->display_as('contactlastname', 'Last Name');
|
||||
$crud->set_subject('Customer');
|
||||
$crud->set_relation('salesrepemployeenumber','employees','lastname');
|
||||
$crud->set_relation('salesrepemployeenumber', 'employees', 'lastname');
|
||||
|
||||
$output = $crud->render();
|
||||
|
||||
@@ -89,8 +89,8 @@ class Examples extends CI_Controller {
|
||||
{
|
||||
$crud = new grocery_CRUD();
|
||||
|
||||
$crud->set_relation('customerNumber','customers','{contactLastName} {contactFirstName}');
|
||||
$crud->display_as('customerNumber','Customer');
|
||||
$crud->set_relation('customerNumber', 'customers', '{contactLastName} {contactFirstName}');
|
||||
$crud->display_as('customerNumber', 'Customer');
|
||||
$crud->set_table('orders');
|
||||
$crud->set_subject('Order');
|
||||
$crud->unset_add();
|
||||
@@ -108,7 +108,7 @@ class Examples extends CI_Controller {
|
||||
$crud->set_table('products');
|
||||
$crud->set_subject('Product');
|
||||
$crud->unset_columns('productDescription');
|
||||
$crud->callback_column('buyPrice',array($this,'valueToEuro'));
|
||||
$crud->callback_column('buyPrice', array($this,'valueToEuro'));
|
||||
|
||||
$output = $crud->render();
|
||||
|
||||
@@ -125,11 +125,11 @@ class Examples extends CI_Controller {
|
||||
$crud = new grocery_CRUD();
|
||||
|
||||
$crud->set_table('film');
|
||||
$crud->set_relation_n_n('actors', 'film_actor', 'actor', 'film_id', 'actor_id', 'fullname','priority');
|
||||
$crud->set_relation_n_n('actors', 'film_actor', 'actor', 'film_id', 'actor_id', 'fullname', 'priority');
|
||||
$crud->set_relation_n_n('category', 'film_category', 'category', 'film_id', 'category_id', 'name');
|
||||
$crud->unset_columns('special_features','description','actors');
|
||||
$crud->unset_columns('special_features', 'description', 'actors');
|
||||
|
||||
$crud->fields('title', 'description', 'actors' , 'category' ,'release_year', 'rental_duration', 'rental_rate', 'length', 'replacement_cost', 'rating', 'special_features');
|
||||
$crud->fields('title', 'description', 'actors', 'category', 'release_year', 'rental_duration', 'rental_rate', 'length', 'replacement_cost', 'rating', 'special_features');
|
||||
|
||||
$output = $crud->render();
|
||||
|
||||
@@ -143,15 +143,14 @@ class Examples extends CI_Controller {
|
||||
|
||||
$crud->set_theme('twitter-bootstrap');
|
||||
$crud->set_table('film');
|
||||
$crud->set_relation_n_n('actors', 'film_actor', 'actor', 'film_id', 'actor_id', 'fullname','priority');
|
||||
$crud->set_relation_n_n('actors', 'film_actor', 'actor', 'film_id', 'actor_id', 'fullname', 'priority');
|
||||
$crud->set_relation_n_n('category', 'film_category', 'category', 'film_id', 'category_id', 'name');
|
||||
$crud->unset_columns('special_features','description','actors');
|
||||
$crud->unset_columns('special_features', 'description', 'actors');
|
||||
|
||||
$crud->fields('title', 'description', 'actors' , 'category' ,'release_year', 'rental_duration', 'rental_rate', 'length', 'replacement_cost', 'rating', 'special_features');
|
||||
$crud->fields('title', 'description', 'actors', 'category', 'release_year', 'rental_duration', 'rental_rate', 'length', 'replacement_cost', 'rating', 'special_features');
|
||||
|
||||
$output = $crud->render();
|
||||
$this->_example_output($output);
|
||||
|
||||
}catch(Exception $e){
|
||||
show_error($e->getMessage().' --- '.$e->getTraceAsString());
|
||||
}
|
||||
@@ -160,8 +159,8 @@ class Examples extends CI_Controller {
|
||||
function multigrids()
|
||||
{
|
||||
$this->config->load('grocery_crud');
|
||||
$this->config->set_item('grocery_crud_dialog_forms',true);
|
||||
$this->config->set_item('grocery_crud_default_per_page',10);
|
||||
$this->config->set_item('grocery_crud_dialog_forms', true);
|
||||
$this->config->set_item('grocery_crud_default_per_page', 10);
|
||||
|
||||
$output1 = $this->offices_management2();
|
||||
|
||||
@@ -186,7 +185,7 @@ class Examples extends CI_Controller {
|
||||
$crud->set_table('offices');
|
||||
$crud->set_subject('Office');
|
||||
|
||||
$crud->set_crud_url_path(site_url(strtolower(__CLASS__."/".__FUNCTION__)),site_url(strtolower(__CLASS__."/multigrids")));
|
||||
$crud->set_crud_url_path(site_url(strtolower(__CLASS__."/".__FUNCTION__)), site_url(strtolower(__CLASS__."/multigrids")));
|
||||
|
||||
$output = $crud->render();
|
||||
|
||||
@@ -203,15 +202,15 @@ class Examples extends CI_Controller {
|
||||
|
||||
$crud->set_theme('datatables');
|
||||
$crud->set_table('employees');
|
||||
$crud->set_relation('officeCode','offices','city');
|
||||
$crud->display_as('officeCode','Office City');
|
||||
$crud->set_relation('officeCode', 'offices', 'city');
|
||||
$crud->display_as('officeCode', 'Office City');
|
||||
$crud->set_subject('Employee');
|
||||
|
||||
$crud->required_fields('lastName');
|
||||
|
||||
$crud->set_field_upload('file_url','assets/uploads/files');
|
||||
$crud->set_field_upload('file_url', 'assets/uploads/files');
|
||||
|
||||
$crud->set_crud_url_path(site_url(strtolower(__CLASS__."/".__FUNCTION__)),site_url(strtolower(__CLASS__."/multigrids")));
|
||||
$crud->set_crud_url_path(site_url(strtolower(__CLASS__."/".__FUNCTION__)), site_url(strtolower(__CLASS__."/multigrids")));
|
||||
|
||||
$output = $crud->render();
|
||||
|
||||
@@ -227,14 +226,14 @@ class Examples extends CI_Controller {
|
||||
$crud = new grocery_CRUD();
|
||||
|
||||
$crud->set_table('customers');
|
||||
$crud->columns('customerName','contactLastName','phone','city','country','salesRepEmployeeNumber','creditLimit');
|
||||
$crud->display_as('salesRepEmployeeNumber','from Employeer')
|
||||
->display_as('customerName','Name')
|
||||
->display_as('contactLastName','Last Name');
|
||||
$crud->columns('customerName', 'contactLastName', 'phone', 'city', 'country', 'salesRepEmployeeNumber', 'creditLimit');
|
||||
$crud->display_as('salesRepEmployeeNumber', 'from Employeer')
|
||||
->display_as('customerName', 'Name')
|
||||
->display_as('contactLastName', 'Last Name');
|
||||
$crud->set_subject('Customer');
|
||||
$crud->set_relation('salesRepEmployeeNumber','employees','lastName');
|
||||
$crud->set_relation('salesRepEmployeeNumber', 'employees', 'lastName');
|
||||
|
||||
$crud->set_crud_url_path(site_url(strtolower(__CLASS__."/".__FUNCTION__)),site_url(strtolower(__CLASS__."/multigrids")));
|
||||
$crud->set_crud_url_path(site_url(strtolower(__CLASS__."/".__FUNCTION__)), site_url(strtolower(__CLASS__."/multigrids")));
|
||||
|
||||
$output = $crud->render();
|
||||
|
||||
@@ -244,5 +243,4 @@ class Examples extends CI_Controller {
|
||||
return $output;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,160 +0,0 @@
|
||||
<?php
|
||||
|
||||
class Migrate extends FHC_Controller
|
||||
{
|
||||
private $class_version = '1.0';
|
||||
private $cli = false;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
|
||||
if ($this->input->is_cli_request())
|
||||
{
|
||||
$cli=true;
|
||||
}
|
||||
else
|
||||
{
|
||||
//$this->output->set_status_header(403, 'Migrations must be run from the CLI');
|
||||
//exit;
|
||||
}
|
||||
$this->load->database('system'); //Use the system-Connection for DB-Manipulation
|
||||
$this->load->library('migration');
|
||||
$this->load->library('FHC_Seed');
|
||||
}
|
||||
|
||||
|
||||
public function help() {
|
||||
$result = "The following are the available command line interface commands\n\n";
|
||||
$result .= "php index.ci.php db migrate [\"version_number\"] Run all migrations. The version number is optional.\n";
|
||||
$result .= "php index.ci.php db seed \"file_name\" Run the specified seed file. Filename is optional.\n";
|
||||
|
||||
echo $result . PHP_EOL;
|
||||
}
|
||||
|
||||
/**
|
||||
* Migrate to latest or current version
|
||||
*
|
||||
* @param string $version One of either "latest" or "current"
|
||||
*/
|
||||
public function migrate($version = 'latest')
|
||||
{
|
||||
|
||||
if ($this->cli && $this->migration->current() === FALSE)
|
||||
{
|
||||
show_error($this->migration->error_string());
|
||||
}
|
||||
elseif ($version != 'latest' && $version != 'current')
|
||||
{
|
||||
$this->_failed('Migration version must be either latest or current');
|
||||
}
|
||||
|
||||
if (!$this->migration->version())
|
||||
{
|
||||
$this->_failed();
|
||||
}
|
||||
|
||||
$this->_succeeded();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Migrate to a specific version
|
||||
*/
|
||||
public function version()
|
||||
{
|
||||
if ($version == 'latest' || $version == 'current')
|
||||
{
|
||||
$this->index($version);
|
||||
exit;
|
||||
}
|
||||
|
||||
if (!$this->migrate->version($version))
|
||||
{
|
||||
$this->_failed();
|
||||
}
|
||||
|
||||
$this->_succeeded();
|
||||
}
|
||||
|
||||
/**
|
||||
* Roll-back to the last version before current
|
||||
*
|
||||
* @param int $version The migration to rollback to, defaults to previous
|
||||
*/
|
||||
public function rollback($version = null)
|
||||
{
|
||||
if (is_null($version))
|
||||
{
|
||||
$version = $this->_get_version() ?: 1;
|
||||
$version--;
|
||||
}
|
||||
|
||||
// Check it's definitely false, we could be rolling back to v0
|
||||
if (false === $this->migration->version($version))
|
||||
{
|
||||
$this->_failed();
|
||||
}
|
||||
|
||||
$this->_succeeded('rolled back');
|
||||
}
|
||||
|
||||
/**
|
||||
* ROLLBACK ALL THE THINGS!
|
||||
*/
|
||||
public function uninstall()
|
||||
{
|
||||
$this->rollback(0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Seed DB with TestData
|
||||
*
|
||||
* @param string $name Name of the SeedFile
|
||||
*/
|
||||
|
||||
public function seed($name = null)
|
||||
{
|
||||
$this->fhc_seed->seed($name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Yay, it worked! Tell the user.
|
||||
*
|
||||
* @param string $task What did we just do? We...
|
||||
*/
|
||||
private function _succeeded($task = 'migrated')
|
||||
{
|
||||
$version = $this->_get_version();
|
||||
exit('Successfully '.$task.' to version '.$version);
|
||||
}
|
||||
|
||||
/**
|
||||
* Output an error message when it all goes tits up
|
||||
*
|
||||
* @param string $message Error to output (default to CI's migration error)
|
||||
*/
|
||||
private function _failed($message = null)
|
||||
{
|
||||
$message = $message ?: $this->migration->error_string();
|
||||
show_error($message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Carbon copy of parent::_get_version, but that's protected.
|
||||
*
|
||||
* @return int Currently installed migration number
|
||||
*/
|
||||
private function _get_version()
|
||||
{
|
||||
$row = $this->db->get('ci_migrations')->row();
|
||||
return $row ? $row->version : 0;
|
||||
}
|
||||
|
||||
public function about()
|
||||
{
|
||||
echo "CI-Migrate_CLI v".$this->class_version;
|
||||
echo "\nCheck http://github.com/dshoreman/ci-migrate_cli/ for updates";
|
||||
exit;
|
||||
}
|
||||
}
|
||||
@@ -21,20 +21,25 @@
|
||||
* @return some value on success.
|
||||
*/
|
||||
|
||||
defined('BASEPATH') OR exit('No direct script access allowed');
|
||||
defined('BASEPATH') || exit('No direct script access allowed');
|
||||
|
||||
/**
|
||||
/**
|
||||
* @class Rest_server
|
||||
* @brief Rest Server Controller
|
||||
*
|
||||
* A more detailed class description.
|
||||
*/
|
||||
class Rest_server extends MY_Controller {
|
||||
|
||||
public function index()
|
||||
*/
|
||||
class Rest_server extends FHC_Controller
|
||||
{
|
||||
/**
|
||||
* Index Method for default function.
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$this->load->helper('url');
|
||||
|
||||
$this->load->view('rest_server');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
<?php
|
||||
defined('BASEPATH') OR exit('No direct script access allowed');
|
||||
defined('BASEPATH') || exit('No direct script access allowed');
|
||||
|
||||
class Vilesci extends CI_Controller {
|
||||
class Vilesci extends CI_Controller
|
||||
{
|
||||
|
||||
/**
|
||||
* Index Page for this controller.
|
||||
@@ -17,10 +18,12 @@ class Vilesci extends CI_Controller {
|
||||
* So any other public methods not prefixed with an underscore will
|
||||
* map to /index.php/welcome/<method_name>
|
||||
* @see http://codeigniter.com/user_guide/general/urls.html
|
||||
* @return void
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
if (false)//$this->dbupdate())
|
||||
// ToDo: check if update is needed
|
||||
if (false && $this->dbupdate())
|
||||
echo 'System-DB needs update!';
|
||||
else
|
||||
{
|
||||
@@ -30,11 +33,15 @@ class Vilesci extends CI_Controller {
|
||||
}
|
||||
}
|
||||
|
||||
private function dbupdate()
|
||||
/**
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function __dbupdate()
|
||||
{
|
||||
// Check for update (codeigniter migration)
|
||||
$this->load->library('migration');
|
||||
if ($this->migration->current() === FALSE)
|
||||
if ($this->migration->current() === false)
|
||||
show_error($this->migration->error_string());
|
||||
if ($this->migration->current() != $this->migration->latest())
|
||||
return true;
|
||||
|
||||
+14
-12
@@ -14,22 +14,23 @@
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
|
||||
defined('BASEPATH') OR exit('No direct script access allowed');
|
||||
if (! defined('BASEPATH'))
|
||||
exit('No direct script access allowed');
|
||||
|
||||
// This can be removed if you use __autoload() in config.php OR use Modular Extensions
|
||||
require APPPATH . '/libraries/REST_Controller.php';
|
||||
//require APPPATH . '/libraries/REST_Controller.php';
|
||||
|
||||
/**
|
||||
* Handles user authentication and registration process
|
||||
*/
|
||||
class AuthAPI extends REST_Controller {
|
||||
|
||||
class AuthAPI extends APIv1_Controller
|
||||
{
|
||||
/**
|
||||
* Userauth-Controller constructor.
|
||||
* A more elaborate description of the constructor.
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
function __construct()
|
||||
public function __construct()
|
||||
{
|
||||
// Construct the parent class
|
||||
parent::__construct();
|
||||
@@ -39,16 +40,18 @@ class AuthAPI extends REST_Controller {
|
||||
$this->methods['login_get']['limit'] = 500; // 500 requests per hour per user/key
|
||||
|
||||
// Load helper
|
||||
$this->load->helper('fhcauth');
|
||||
//$this->load->helper('fhcauth');
|
||||
$this->load->library('session');
|
||||
$this->load->library('FHC_Auth');
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks user credentials and creates a new session
|
||||
* @return string JSON that indicates success/failure of login
|
||||
*
|
||||
* @example normal account: http://wsp.fortyseeds.at/backend/api/userauth/login/username/foo%40bar.at/password/secret/device_id/abcdef123
|
||||
* @example OAuth Google: http://wsp.fortyseeds.at/backend/api/userauth/login/username/foo%40bar.at/device_id/abcdef123/google_token/qwert321
|
||||
* @example OAuth Facebook: http://wsp.fortyseeds.at/backend/api/userauth/login/username/foo%40bar.at/device_id/abcdef123/fb_token/qwert321
|
||||
* @return void JSON that indicates success/failure of login.
|
||||
*/
|
||||
public function login_get()
|
||||
{
|
||||
@@ -57,8 +60,8 @@ class AuthAPI extends REST_Controller {
|
||||
$httpstatus = null;
|
||||
$username = urldecode($this->get('username'));
|
||||
$password = urldecode($this->get('password'));
|
||||
|
||||
$account = auth($username,$password);
|
||||
|
||||
$account = $this->FHCAuth->auth($username, $password);
|
||||
|
||||
// perform login checks
|
||||
if (!$account)
|
||||
@@ -92,8 +95,9 @@ class AuthAPI extends REST_Controller {
|
||||
|
||||
/**
|
||||
* Logs out user by destroying session
|
||||
* @return string JSON that indicates success/failure of logout
|
||||
*
|
||||
* @example http://wsp.fortyseeds.at/backend/api/userauth/logout/username/foo%40bar.at/session_id/55afab8ba6f1b/device_id/abcdef123
|
||||
* @return void JSON that indicates success/failure of logout
|
||||
*/
|
||||
public function logout_get()
|
||||
{
|
||||
@@ -125,6 +129,4 @@ class AuthAPI extends REST_Controller {
|
||||
// Set the response and exit
|
||||
$this->response($payload, $httpstatus);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,9 +1,10 @@
|
||||
<?php
|
||||
|
||||
defined('BASEPATH') OR exit('No direct script access allowed');
|
||||
if (! defined('BASEPATH'))
|
||||
exit('No direct script access allowed');
|
||||
|
||||
// This can be removed if you use __autoload() in config.php OR use Modular Extensions
|
||||
require APPPATH . '/libraries/REST_Controller.php';
|
||||
require APPPATH.'/libraries/REST_Controller.php';
|
||||
|
||||
/**
|
||||
* Keys Controller
|
||||
@@ -16,8 +17,8 @@ require APPPATH . '/libraries/REST_Controller.php';
|
||||
* @license MIT
|
||||
* @link https://github.com/chriskacerguis/codeigniter-restserver
|
||||
*/
|
||||
class Key extends REST_Controller {
|
||||
|
||||
class APIKey extends REST_Controller
|
||||
{
|
||||
protected $methods = [
|
||||
'index_put' => ['level' => 10, 'limit' => 10],
|
||||
'index_delete' => ['level' => 10],
|
||||
@@ -34,24 +35,24 @@ class Key extends REST_Controller {
|
||||
public function index_put()
|
||||
{
|
||||
// Build a new key
|
||||
$key = $this->_generate_key();
|
||||
$key = $this->__generateKey();
|
||||
|
||||
// If no key level provided, provide a generic key
|
||||
$level = $this->put('level') ? $this->put('level') : 1;
|
||||
$ignore_limits = ctype_digit($this->put('ignore_limits')) ? (int) $this->put('ignore_limits') : 1;
|
||||
$ignore_limits = ctype_digit($this->put('ignore_limits')) ? (int)$this->put('ignore_limits') : 1;
|
||||
|
||||
// Insert the new key
|
||||
if ($this->_insert_key($key, ['level' => $level, 'ignore_limits' => $ignore_limits]))
|
||||
{
|
||||
$this->response([
|
||||
'status' => TRUE,
|
||||
'status' => true,
|
||||
'key' => $key
|
||||
], REST_Controller::HTTP_CREATED); // CREATED (201) being the HTTP response code
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->response([
|
||||
'status' => FALSE,
|
||||
'status' => false,
|
||||
'message' => 'Could not save the key'
|
||||
], REST_Controller::HTTP_INTERNAL_SERVER_ERROR); // INTERNAL_SERVER_ERROR (500) being the HTTP response code
|
||||
}
|
||||
@@ -72,7 +73,7 @@ class Key extends REST_Controller {
|
||||
{
|
||||
// It doesn't appear the key exists
|
||||
$this->response([
|
||||
'status' => FALSE,
|
||||
'status' => false,
|
||||
'message' => 'Invalid API key'
|
||||
], REST_Controller::HTTP_BAD_REQUEST); // BAD_REQUEST (400) being the HTTP response code
|
||||
}
|
||||
@@ -82,7 +83,7 @@ class Key extends REST_Controller {
|
||||
|
||||
// Respond that the key was destroyed
|
||||
$this->response([
|
||||
'status' => TRUE,
|
||||
'status' => true,
|
||||
'message' => 'API key was deleted'
|
||||
], REST_Controller::HTTP_NO_CONTENT); // NO_CONTENT (204) being the HTTP response code
|
||||
}
|
||||
@@ -103,7 +104,7 @@ class Key extends REST_Controller {
|
||||
{
|
||||
// It doesn't appear the key exists
|
||||
$this->response([
|
||||
'status' => FALSE,
|
||||
'status' => false,
|
||||
'message' => 'Invalid API key'
|
||||
], REST_Controller::HTTP_BAD_REQUEST); // BAD_REQUEST (400) being the HTTP response code
|
||||
}
|
||||
@@ -112,14 +113,14 @@ class Key extends REST_Controller {
|
||||
if ($this->_update_key($key, ['level' => $new_level]))
|
||||
{
|
||||
$this->response([
|
||||
'status' => TRUE,
|
||||
'status' => true,
|
||||
'message' => 'API key was updated'
|
||||
], REST_Controller::HTTP_OK); // OK (200) being the HTTP response code
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->response([
|
||||
'status' => FALSE,
|
||||
'status' => false,
|
||||
'message' => 'Could not update the key level'
|
||||
], REST_Controller::HTTP_INTERNAL_SERVER_ERROR); // INTERNAL_SERVER_ERROR (500) being the HTTP response code
|
||||
}
|
||||
@@ -140,7 +141,7 @@ class Key extends REST_Controller {
|
||||
{
|
||||
// It doesn't appear the key exists
|
||||
$this->response([
|
||||
'status' => FALSE,
|
||||
'status' => false,
|
||||
'message' => 'Invalid API key'
|
||||
], REST_Controller::HTTP_BAD_REQUEST); // BAD_REQUEST (400) being the HTTP response code
|
||||
}
|
||||
@@ -149,14 +150,14 @@ class Key extends REST_Controller {
|
||||
if ($this->_update_key($key, ['level' => 0]))
|
||||
{
|
||||
$this->response([
|
||||
'status' => TRUE,
|
||||
'status' => true,
|
||||
'message' => 'Key was suspended'
|
||||
], REST_Controller::HTTP_OK); // OK (200) being the HTTP response code
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->response([
|
||||
'status' => FALSE,
|
||||
'status' => false,
|
||||
'message' => 'Could not suspend the user'
|
||||
], REST_Controller::HTTP_INTERNAL_SERVER_ERROR); // INTERNAL_SERVER_ERROR (500) being the HTTP response code
|
||||
}
|
||||
@@ -171,20 +172,20 @@ class Key extends REST_Controller {
|
||||
public function regenerate_post()
|
||||
{
|
||||
$old_key = $this->post('key');
|
||||
$key_details = $this->_get_key($old_key);
|
||||
$key_details = $this->__getKey($old_key);
|
||||
|
||||
// Does this key exist?
|
||||
if (!$key_details)
|
||||
{
|
||||
// It doesn't appear the key exists
|
||||
$this->response([
|
||||
'status' => FALSE,
|
||||
'status' => false,
|
||||
'message' => 'Invalid API key'
|
||||
], REST_Controller::HTTP_BAD_REQUEST); // BAD_REQUEST (400) being the HTTP response code
|
||||
}
|
||||
|
||||
// Build a new key
|
||||
$new_key = $this->_generate_key();
|
||||
$new_key = $this->__generateKey();
|
||||
|
||||
// Insert the new key
|
||||
if ($this->_insert_key($new_key, ['level' => $key_details->level, 'ignore_limits' => $key_details->ignore_limits]))
|
||||
@@ -193,14 +194,14 @@ class Key extends REST_Controller {
|
||||
$this->_update_key($old_key, ['level' => 0]);
|
||||
|
||||
$this->response([
|
||||
'status' => TRUE,
|
||||
'status' => true,
|
||||
'key' => $new_key
|
||||
], REST_Controller::HTTP_CREATED); // CREATED (201) being the HTTP response code
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->response([
|
||||
'status' => FALSE,
|
||||
'status' => false,
|
||||
'message' => 'Could not save the key'
|
||||
], REST_Controller::HTTP_INTERNAL_SERVER_ERROR); // INTERNAL_SERVER_ERROR (500) being the HTTP response code
|
||||
}
|
||||
@@ -208,7 +209,13 @@ class Key extends REST_Controller {
|
||||
|
||||
/* Helper Methods */
|
||||
|
||||
private function _generate_key()
|
||||
/**
|
||||
* Generate a key
|
||||
*
|
||||
* @access private
|
||||
* @return void
|
||||
*/
|
||||
private function __generateKey()
|
||||
{
|
||||
do
|
||||
{
|
||||
@@ -216,9 +223,9 @@ class Key extends REST_Controller {
|
||||
$salt = base_convert(bin2hex($this->security->get_random_bytes(64)), 16, 36);
|
||||
|
||||
// If an error occurred, then fall back to the previous method
|
||||
if ($salt === FALSE)
|
||||
if ($salt === false)
|
||||
{
|
||||
$salt = hash('sha256', time() . mt_rand());
|
||||
$salt = hash('sha256', time().mt_rand());
|
||||
}
|
||||
|
||||
$new_key = substr($salt, 0, config_item('rest_key_length'));
|
||||
@@ -230,7 +237,14 @@ class Key extends REST_Controller {
|
||||
|
||||
/* Private Data Methods */
|
||||
|
||||
private function _get_key($key)
|
||||
/**
|
||||
* Get a key
|
||||
*
|
||||
* @access private
|
||||
* @param string $key The API-Key.
|
||||
* @return array
|
||||
*/
|
||||
private function __getKey($key)
|
||||
{
|
||||
return $this->db
|
||||
->where(config_item('rest_key_column'), $key)
|
||||
@@ -238,6 +252,13 @@ class Key extends REST_Controller {
|
||||
->row();
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if key exists
|
||||
*
|
||||
* @access private
|
||||
* @param string $key The API-Key.
|
||||
* @return bool
|
||||
*/
|
||||
private function _key_exists($key)
|
||||
{
|
||||
return $this->db
|
||||
@@ -245,6 +266,14 @@ class Key extends REST_Controller {
|
||||
->count_all_results(config_item('rest_keys_table')) > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert a key
|
||||
*
|
||||
* @access private
|
||||
* @param string $key The API-Key.
|
||||
* @param array $data The API-Key-Data.
|
||||
* @return bool
|
||||
*/
|
||||
private function _insert_key($key, $data)
|
||||
{
|
||||
$data[config_item('rest_key_column')] = $key;
|
||||
@@ -255,6 +284,14 @@ class Key extends REST_Controller {
|
||||
->insert(config_item('rest_keys_table'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a key
|
||||
*
|
||||
* @access private
|
||||
* @param string $key The API-Key.
|
||||
* @param array $data The API-Key-Data.
|
||||
* @return bool
|
||||
*/
|
||||
private function _update_key($key, $data)
|
||||
{
|
||||
return $this->db
|
||||
@@ -262,6 +299,13 @@ class Key extends REST_Controller {
|
||||
->update(config_item('rest_keys_table'), $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a key
|
||||
*
|
||||
* @access private
|
||||
* @param string $key The API-Key.
|
||||
* @return bool
|
||||
*/
|
||||
private function _delete_key($key)
|
||||
{
|
||||
return $this->db
|
||||
@@ -11,17 +11,18 @@
|
||||
* @license MIT
|
||||
* @link https://github.com/chriskacerguis/codeigniter-restserver
|
||||
*/
|
||||
defined('BASEPATH') OR exit('No direct script access allowed');
|
||||
if (! defined('BASEPATH'))
|
||||
exit('No direct script access allowed');
|
||||
|
||||
// This can be removed if you use __autoload() in config.php OR use Modular Extensions
|
||||
require APPPATH . '/libraries/REST_Controller.php';
|
||||
require APPPATH.'/libraries/REST_Controller.php';
|
||||
|
||||
class Example extends REST_Controller {
|
||||
|
||||
/**
|
||||
* @copydoc REST_Controller::__construct()
|
||||
*/
|
||||
function __construct()
|
||||
class Example extends REST_Controller
|
||||
{
|
||||
/**
|
||||
* @copydoc REST_Controller::__construct()
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
// Construct the parent class
|
||||
parent::__construct();
|
||||
@@ -33,6 +34,9 @@ class Example extends REST_Controller {
|
||||
$this->methods['user_delete']['limit'] = 50; // 50 requests per hour per user/key
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function users_get()
|
||||
{
|
||||
// Users from a data store e.g. database
|
||||
@@ -46,9 +50,9 @@ class Example extends REST_Controller {
|
||||
|
||||
// If the id parameter doesn't exist return all the users
|
||||
|
||||
if ($id === NULL)
|
||||
if ($id === null)
|
||||
{
|
||||
// Check if the users data store contains users (in case the database result returns NULL)
|
||||
// Check if the users data store contains users (in case the database result returns null)
|
||||
if ($users)
|
||||
{
|
||||
// Set the response and exit
|
||||
@@ -58,7 +62,7 @@ class Example extends REST_Controller {
|
||||
{
|
||||
// Set the response and exit
|
||||
$this->response([
|
||||
'status' => FALSE,
|
||||
'status' => false,
|
||||
'message' => 'No users were found'
|
||||
], REST_Controller::HTTP_NOT_FOUND); // NOT_FOUND (404) being the HTTP response code
|
||||
}
|
||||
@@ -66,19 +70,19 @@ class Example extends REST_Controller {
|
||||
|
||||
// Find and return a single record for a particular user.
|
||||
|
||||
$id = (int) $id;
|
||||
$id = (int)$id;
|
||||
|
||||
// Validate the id.
|
||||
if ($id <= 0)
|
||||
{
|
||||
// Invalid id, set the response and exit.
|
||||
$this->response(NULL, REST_Controller::HTTP_BAD_REQUEST); // BAD_REQUEST (400) being the HTTP response code
|
||||
$this->response(null, REST_Controller::HTTP_BAD_REQUEST); // BAD_REQUEST (400) being the HTTP response code
|
||||
}
|
||||
|
||||
// Get the user from the array, using the id as key for retreival.
|
||||
// Usually a model is to be used for this.
|
||||
|
||||
$user = NULL;
|
||||
$user = null;
|
||||
|
||||
if (!empty($users))
|
||||
{
|
||||
@@ -98,12 +102,15 @@ class Example extends REST_Controller {
|
||||
else
|
||||
{
|
||||
$this->set_response([
|
||||
'status' => FALSE,
|
||||
'status' => false,
|
||||
'message' => 'User could not be found'
|
||||
], REST_Controller::HTTP_NOT_FOUND); // NOT_FOUND (404) being the HTTP response code
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function users_post()
|
||||
{
|
||||
// $this->some_model->update_user( ... );
|
||||
@@ -117,15 +124,18 @@ class Example extends REST_Controller {
|
||||
$this->set_response($message, REST_Controller::HTTP_CREATED); // CREATED (201) being the HTTP response code
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function users_delete()
|
||||
{
|
||||
$id = (int) $this->get('id');
|
||||
$id = (int)$this->get('id');
|
||||
|
||||
// Validate the id.
|
||||
if ($id <= 0)
|
||||
{
|
||||
// Set the response and exit
|
||||
$this->response(NULL, REST_Controller::HTTP_BAD_REQUEST); // BAD_REQUEST (400) being the HTTP response code
|
||||
$this->response(null, REST_Controller::HTTP_BAD_REQUEST); // BAD_REQUEST (400) being the HTTP response code
|
||||
}
|
||||
|
||||
// $this->some_model->delete_something($id);
|
||||
@@ -134,7 +144,7 @@ class Example extends REST_Controller {
|
||||
'message' => 'Deleted the resource'
|
||||
];
|
||||
|
||||
$this->set_response($message, REST_Controller::HTTP_NO_CONTENT); // NO_CONTENT (204) being the HTTP response code
|
||||
// NO_CONTENT (204) being the HTTP response code
|
||||
$this->set_response($message, REST_Controller::HTTP_NO_CONTENT);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,188 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* FH-Complete
|
||||
*
|
||||
* @package FHC-API
|
||||
* @author FHC-Team
|
||||
* @copyright Copyright (c) 2016, fhcomplete.org
|
||||
* @license GPLv3
|
||||
* @link http://fhcomplete.org
|
||||
* @since Version 1.0
|
||||
* @filesource
|
||||
*/
|
||||
// ------------------------------------------------------------------------
|
||||
|
||||
if (! defined('BASEPATH'))
|
||||
exit('No direct script access allowed');
|
||||
|
||||
class Person extends APIv1_Controller
|
||||
{
|
||||
/**
|
||||
* Person API constructor.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
$this->load->model('person/person_model');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function person_get()
|
||||
{
|
||||
//if(!$this->session_model->validate($this->get('session_id'), $this->get('device_id')))
|
||||
// $this->response(array(['success' => false, 'message' => 'access denied']), REST_Controller::HTTP_UNAUTHORIZED);
|
||||
|
||||
$code = $this->get('code');
|
||||
$person_id = $this->get('person_id');
|
||||
|
||||
if (! is_null($code))
|
||||
{
|
||||
$result = $this->person_model->getPersonByCode($code);
|
||||
}
|
||||
elseif (! is_null($person_id))
|
||||
{
|
||||
$result = $this->person_model->getPerson($person_id);
|
||||
}
|
||||
else
|
||||
{
|
||||
$result = $this->person_model->getPerson();
|
||||
}
|
||||
|
||||
if ($result['err'])
|
||||
{
|
||||
$payload = [
|
||||
'success' => false,
|
||||
'message' => $result['msg'].': '.$result['retval']
|
||||
];
|
||||
$httpstatus = REST_Controller::HTTP_OK;
|
||||
}
|
||||
else
|
||||
{
|
||||
// return all available persons
|
||||
$payload = [
|
||||
'success' => true,
|
||||
'message' => 'Persons found'
|
||||
];
|
||||
$payload['data'] = $result;
|
||||
$httpstatus = REST_Controller::HTTP_OK;
|
||||
}
|
||||
|
||||
if (empty($result))
|
||||
{
|
||||
$payload = [
|
||||
'success' => false,
|
||||
'message' => 'Person not found'
|
||||
];
|
||||
$httpstatus = REST_Controller::HTTP_OK;
|
||||
}
|
||||
else
|
||||
{
|
||||
// return all available persons
|
||||
$payload = [
|
||||
'success' => true,
|
||||
'message' => 'Persons found'
|
||||
];
|
||||
$payload['data'] = $result;
|
||||
$httpstatus = REST_Controller::HTTP_OK;
|
||||
}
|
||||
// Set the response and exit
|
||||
$this->response($payload, $httpstatus);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function person_post()
|
||||
{
|
||||
$result = $this->person_model->savePerson($this->post());
|
||||
if ($result != false)
|
||||
{
|
||||
$httpstatus = REST_Controller::HTTP_OK;
|
||||
$payload = [
|
||||
'success' => true,
|
||||
'message' => 'Person saved.'
|
||||
];
|
||||
$payload['data'] = $result;
|
||||
}
|
||||
else
|
||||
{
|
||||
$payload = [
|
||||
'success' => false,
|
||||
'message' => 'Could not save person.'
|
||||
];
|
||||
$httpstatus = REST_Controller::HTTP_OK;
|
||||
}
|
||||
$this->response($payload, $httpstatus);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function personUpdate_post()
|
||||
{
|
||||
$result = $this->person_model->updatePerson($this->post());
|
||||
if ($result != false)
|
||||
{
|
||||
$httpstatus = REST_Controller::HTTP_OK;
|
||||
$payload = [
|
||||
'success' => true,
|
||||
'message' => 'Person updated.'
|
||||
];
|
||||
$payload['data'] = $result;
|
||||
}
|
||||
else
|
||||
{
|
||||
$payload = [
|
||||
'success' => false,
|
||||
'message' => 'Could not update person.'
|
||||
];
|
||||
$httpstatus = REST_Controller::HTTP_OK;
|
||||
}
|
||||
$this->response($payload, $httpstatus);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function checkBewerbung_get()
|
||||
{
|
||||
$result = $this->person_model->checkBewerbung($this->get("email"), $this->get("studiensemester_kurzbz"));
|
||||
$httpstatus = REST_Controller::HTTP_OK;
|
||||
$payload = [
|
||||
'success' => true,
|
||||
'message' => 'Bewerbung exists.'
|
||||
];
|
||||
$payload['data'] = $result;
|
||||
$this->response($payload, $httpstatus);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function checkZugangscodePerson_get()
|
||||
{
|
||||
$result = $this->person_model->checkZugangscodePerson($this->get("code"));
|
||||
$httpstatus = REST_Controller::HTTP_OK;
|
||||
if (!empty($result))
|
||||
{
|
||||
$payload = [
|
||||
'success' => true,
|
||||
'message' => 'Zugangscode exists.'
|
||||
];
|
||||
$payload['data'] = $result;
|
||||
}
|
||||
else
|
||||
{
|
||||
$payload = [
|
||||
'success' => false,
|
||||
'message' => 'Zugangscode does not exist.'
|
||||
];
|
||||
$httpstatus = REST_Controller::HTTP_OK;
|
||||
}
|
||||
|
||||
$this->response($payload, $httpstatus);
|
||||
}
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
<?php
|
||||
defined('BASEPATH') OR exit('No direct script access allowed');
|
||||
|
||||
// This can be removed if you use __autoload() in config.php OR use Modular Extensions
|
||||
//require APPPATH . '/libraries/REST_Controller.php';
|
||||
|
||||
class API_Controller extends REST_Controller
|
||||
{
|
||||
function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
//$this->load->library('session'); -> autoload
|
||||
//$this->load->library('database'); -> autoload
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -12,7 +12,7 @@ class FHC_Controller extends CI_Controller
|
||||
|
||||
require_once APPPATH . '/libraries/REST_Controller.php';
|
||||
|
||||
class API_Controller extends REST_Controller
|
||||
class APIv1_Controller extends REST_Controller
|
||||
{
|
||||
function __construct()
|
||||
{
|
||||
|
||||
@@ -3,6 +3,7 @@ defined('BASEPATH') OR exit('No direct script access allowed');
|
||||
|
||||
class FHC_Model extends CI_Model
|
||||
{
|
||||
//protected errormsg;
|
||||
function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
@@ -31,12 +32,13 @@ class FHC_Model extends CI_Model
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function _general_error()
|
||||
protected function _general_error($retval = '', $message = FHC_ERR_GENERAL)
|
||||
{
|
||||
return array(
|
||||
'err' => 1,
|
||||
'code' => FHC_ERR_GENERAL,
|
||||
'msg' => lang('fhc_'.FHC_ERR_GENERAL)
|
||||
'msg' => lang('fhc_'.$message),
|
||||
'retval' => $retval
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,430 @@
|
||||
<?php
|
||||
/**
|
||||
* FH-Complete
|
||||
*
|
||||
* @package FHC-Helper
|
||||
* @author FHC-Team
|
||||
* @copyright Copyright (c) 2016 fhcomplete.org
|
||||
* @license GPLv3
|
||||
* @link https://fhcomplete.org
|
||||
* @since Version 1.0.0
|
||||
* @filesource
|
||||
*/
|
||||
defined('BASEPATH') OR exit('No direct script access allowed');
|
||||
//require_once 'include/basis.class.php';
|
||||
|
||||
/**
|
||||
* FHC-Auth Helpers
|
||||
*
|
||||
* @package FH-Complete
|
||||
* @subpackage Helpers
|
||||
* @category Helpers
|
||||
* @author FHC-Team
|
||||
* @link http://fhcomplete.org/user_guide/helpers/fhcauth_helper.html
|
||||
*/
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
|
||||
//require_once('include/sprache.class.php');
|
||||
|
||||
class basis_db
|
||||
{
|
||||
protected $ci=null;
|
||||
protected $db_result=null;
|
||||
protected $errormsg=null;
|
||||
|
||||
public function __construct($ci)
|
||||
{
|
||||
$this->ci=$ci;
|
||||
}
|
||||
|
||||
public function db_connect()
|
||||
{
|
||||
$conn_str='host='.DB_HOST.' port='.DB_PORT.' dbname='.DB_NAME.' user='.DB_USER.' password='.DB_PASSWORD;
|
||||
//Connection Herstellen
|
||||
if (DB_CONNECT_PERSISTENT)
|
||||
{
|
||||
if(!basis_db::$db_conn = pg_pconnect($conn_str))
|
||||
die('Fehler beim Oeffnen der Datenbankverbindung');
|
||||
}
|
||||
else
|
||||
{
|
||||
if(!basis_db::$db_conn = pg_connect($conn_str))
|
||||
die('Fehler beim Oeffnen der Datenbankverbindung');
|
||||
}
|
||||
}
|
||||
|
||||
public function db_query($sql)
|
||||
{
|
||||
if ($this->db_result=$this->ci->db->simple_query($sql))
|
||||
return $this->db_result;
|
||||
else
|
||||
{
|
||||
$this->errormsg.='Abfrage in Datenbank fehlgeschlagen! '.$this->db_last_error();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public function db_num_rows($result=null)
|
||||
{
|
||||
if(is_null($result))
|
||||
return pg_num_rows($this->db_result);
|
||||
else
|
||||
return pg_num_rows($result);
|
||||
}
|
||||
|
||||
public function db_fetch_object($result = null, $i=null)
|
||||
{
|
||||
if(is_null($result))
|
||||
{
|
||||
if(is_null($i))
|
||||
return pg_fetch_object($this->db_result);
|
||||
else
|
||||
return pg_fetch_object($this->db_result, $i);
|
||||
}
|
||||
else
|
||||
{
|
||||
if(is_null($i))
|
||||
return pg_fetch_object($result);
|
||||
else
|
||||
return pg_fetch_object($result, $i);
|
||||
}
|
||||
}
|
||||
|
||||
public function db_fetch_row($result = null, $i=null)
|
||||
{
|
||||
if(is_null($result))
|
||||
{
|
||||
if(is_null($i))
|
||||
return pg_fetch_row($this->db_result);
|
||||
else
|
||||
return pg_fetch_row($this->db_result, $i);
|
||||
}
|
||||
else
|
||||
{
|
||||
if(is_null($i))
|
||||
return pg_fetch_row($result);
|
||||
else
|
||||
return pg_fetch_row($result, $i);
|
||||
}
|
||||
}
|
||||
|
||||
public function db_fetch_assoc($result = null, $i=null)
|
||||
{
|
||||
if(is_null($result))
|
||||
{
|
||||
if(is_null($i))
|
||||
return pg_fetch_assoc($this->db_result);
|
||||
else
|
||||
return pg_fetch_assoc($this->db_result, $i);
|
||||
}
|
||||
else
|
||||
{
|
||||
if(is_null($i))
|
||||
return pg_fetch_row($result);
|
||||
else
|
||||
return pg_fetch_row($result, $i);
|
||||
}
|
||||
}
|
||||
|
||||
public function db_result($result = null, $i,$item)
|
||||
{
|
||||
if(is_null($result))
|
||||
{
|
||||
return pg_result($this->db_result, $i,$item);
|
||||
}
|
||||
else
|
||||
{
|
||||
return pg_result($result, $i,$item);
|
||||
}
|
||||
}
|
||||
|
||||
public function db_getResultJSON($result = null)
|
||||
{
|
||||
$rows=array();
|
||||
if(is_null($result))
|
||||
{
|
||||
while($r = pg_fetch_assoc($this->db_result))
|
||||
$rows[] = $r;
|
||||
|
||||
//print json_encode($rows);
|
||||
}
|
||||
else
|
||||
{
|
||||
pg_result_seek($result, 0);
|
||||
//var_dump($result);
|
||||
while($r = pg_fetch_assoc($result))
|
||||
{
|
||||
$rows[] = $r;
|
||||
}
|
||||
|
||||
//print json_encode($rows);
|
||||
}
|
||||
return json_encode($rows);
|
||||
}
|
||||
|
||||
public function db_last_error()
|
||||
{
|
||||
return pg_last_error();
|
||||
}
|
||||
|
||||
public function db_affected_rows($result=null)
|
||||
{
|
||||
if(is_null($result))
|
||||
return pg_affected_rows($this->db_result);
|
||||
else
|
||||
return pg_affected_rows($result);
|
||||
}
|
||||
|
||||
public function db_result_seek($result=null, $offset)
|
||||
{
|
||||
if(is_null($result))
|
||||
return pg_result_seek($this->db_result, $offset);
|
||||
else
|
||||
return pg_result_seek($result, $offset);
|
||||
}
|
||||
|
||||
public function db_fetch_array($result=null)
|
||||
{
|
||||
if(is_null($result))
|
||||
return pg_fetch_array($this->db_result);
|
||||
else
|
||||
return pg_fetch_array($result);
|
||||
}
|
||||
|
||||
public function db_num_fields($result=null)
|
||||
{
|
||||
if(is_null($result))
|
||||
return pg_num_fields($this->db_result);
|
||||
else
|
||||
return pg_num_fields($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Liefert den Feldnamen mit index i
|
||||
*/
|
||||
public function db_field_name($result=null, $i)
|
||||
{
|
||||
if(is_null($result))
|
||||
return pg_field_name($this->db_result, $i);
|
||||
else
|
||||
return pg_field_name($result, $i);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gibt den Speicher wieder Frei.
|
||||
* (ist das sinnvoll wenn es per Value uebergeben wird??)
|
||||
*/
|
||||
public function db_free_result($result = null)
|
||||
{
|
||||
if(is_null($result))
|
||||
{
|
||||
return pg_free_result($this->db_result);
|
||||
}
|
||||
else
|
||||
{
|
||||
return pg_free_result($result);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Liefert die aktuelle Datenbankversion
|
||||
*/
|
||||
public function db_version()
|
||||
{
|
||||
return pg_version(basis_db::$db_conn);
|
||||
}
|
||||
|
||||
/**
|
||||
* Escaped Sonderzeichen in Variablen vor der Verwendung in SQL Statements
|
||||
* um SQL Injections zu verhindern
|
||||
*
|
||||
*/
|
||||
public function db_escape($var)
|
||||
{
|
||||
return pg_escape_string($var);
|
||||
}
|
||||
|
||||
/**
|
||||
* Null Value Handling und Hochkomma für Inserts / Updates
|
||||
* Wenn die Uebergebe Variable leer ist, wird ein String mit null
|
||||
* zurueckgeliefert, wenn nicht dann wird der string unter Hochkomma zurueckgeliefert
|
||||
* es sei denn qoute=false dann wird nur der String zurueckgeliefert
|
||||
*
|
||||
* @param $var String-Value fuer SQL Request
|
||||
* @return string
|
||||
*/
|
||||
public function db_null_value($var, $qoute=true)
|
||||
{
|
||||
if($qoute)
|
||||
return ($var!==''?$this->db_qoute($var):'null');
|
||||
else
|
||||
return ($var!==''?$var:'null');
|
||||
}
|
||||
|
||||
/**
|
||||
* Setzt einen String unter Hochkomma
|
||||
* @param $var Value fuer Insert/Update
|
||||
* @return value unter Hochkomma
|
||||
*/
|
||||
public function db_qoute($var)
|
||||
{
|
||||
return "'".$var."'";
|
||||
}
|
||||
|
||||
/**
|
||||
* Escaped einen Parameter fuer die Verwendung in Insert/Update SQL Befehlen
|
||||
* Es werden abhaengig vom Typ Hochkomma oder Null hinzugefuegt
|
||||
* @param $var Value der gesetzt werden soll
|
||||
* @param $type Typ des Values (FHC_STRING | FHC_BOOLEAN | FHC_INTEGER | ...)
|
||||
* @param $nullable boolean gibt an ob das Feld NULL sein darf. Wenn true wird
|
||||
* NULL statt einem Leerstring zurueckgeliefert
|
||||
* @return Escapter Value inklusive Hochkomma wenn noetig
|
||||
*
|
||||
* Verwendungsbeispiel:
|
||||
* Update tbl_person set nachname=$this->db_add_param($var)
|
||||
* Update tbl_person set aktiv=$this->db_add_param($var, FHC_BOOL, false)
|
||||
* Update tbl_person set anzahlkinder=$this->db_add_param($var, FHC_INT)
|
||||
*/
|
||||
public function db_add_param($var, $type=FHC_STRING, $nullable=true)
|
||||
{
|
||||
if(($var==='' || is_null($var)) && $type!=FHC_BOOLEAN)
|
||||
{
|
||||
if($nullable)
|
||||
return 'null';
|
||||
else
|
||||
return "''";
|
||||
}
|
||||
|
||||
switch($type)
|
||||
{
|
||||
case FHC_INTEGER:
|
||||
$var = $this->db_escape($var);
|
||||
if(!is_numeric($var) && $var!=='')
|
||||
die('Invalid Integer Parameter detected:'.$var);
|
||||
$var = $this->db_null_value($var, false);
|
||||
break;
|
||||
|
||||
case FHC_LANG_ARRAY:
|
||||
|
||||
$sprache = new sprache();
|
||||
$sprache->getAll(true);
|
||||
$buf = $var;
|
||||
$var = array();
|
||||
$languages = $sprache->getAllIndexesSorted();
|
||||
|
||||
foreach($languages as $sk => $sp)
|
||||
{
|
||||
if(!$sp || !isset($buf[$sp]))
|
||||
$var[$sk] = "";
|
||||
else
|
||||
$var[$sk] = $this->db_escape($buf[$sp]);
|
||||
}
|
||||
$var = str_replace('\\', '\\\\', $var);
|
||||
$var = str_replace('"', '\\\"', $var);
|
||||
$var = '\'{"' . join('","', $var) . '"}\'';
|
||||
|
||||
break;
|
||||
|
||||
case FHC_BOOLEAN:
|
||||
if($var===true)
|
||||
$var='true';
|
||||
elseif($var===false)
|
||||
$var='false';
|
||||
elseif($var=='' && $nullable)
|
||||
$var = 'null';
|
||||
else
|
||||
die('Invalid Boolean Parameter detected');
|
||||
break;
|
||||
|
||||
case FHC_STRING:
|
||||
default:
|
||||
$var = $this->db_escape($var);
|
||||
$var = $this->db_null_value($var);
|
||||
break;
|
||||
}
|
||||
return $var;
|
||||
}
|
||||
|
||||
/**
|
||||
* Erzeugt aus einem DB-Result-Boolean einen PHP Boolean
|
||||
*/
|
||||
public function db_parse_bool($var)
|
||||
{
|
||||
if($var=='t')
|
||||
return true;
|
||||
elseif($var=='f')
|
||||
return false;
|
||||
elseif($var=='')
|
||||
return '';
|
||||
else
|
||||
die('Invalid DB Boolean. Wrong DB-Engine?');
|
||||
}
|
||||
|
||||
/**
|
||||
* Bereitet ein Array von Elementen auf, damit es in der IN-Klausel eines
|
||||
* Select Befehls verwendet werden kann.
|
||||
*/
|
||||
public function db_implode4SQL($array)
|
||||
{
|
||||
$string = '';
|
||||
foreach($array as $row)
|
||||
{
|
||||
if($string!='')
|
||||
$string.=',';
|
||||
$string.=$this->db_add_param($row);
|
||||
}
|
||||
return $string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Erstellt aus einem DB Array ein PHP Array
|
||||
* @param $var DB Result Array Spalte
|
||||
* @return php array
|
||||
*/
|
||||
public function db_parse_array($var)
|
||||
{
|
||||
if ($var == '')
|
||||
return;
|
||||
preg_match_all('/(?<=^\{|,)(([^,"{]*)|\s*"((?:[^"\\\\]|\\\\(?:.|[0-9]+|x[0-9a-f]+))*)"\s*)(,|(?<!^\{)(?=\}$))/i', $var, $matches, PREG_SET_ORDER);
|
||||
$values = array();
|
||||
foreach ($matches as $match)
|
||||
{
|
||||
$values[] = $match[3] != '' ? stripcslashes($match[3]) : (strtolower($match[2]) == 'null' ? null : $match[2]);
|
||||
}
|
||||
return $values;
|
||||
}
|
||||
|
||||
/**
|
||||
* Erstellt aus einem DB Array ein PHP Array
|
||||
* @param $var DB Result Array Spalte
|
||||
* @return php array
|
||||
*/
|
||||
public function db_parse_lang_array($var)
|
||||
{
|
||||
|
||||
if ($var == '')
|
||||
return;
|
||||
preg_match_all('/(?<=^\{|,)(([^,"{]*)|\s*"((?:[^"\\\\]|\\\\(?:.|[0-9]+|x[0-9a-f]+))*)"\s*)(,|(?<!^\{)(?=\}$))/i', $var, $matches, PREG_SET_ORDER);
|
||||
$values = array();
|
||||
|
||||
$sprache = new sprache();
|
||||
$sprache->loadIndexArray();
|
||||
|
||||
$sprache = new sprache();
|
||||
$sprache->getAll(true);
|
||||
$languages = $sprache->getAllIndexesSorted();
|
||||
|
||||
|
||||
foreach ($matches as $mk => $match)
|
||||
{
|
||||
$values[$languages[$mk+1]] = $match[3] != '' ? stripcslashes($match[3]) : (strtolower($match[2]) == 'null' ? null : $match[2]);
|
||||
}
|
||||
return $values;
|
||||
}
|
||||
}
|
||||
|
||||
function indexSort($a, $b)
|
||||
{
|
||||
return strcmp($a->index, $b->index);
|
||||
}
|
||||
@@ -11,7 +11,7 @@
|
||||
* @filesource
|
||||
*/
|
||||
defined('BASEPATH') OR exit('No direct script access allowed');
|
||||
require_once 'include/authentication.class.php';
|
||||
require_once FCPATH.'include/authentication.class.php';
|
||||
|
||||
/**
|
||||
* FHC-Auth Helpers
|
||||
@@ -39,12 +39,12 @@ class FHC_Auth
|
||||
$auth = new authentication();
|
||||
if ($auth->checkpassword($username, $password))
|
||||
{
|
||||
echo 'Auth-Method-True';
|
||||
//echo 'Auth-Method-True';
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
echo 'Auth-Method-False';
|
||||
//echo 'Auth-Method-False';
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -434,7 +434,7 @@ abstract class REST_Controller extends CI_Controller {
|
||||
$language = $this->config->item('rest_language');
|
||||
if ($language === NULL)
|
||||
{
|
||||
$language = 'english';
|
||||
$language = 'en-US';
|
||||
}
|
||||
|
||||
// Load the language file
|
||||
|
||||
@@ -6,15 +6,31 @@ class Migration_Init extends CI_Migration {
|
||||
|
||||
public function up()
|
||||
{
|
||||
//$this->load->database('system');
|
||||
$this->load->database('system');
|
||||
// Schemas
|
||||
//$this->db->query('CREATE SCHEMA IF NOT EXISTS gis;');
|
||||
|
||||
$this->db->query('CREATE SCHEMA IF NOT EXISTS public;');
|
||||
$this->db->query('CREATE SCHEMA IF NOT EXISTS addon;');
|
||||
$this->db->query('CREATE SCHEMA IF NOT EXISTS fue;');
|
||||
$this->db->query('CREATE SCHEMA IF NOT EXISTS lehre;');
|
||||
$this->db->query('CREATE SCHEMA IF NOT EXISTS system;');
|
||||
$this->db->query('CREATE SCHEMA IF NOT EXISTS bis;');
|
||||
}
|
||||
|
||||
public function down()
|
||||
{
|
||||
//$this->db->query('DROP SCHEMA IF EXISTS gis;');
|
||||
/* $this->db->query('
|
||||
DROP SCHEMA IF EXISTS addon;
|
||||
DROP SCHEMA IF EXISTS bis;
|
||||
DROP SCHEMA IF EXISTS campus;
|
||||
DROP SCHEMA IF EXISTS fue;
|
||||
DROP SCHEMA IF EXISTS kommune;
|
||||
DROP SCHEMA IF EXISTS lehre;
|
||||
DROP SCHEMA IF EXISTS public;
|
||||
DROP SCHEMA IF EXISTS sync;
|
||||
DROP SCHEMA IF EXISTS system;
|
||||
DROP SCHEMA IF EXISTS testtool;
|
||||
DROP SCHEMA IF EXISTS wawi;
|
||||
');*/
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -5,26 +5,20 @@ defined('BASEPATH') OR exit('No direct script access allowed');
|
||||
class Migration_fhc30 extends CI_Migration {
|
||||
|
||||
public function up()
|
||||
{
|
||||
//$this->load->helper('file');
|
||||
require_once(FCPATH.'include/basis_db.class.php');
|
||||
$db = new basis_db();
|
||||
//$db = $this->db;
|
||||
//$db->db_query = $this->db->simple_query;
|
||||
{
|
||||
echo '<br/><h1>Update to FHC 3.0</h1><br/>';
|
||||
$this->db=$this->load->database('system', true);
|
||||
$this->load->helper('fhcdb');
|
||||
$db = new basis_db($this);
|
||||
require_once('./system/dbupdate_3.0.php');
|
||||
}
|
||||
|
||||
public function down()
|
||||
{
|
||||
/*$this->db->simple_query('DROP SCHEMA bis;');
|
||||
$this->db->simple_query('DROP SCHEMA campus;');
|
||||
$this->db->simple_query('DROP SCHEMA fue;');
|
||||
$this->db->simple_query('DROP SCHEMA kommune;');
|
||||
$this->db->simple_query('DROP SCHEMA lehre;');
|
||||
$this->db->simple_query('DROP SCHEMA sync;');
|
||||
$this->db->simple_query('DROP SCHEMA system;');
|
||||
$this->db->simple_query('DROP SCHEMA testtool;');
|
||||
$this->db->simple_query('DROP SCHEMA wawi;');*/
|
||||
/*$this->db->simple_query('DROP TABLE fue.tbl_scrumteam;');
|
||||
$this->db->simple_query('DROP TABLE lehre.tbl_studienordnung;');
|
||||
$this->db->simple_query('DROP TABLE lehre.tbl_studienordnung_semester;');
|
||||
$this->db->simple_query('DROP TABLE lehre.tbl_studienplan;');*/
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -6,17 +6,11 @@ class Migration_fhc31 extends CI_Migration {
|
||||
|
||||
public function up()
|
||||
{
|
||||
//$this->load->database('system');
|
||||
if (!$this->db->table_exists('tbl_person'))
|
||||
{
|
||||
$this->load->helper('file');
|
||||
$sqlfile = read_file('./system/fhcomplete3.0.sql');
|
||||
|
||||
if (!$this->db->simple_query($sqlfile))
|
||||
{
|
||||
echo "Error creating Basis DB-Schema!";
|
||||
}
|
||||
}
|
||||
echo '<br/><h1>Update to FHC 3.1</h1><br/>';
|
||||
$this->db=$this->load->database('system', true);
|
||||
$this->load->helper('fhcdb');
|
||||
$db = new basis_db($this);
|
||||
require_once('./system/dbupdate_3.1.php');
|
||||
}
|
||||
|
||||
public function down()
|
||||
|
||||
@@ -6,17 +6,11 @@ class Migration_fhc32 extends CI_Migration {
|
||||
|
||||
public function up()
|
||||
{
|
||||
//$this->load->database('system');
|
||||
if (!$this->db->table_exists('tbl_person'))
|
||||
{
|
||||
$this->load->helper('file');
|
||||
$sqlfile = read_file('./system/fhcomplete3.0.sql');
|
||||
|
||||
if (!$this->db->simple_query($sqlfile))
|
||||
{
|
||||
echo "Error creating Basis DB-Schema!";
|
||||
}
|
||||
}
|
||||
echo '<br/><h1>Update to FHC 3.2</h1><br/>';
|
||||
$this->db=$this->load->database('system', true);
|
||||
$this->load->helper('fhcdb');
|
||||
$db = new basis_db($this);
|
||||
require_once('./system/dbupdate_3.2.php');
|
||||
}
|
||||
|
||||
public function down()
|
||||
|
||||
Executable
+592
@@ -0,0 +1,592 @@
|
||||
<?php
|
||||
/**
|
||||
* PHP grocery CRUD
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* Grocery CRUD is released with dual licensing, using the GPL v3 (license-gpl3.txt) and the MIT license (license-mit.txt).
|
||||
* You don't have to do anything special to choose one license or the other and you don't have to notify anyone which license you are using.
|
||||
* Please see the corresponding license file for details of these licenses.
|
||||
* You are free to use, modify and distribute this software, but all copyright information must remain.
|
||||
*
|
||||
* @package grocery CRUD
|
||||
* @copyright Copyright (c) 2010 through 2012, John Skoumbourdis
|
||||
* @license https://github.com/scoumbourdis/grocery-crud/blob/master/license-grocery-crud.txt
|
||||
* @version 1.4.2
|
||||
* @author John Skoumbourdis <scoumbourdisj@gmail.com>
|
||||
*/
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Grocery CRUD Model
|
||||
*
|
||||
*
|
||||
* @package grocery CRUD
|
||||
* @author John Skoumbourdis <scoumbourdisj@gmail.com>
|
||||
* @version 1.5.4
|
||||
* @link http://www.grocerycrud.com/documentation
|
||||
*/
|
||||
class Grocery_crud_model extends CI_Model {
|
||||
|
||||
protected $primary_key = null;
|
||||
protected $table_name = null;
|
||||
protected $relation = array();
|
||||
protected $relation_n_n = array();
|
||||
protected $primary_keys = array();
|
||||
|
||||
function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
function db_table_exists($table_name = null)
|
||||
{
|
||||
return $this->db->table_exists($table_name);
|
||||
}
|
||||
|
||||
function get_list()
|
||||
{
|
||||
if($this->table_name === null)
|
||||
return false;
|
||||
|
||||
$select = "{$this->table_name}.*";
|
||||
|
||||
//set_relation special queries
|
||||
if(!empty($this->relation))
|
||||
{
|
||||
foreach($this->relation as $relation)
|
||||
{
|
||||
list($field_name , $related_table , $related_field_title) = $relation;
|
||||
$unique_join_name = $this->_unique_join_name($field_name);
|
||||
$unique_field_name = $this->_unique_field_name($field_name);
|
||||
|
||||
if(strstr($related_field_title,'{'))
|
||||
{
|
||||
$related_field_title = str_replace(" "," ",$related_field_title);
|
||||
$select .= ", CONCAT('".str_replace(array('{','}'),array("',COALESCE({$unique_join_name}.",", ''),'"),str_replace("'","\\'",$related_field_title))."') as $unique_field_name";
|
||||
}
|
||||
else
|
||||
{
|
||||
$select .= ", $unique_join_name.$related_field_title AS $unique_field_name";
|
||||
}
|
||||
|
||||
if($this->field_exists($related_field_title))
|
||||
$select .= ", {$this->table_name}.$related_field_title AS {$this->table_name}.$related_field_title";
|
||||
}
|
||||
}
|
||||
|
||||
//set_relation_n_n special queries. We prefer sub queries from a simple join for the relation_n_n as it is faster and more stable on big tables.
|
||||
if(!empty($this->relation_n_n))
|
||||
{
|
||||
$select = $this->relation_n_n_queries($select);
|
||||
}
|
||||
|
||||
$this->db->select($select, false);
|
||||
|
||||
$results = $this->db->get($this->table_name)->result();
|
||||
|
||||
return $results;
|
||||
}
|
||||
|
||||
public function get_row($table_name = null)
|
||||
{
|
||||
$table_name = $table_name === null ? $this->table_name : $table_name;
|
||||
|
||||
return $this->db->get($table_name)->row();
|
||||
}
|
||||
|
||||
public function set_primary_key($field_name, $table_name = null)
|
||||
{
|
||||
$table_name = $table_name === null ? $this->table_name : $table_name;
|
||||
|
||||
$this->primary_keys[$table_name] = $field_name;
|
||||
}
|
||||
|
||||
protected function relation_n_n_queries($select)
|
||||
{
|
||||
$this_table_primary_key = $this->get_primary_key();
|
||||
foreach($this->relation_n_n as $relation_n_n)
|
||||
{
|
||||
list($field_name, $relation_table, $selection_table, $primary_key_alias_to_this_table,
|
||||
$primary_key_alias_to_selection_table, $title_field_selection_table, $priority_field_relation_table) = array_values((array)$relation_n_n);
|
||||
|
||||
$primary_key_selection_table = $this->get_primary_key($selection_table);
|
||||
|
||||
$field = "";
|
||||
$use_template = strpos($title_field_selection_table,'{') !== false;
|
||||
$field_name_hash = $this->_unique_field_name($title_field_selection_table);
|
||||
if($use_template)
|
||||
{
|
||||
$title_field_selection_table = str_replace(" ", " ", $title_field_selection_table);
|
||||
$field .= "CONCAT('".str_replace(array('{','}'),array("',COALESCE(",", ''),'"),str_replace("'","\\'",$title_field_selection_table))."')";
|
||||
}
|
||||
else
|
||||
{
|
||||
$field .= "$selection_table.$title_field_selection_table";
|
||||
}
|
||||
|
||||
//Sorry Codeigniter but you cannot help me with the subquery!
|
||||
$select .= ", (SELECT GROUP_CONCAT(DISTINCT $field) FROM $selection_table "
|
||||
."LEFT JOIN $relation_table ON $relation_table.$primary_key_alias_to_selection_table = $selection_table.$primary_key_selection_table "
|
||||
."WHERE $relation_table.$primary_key_alias_to_this_table = `{$this->table_name}`.$this_table_primary_key GROUP BY $relation_table.$primary_key_alias_to_this_table) AS $field_name";
|
||||
}
|
||||
|
||||
return $select;
|
||||
}
|
||||
|
||||
function order_by($order_by , $direction)
|
||||
{
|
||||
$this->db->order_by( $order_by , $direction );
|
||||
}
|
||||
|
||||
function where($key, $value = NULL, $escape = TRUE)
|
||||
{
|
||||
$this->db->where( $key, $value, $escape);
|
||||
}
|
||||
|
||||
function or_where($key, $value = NULL, $escape = TRUE)
|
||||
{
|
||||
$this->db->or_where( $key, $value, $escape);
|
||||
}
|
||||
|
||||
function having($key, $value = NULL, $escape = TRUE)
|
||||
{
|
||||
$this->db->having( $key, $value, $escape);
|
||||
}
|
||||
|
||||
function or_having($key, $value = NULL, $escape = TRUE)
|
||||
{
|
||||
$this->db->or_having( $key, $value, $escape);
|
||||
}
|
||||
|
||||
function like($field, $match = '', $side = 'both')
|
||||
{
|
||||
$this->db->like($field, $match, $side);
|
||||
}
|
||||
|
||||
function or_like($field, $match = '', $side = 'both')
|
||||
{
|
||||
$this->db->or_like($field, $match, $side);
|
||||
}
|
||||
|
||||
function limit($value, $offset = '')
|
||||
{
|
||||
$this->db->limit( $value , $offset );
|
||||
}
|
||||
|
||||
function get_total_results()
|
||||
{
|
||||
//set_relation_n_n special queries. We prefer sub queries from a simple join for the relation_n_n as it is faster and more stable on big tables.
|
||||
if(!empty($this->relation_n_n))
|
||||
{
|
||||
$select = "{$this->table_name}.*";
|
||||
$select = $this->relation_n_n_queries($select);
|
||||
|
||||
$this->db->select($select,false);
|
||||
}
|
||||
|
||||
return $this->db->get($this->table_name)->num_rows();
|
||||
}
|
||||
|
||||
function set_basic_table($table_name = null)
|
||||
{
|
||||
if( !($this->db->table_exists($table_name)) )
|
||||
return false;
|
||||
|
||||
$this->table_name = $table_name;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
function get_edit_values($primary_key_value)
|
||||
{
|
||||
$primary_key_field = $this->get_primary_key();
|
||||
$this->db->where($primary_key_field,$primary_key_value);
|
||||
$result = $this->db->get($this->table_name)->row();
|
||||
return $result;
|
||||
}
|
||||
|
||||
function join_relation($field_name , $related_table , $related_field_title)
|
||||
{
|
||||
$related_primary_key = $this->get_primary_key($related_table);
|
||||
|
||||
if($related_primary_key !== false)
|
||||
{
|
||||
$unique_name = $this->_unique_join_name($field_name);
|
||||
$this->db->join( $related_table.' as '.$unique_name , "$unique_name.$related_primary_key = {$this->table_name}.$field_name",'left');
|
||||
|
||||
$this->relation[$field_name] = array($field_name , $related_table , $related_field_title);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
function set_relation_n_n_field($field_info)
|
||||
{
|
||||
$this->relation_n_n[$field_info->field_name] = $field_info;
|
||||
}
|
||||
|
||||
protected function _unique_join_name($field_name)
|
||||
{
|
||||
return 'j'.substr(md5($field_name),0,8); //This j is because is better for a string to begin with a letter and not with a number
|
||||
}
|
||||
|
||||
protected function _unique_field_name($field_name)
|
||||
{
|
||||
return 's'.substr(md5($field_name),0,8); //This s is because is better for a string to begin with a letter and not with a number
|
||||
}
|
||||
|
||||
function get_relation_array($field_name , $related_table , $related_field_title, $where_clause, $order_by, $limit = null, $search_like = null)
|
||||
{
|
||||
$relation_array = array();
|
||||
$field_name_hash = $this->_unique_field_name($field_name);
|
||||
|
||||
$related_primary_key = $this->get_primary_key($related_table);
|
||||
|
||||
$select = "$related_table.$related_primary_key, ";
|
||||
|
||||
if(strstr($related_field_title,'{'))
|
||||
{
|
||||
$related_field_title = str_replace(" ", " ", $related_field_title);
|
||||
$select .= "CONCAT('".str_replace(array('{','}'),array("',COALESCE(",", ''),'"),str_replace("'","\\'",$related_field_title))."') as $field_name_hash";
|
||||
}
|
||||
else
|
||||
{
|
||||
$select .= "$related_table.$related_field_title as $field_name_hash";
|
||||
}
|
||||
|
||||
$this->db->select($select,false);
|
||||
if($where_clause !== null)
|
||||
$this->db->where($where_clause);
|
||||
|
||||
if($where_clause !== null)
|
||||
$this->db->where($where_clause);
|
||||
|
||||
if($limit !== null)
|
||||
$this->db->limit($limit);
|
||||
|
||||
if($search_like !== null)
|
||||
$this->db->having("$field_name_hash LIKE '%".$this->db->escape_like_str($search_like)."%'");
|
||||
|
||||
$order_by !== null
|
||||
? $this->db->order_by($order_by)
|
||||
: $this->db->order_by($field_name_hash);
|
||||
|
||||
$results = $this->db->get($related_table)->result();
|
||||
|
||||
foreach($results as $row)
|
||||
{
|
||||
$relation_array[$row->$related_primary_key] = $row->$field_name_hash;
|
||||
}
|
||||
|
||||
return $relation_array;
|
||||
}
|
||||
|
||||
function get_ajax_relation_array($search, $field_name , $related_table , $related_field_title, $where_clause, $order_by)
|
||||
{
|
||||
return $this->get_relation_array($field_name , $related_table , $related_field_title, $where_clause, $order_by, 10 , $search);
|
||||
}
|
||||
|
||||
function get_relation_total_rows($field_name , $related_table , $related_field_title, $where_clause)
|
||||
{
|
||||
if($where_clause !== null)
|
||||
$this->db->where($where_clause);
|
||||
|
||||
return $this->db->count_all_results($related_table);
|
||||
}
|
||||
|
||||
function get_relation_n_n_selection_array($primary_key_value, $field_info)
|
||||
{
|
||||
$select = "";
|
||||
$related_field_title = $field_info->title_field_selection_table;
|
||||
$use_template = strpos($related_field_title,'{') !== false;;
|
||||
$field_name_hash = $this->_unique_field_name($related_field_title);
|
||||
if($use_template)
|
||||
{
|
||||
$related_field_title = str_replace(" ", " ", $related_field_title);
|
||||
$select .= "CONCAT('".str_replace(array('{','}'),array("',COALESCE(",", ''),'"),str_replace("'","\\'",$related_field_title))."') as $field_name_hash";
|
||||
}
|
||||
else
|
||||
{
|
||||
$select .= "$related_field_title as $field_name_hash";
|
||||
}
|
||||
$this->db->select('*, '.$select,false);
|
||||
|
||||
$selection_primary_key = $this->get_primary_key($field_info->selection_table);
|
||||
|
||||
if(empty($field_info->priority_field_relation_table))
|
||||
{
|
||||
if(!$use_template){
|
||||
$this->db->order_by("{$field_info->selection_table}.{$field_info->title_field_selection_table}");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->db->order_by("{$field_info->relation_table}.{$field_info->priority_field_relation_table}");
|
||||
}
|
||||
$this->db->where($field_info->primary_key_alias_to_this_table, $primary_key_value);
|
||||
$this->db->join(
|
||||
$field_info->selection_table,
|
||||
"{$field_info->relation_table}.{$field_info->primary_key_alias_to_selection_table} = {$field_info->selection_table}.{$selection_primary_key}"
|
||||
);
|
||||
$results = $this->db->get($field_info->relation_table)->result();
|
||||
|
||||
$results_array = array();
|
||||
foreach($results as $row)
|
||||
{
|
||||
$results_array[$row->{$field_info->primary_key_alias_to_selection_table}] = $row->{$field_name_hash};
|
||||
}
|
||||
|
||||
return $results_array;
|
||||
}
|
||||
|
||||
function get_relation_n_n_unselected_array($field_info, $selected_values)
|
||||
{
|
||||
$use_where_clause = !empty($field_info->where_clause);
|
||||
|
||||
$select = "";
|
||||
$related_field_title = $field_info->title_field_selection_table;
|
||||
$use_template = strpos($related_field_title,'{') !== false;
|
||||
$field_name_hash = $this->_unique_field_name($related_field_title);
|
||||
|
||||
if($use_template)
|
||||
{
|
||||
$related_field_title = str_replace(" ", " ", $related_field_title);
|
||||
$select .= "CONCAT('".str_replace(array('{','}'),array("',COALESCE(",", ''),'"),str_replace("'","\\'",$related_field_title))."') as $field_name_hash";
|
||||
}
|
||||
else
|
||||
{
|
||||
$select .= "$related_field_title as $field_name_hash";
|
||||
}
|
||||
$this->db->select('*, '.$select,false);
|
||||
|
||||
if($use_where_clause){
|
||||
$this->db->where($field_info->where_clause);
|
||||
}
|
||||
|
||||
$selection_primary_key = $this->get_primary_key($field_info->selection_table);
|
||||
if(!$use_template)
|
||||
$this->db->order_by("{$field_info->selection_table}.{$field_info->title_field_selection_table}");
|
||||
$results = $this->db->get($field_info->selection_table)->result();
|
||||
|
||||
$results_array = array();
|
||||
foreach($results as $row)
|
||||
{
|
||||
if(!isset($selected_values[$row->$selection_primary_key]))
|
||||
$results_array[$row->$selection_primary_key] = $row->{$field_name_hash};
|
||||
}
|
||||
|
||||
return $results_array;
|
||||
}
|
||||
|
||||
function db_relation_n_n_update($field_info, $post_data ,$main_primary_key)
|
||||
{
|
||||
$this->db->where($field_info->primary_key_alias_to_this_table, $main_primary_key);
|
||||
if(!empty($post_data))
|
||||
$this->db->where_not_in($field_info->primary_key_alias_to_selection_table , $post_data);
|
||||
$this->db->delete($field_info->relation_table);
|
||||
|
||||
$counter = 0;
|
||||
if(!empty($post_data))
|
||||
{
|
||||
foreach($post_data as $primary_key_value)
|
||||
{
|
||||
$where_array = array(
|
||||
$field_info->primary_key_alias_to_this_table => $main_primary_key,
|
||||
$field_info->primary_key_alias_to_selection_table => $primary_key_value,
|
||||
);
|
||||
|
||||
$this->db->where($where_array);
|
||||
$count = $this->db->from($field_info->relation_table)->count_all_results();
|
||||
|
||||
if($count == 0)
|
||||
{
|
||||
if(!empty($field_info->priority_field_relation_table))
|
||||
$where_array[$field_info->priority_field_relation_table] = $counter;
|
||||
|
||||
$this->db->insert($field_info->relation_table, $where_array);
|
||||
|
||||
}elseif($count >= 1 && !empty($field_info->priority_field_relation_table))
|
||||
{
|
||||
$this->db->update( $field_info->relation_table, array($field_info->priority_field_relation_table => $counter) , $where_array);
|
||||
}
|
||||
|
||||
$counter++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function db_relation_n_n_delete($field_info, $main_primary_key)
|
||||
{
|
||||
$this->db->where($field_info->primary_key_alias_to_this_table, $main_primary_key);
|
||||
$this->db->delete($field_info->relation_table);
|
||||
}
|
||||
|
||||
function get_field_types_basic_table()
|
||||
{
|
||||
$db_field_types = array();
|
||||
foreach($this->db->query('SELECT column_name AS "Field", data_type AS "Type",
|
||||
CASE WHEN indisprimary THEN '."'PRI'".' WHEN indisunique THEN '."'UNI'".' ELSE null END AS "Key", is_nullable AS "Null", NULL AS "Extra"
|
||||
FROM information_schema.columns
|
||||
JOIN pg_namespace ON (table_schema=nspname)
|
||||
JOIN pg_class ON (pg_class.relnamespace = pg_namespace.oid AND pg_class.oid = table_name::regclass)
|
||||
JOIN pg_attribute ON (pg_attribute.attrelid = pg_class.oid AND attname=column_name)
|
||||
LEFT JOIN pg_index ON (indrelid = pg_class.oid AND
|
||||
pg_attribute.attnum = any(pg_index.indkey))
|
||||
WHERE table_name = '."'{$this->table_name}'")->result() as $db_field_type)
|
||||
{
|
||||
$type = explode("(",$db_field_type->Type);
|
||||
$db_type = $type[0];
|
||||
|
||||
if(isset($type[1]))
|
||||
{
|
||||
if(substr($type[1],-1) == ')')
|
||||
{
|
||||
$length = substr($type[1],0,-1);
|
||||
}
|
||||
else
|
||||
{
|
||||
list($length) = explode(" ",$type[1]);
|
||||
$length = substr($length,0,-1);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
$length = '';
|
||||
}
|
||||
$db_field_types[$db_field_type->Field]['db_max_length'] = $length;
|
||||
$db_field_types[$db_field_type->Field]['db_type'] = $db_type;
|
||||
$db_field_types[$db_field_type->Field]['db_null'] = $db_field_type->Null == 'YES' ? true : false;
|
||||
$db_field_types[$db_field_type->Field]['db_extra'] = $db_field_type->Extra;
|
||||
$db_field_types[$db_field_type->Field]['db_key'] = $db_field_type->Key;
|
||||
$db_field_types[$db_field_type->Field]['primary_key'] = $db_field_type->Key == 'PRI' ? 1 : 0;
|
||||
}
|
||||
|
||||
$results = $this->db->field_data($this->table_name);
|
||||
foreach($results as $num => $row)
|
||||
{
|
||||
$row = (array)$row;
|
||||
$results[$num] = (object)( array_merge($row, $db_field_types[$row['name']]) );
|
||||
}
|
||||
|
||||
return $results;
|
||||
}
|
||||
|
||||
function get_field_types($table_name)
|
||||
{
|
||||
$results = $this->db->field_data($table_name);
|
||||
|
||||
return $results;
|
||||
}
|
||||
|
||||
function db_update($post_array, $primary_key_value)
|
||||
{
|
||||
$primary_key_field = $this->get_primary_key();
|
||||
return $this->db->update($this->table_name,$post_array, array( $primary_key_field => $primary_key_value));
|
||||
}
|
||||
|
||||
function db_insert($post_array)
|
||||
{
|
||||
$insert = $this->db->insert($this->table_name,$post_array);
|
||||
if($insert)
|
||||
{
|
||||
return $this->db->insert_id();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
function db_delete($primary_key_value)
|
||||
{
|
||||
$primary_key_field = $this->get_primary_key();
|
||||
|
||||
if($primary_key_field === false)
|
||||
return false;
|
||||
|
||||
$this->db->limit(1);
|
||||
$this->db->delete($this->table_name,array( $primary_key_field => $primary_key_value));
|
||||
if( $this->db->affected_rows() != 1)
|
||||
return false;
|
||||
else
|
||||
return true;
|
||||
}
|
||||
|
||||
function db_file_delete($field_name, $filename)
|
||||
{
|
||||
if( $this->db->update($this->table_name,array($field_name => ''),array($field_name => $filename)) )
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
function field_exists($field,$table_name = null)
|
||||
{
|
||||
if(empty($table_name))
|
||||
{
|
||||
$table_name = $this->table_name;
|
||||
}
|
||||
return $this->db->field_exists($field,$table_name);
|
||||
}
|
||||
|
||||
function get_primary_key($table_name = null)
|
||||
{
|
||||
if($table_name == null)
|
||||
{
|
||||
if(isset($this->primary_keys[$this->table_name]))
|
||||
{
|
||||
return $this->primary_keys[$this->table_name];
|
||||
}
|
||||
|
||||
if(empty($this->primary_key))
|
||||
{
|
||||
$fields = $this->get_field_types_basic_table();
|
||||
|
||||
foreach($fields as $field)
|
||||
{
|
||||
if($field->primary_key == 1)
|
||||
{
|
||||
return $field->name;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
return $this->primary_key;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if(isset($this->primary_keys[$table_name]))
|
||||
{
|
||||
return $this->primary_keys[$table_name];
|
||||
}
|
||||
|
||||
$fields = $this->get_field_types($table_name);
|
||||
|
||||
foreach($fields as $field)
|
||||
{
|
||||
/*if($field->primary_key == 1)
|
||||
{
|
||||
return $field->name;
|
||||
}*/
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function escape_str($value)
|
||||
{
|
||||
return $this->db->escape_str($value);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,703 @@
|
||||
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
|
||||
|
||||
class Message_model extends DB_Model
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
require_once APPPATH.'config/message.php';
|
||||
//$this->load->helper('language');
|
||||
$this->lang->load('message');
|
||||
}
|
||||
|
||||
/** -----------------------------------------------------------------
|
||||
* get_message() - will return a single message, including the status for specified user.
|
||||
*
|
||||
* @param integer $msg_id EQUIRED
|
||||
* @param integer $user_id REQUIRED
|
||||
* @return array
|
||||
*/
|
||||
function getMessage($msg_id, $user_id)
|
||||
{
|
||||
// Validate
|
||||
if (empty($msg_id))
|
||||
{
|
||||
return $this->_invalid_id(MSG_ERR_INVALID_MSG_ID);
|
||||
}
|
||||
|
||||
if (empty($user_id))
|
||||
{
|
||||
return $this->_invalid_id(MSG_ERR_INVALID_USER_ID);
|
||||
}
|
||||
|
||||
$sql = 'SELECT m.*, s.status, t.subject, ' . USER_TABLE_USERNAME .
|
||||
' FROM ' . $this->db->dbprefix . 'msg_messages m ' .
|
||||
' JOIN ' . $this->db->dbprefix . 'msg_threads t ON (m.thread_id = t.id) ' .
|
||||
' JOIN ' . $this->db->dbprefix . USER_TABLE_TABLENAME . ' ON (' . USER_TABLE_ID . ' = m.sender_id) '.
|
||||
' JOIN ' . $this->db->dbprefix . 'msg_status s ON (s.message_id = m.id AND s.user_id = ? ) ' .
|
||||
' WHERE m.id = ? ' ;
|
||||
|
||||
$result = $this->db->query($sql, array($user_id, $msg_id));
|
||||
|
||||
if ($result)
|
||||
return $this->_success($result->result_array());
|
||||
else
|
||||
return $this->_general_error();
|
||||
}
|
||||
|
||||
/** -----------------------------------------------------------------
|
||||
* Get a Full Thread
|
||||
* get_full_thread() - will return a entire thread, including the status for specified user.
|
||||
*
|
||||
* @param integer $thread_id REQUIRED
|
||||
* @param integer $user_id REQUIRED
|
||||
* @param boolean $full_thread OPTIONAL - If true, user will also see messages from thread posted BEFORE user became participant
|
||||
* @param string $order_by OPTIONAL
|
||||
* @return array
|
||||
*/
|
||||
function get_full_thread($thread_id, $user_id, $full_thread = FALSE, $order_by = 'ASC')
|
||||
{
|
||||
// Validate
|
||||
if (empty($thread_id))
|
||||
{
|
||||
return $this->_invalid_id(MSG_ERR_INVALID_THREAD_ID);
|
||||
}
|
||||
|
||||
if (empty($user_id))
|
||||
{
|
||||
return $this->_invalid_id(MSG_ERR_INVALID_USER_ID);
|
||||
}
|
||||
|
||||
$sql = 'SELECT m.*, s.status, t.subject, '.USER_TABLE_USERNAME .
|
||||
' FROM ' . $this->db->dbprefix . 'msg_participants p ' .
|
||||
' JOIN ' . $this->db->dbprefix . 'msg_threads t ON (t.id = p.thread_id) ' .
|
||||
' JOIN ' . $this->db->dbprefix . 'msg_messages m ON (m.thread_id = t.id) ' .
|
||||
' JOIN ' . $this->db->dbprefix . USER_TABLE_TABLENAME . ' ON (' . USER_TABLE_ID . ' = m.sender_id) '.
|
||||
' JOIN ' . $this->db->dbprefix . 'msg_status s ON (s.message_id = m.id AND s.user_id = ? ) ' .
|
||||
' WHERE p.user_id = ? ' .
|
||||
' AND p.thread_id = ? ';
|
||||
|
||||
if ( ! $full_thread)
|
||||
{
|
||||
$sql .= ' AND m.cdate >= p.cdate';
|
||||
}
|
||||
|
||||
$sql .= ' ORDER BY m.cdate ' . $order_by;
|
||||
|
||||
$result = $this->db->query($sql, array($user_id, $user_id, $thread_id));
|
||||
|
||||
if ($result)
|
||||
return $this->_success($result->result_array());
|
||||
else
|
||||
return $this->_general_error();
|
||||
}
|
||||
|
||||
/** -----------------------------------------------------------------
|
||||
* get_all_threads() - will return all threads for user, including the status for specified user.
|
||||
*
|
||||
* @param integer $user_id REQUIRED
|
||||
* @param boolean $full_thread OPTIONAL - If true, user will also see messages from thread posted BEFORE user became participant
|
||||
* @param string $order_by OPTIONAL
|
||||
* @return array
|
||||
*/
|
||||
function get_all_threads($user_id, $full_thread = FALSE, $order_by = 'asc')
|
||||
{
|
||||
if (empty($user_id))
|
||||
{
|
||||
return $this->_invalid_id(MSG_ERR_INVALID_USER_ID);
|
||||
}
|
||||
|
||||
$sql = 'SELECT m.*, s.status, t.subject, '.USER_TABLE_USERNAME .
|
||||
' FROM ' . $this->db->dbprefix . 'msg_participants p ' .
|
||||
' JOIN ' . $this->db->dbprefix . 'msg_threads t ON (t.id = p.thread_id) ' .
|
||||
' JOIN ' . $this->db->dbprefix . 'msg_messages m ON (m.thread_id = t.id) ' .
|
||||
' JOIN ' . $this->db->dbprefix . USER_TABLE_TABLENAME . ' ON (' . USER_TABLE_ID . ' = m.sender_id) '.
|
||||
' JOIN ' . $this->db->dbprefix . 'msg_status s ON (s.message_id = m.id AND s.user_id = ? ) ' .
|
||||
' WHERE p.user_id = ? ' ;
|
||||
|
||||
if (!$full_thread)
|
||||
{
|
||||
$sql .= ' AND m.cdate >= p.cdate';
|
||||
}
|
||||
|
||||
$sql .= ' ORDER BY t.id ' . $order_by. ', m.cdate '. $order_by;
|
||||
|
||||
$result = $this->db->query($sql, array($user_id, $user_id));
|
||||
|
||||
if ($result)
|
||||
return $this->_success($result->result_array());
|
||||
else
|
||||
return $this->_general_error();
|
||||
}
|
||||
|
||||
/** -----------------------------------------------------------------
|
||||
* Get all Threads Grouped
|
||||
* get_all_threads_grouped() - will return all threads for user, including the status for specified user.
|
||||
* - messages are grouped in threads.
|
||||
*
|
||||
* @param integer $user_id REQUIRED
|
||||
* @param boolean $full_thread OPTIONAL - If true, user will also see messages from thread posted BEFORE user became participant
|
||||
* @param string $order_by OPTIONAL
|
||||
* @return array
|
||||
*/
|
||||
function get_all_threads_grouped($user_id, $full_thread = FALSE, $order_by = 'ASC')
|
||||
{
|
||||
if (empty($user_id))
|
||||
{
|
||||
return $this->_invalid_id(MSG_ERR_INVALID_USER_ID);
|
||||
}
|
||||
|
||||
$message = $this->get_all_threads($user_id, $full_thread, $order_by);
|
||||
if (is_array($message))
|
||||
{
|
||||
$threads = array();
|
||||
|
||||
foreach ($message as $msg)
|
||||
{
|
||||
if ( ! isset($threads[$msg['thread_id']]))
|
||||
{
|
||||
$threads[$msg['thread_id']]['thread_id'] = $msg['thread_id'];
|
||||
$threads[$msg['thread_id']]['messages'] = array($msg);
|
||||
}
|
||||
else
|
||||
{
|
||||
$threads[$msg['thread_id']]['messages'][] = $msg;
|
||||
}
|
||||
}
|
||||
|
||||
return $this->_success($threads);
|
||||
}
|
||||
|
||||
// General Error Occurred
|
||||
return $this->_general_error();
|
||||
}
|
||||
|
||||
/** -----------------------------------------------------------------
|
||||
* Change Message Status
|
||||
* update_message_status() - will change status on message for particular user
|
||||
*
|
||||
* @param integer $msg_id REQUIRED
|
||||
* @param integer $user_id REQUIRED
|
||||
* @param integer $status_id REQUIRED - should come from config/message.php list of constants
|
||||
* @return array
|
||||
*/
|
||||
function update_message_status($msg_id, $user_id, $status_id)
|
||||
{
|
||||
// Validate
|
||||
if (empty($msg_id))
|
||||
{
|
||||
return $this->_invalid_id(MSG_ERR_INVALID_MSG_ID);
|
||||
}
|
||||
|
||||
if (empty($user_id))
|
||||
{
|
||||
return $this->_invalid_id(MSG_ERR_INVALID_USER_ID);
|
||||
}
|
||||
|
||||
if (empty($status_id))
|
||||
{
|
||||
return $this->_invalid_id(MSG_ERR_INVALID_STATUS_ID);
|
||||
}
|
||||
|
||||
$this->db->where(array('message_id' => $msg_id, 'user_id' => $user_id ));
|
||||
$this->db->update('msg_status', array('status' => $status_id ));
|
||||
|
||||
$rows = $this->db->affected_rows();
|
||||
if ($rows == 1)
|
||||
return $this->_success($rows, MSG_STATUS_UPDATE);
|
||||
else
|
||||
return $this->_general_error();
|
||||
}
|
||||
|
||||
|
||||
/** -----------------------------------------------------------------
|
||||
* Add a Participant
|
||||
* add_participant() - adds user to existing thread
|
||||
*
|
||||
* @param integer $thread_id REQUIRED
|
||||
* @param integer $user_id REQUIRED
|
||||
* @return array
|
||||
*/
|
||||
function add_participant($thread_id, $user_id)
|
||||
{
|
||||
|
||||
if (empty($thread_id))
|
||||
{
|
||||
return $this->_invalid_id(MSG_ERR_INVALID_THREAD_ID);
|
||||
}
|
||||
|
||||
if (empty($user_id))
|
||||
{
|
||||
return $this->_invalid_id(MSG_ERR_INVALID_USER_ID);
|
||||
}
|
||||
|
||||
if ( ! $this->valid_new_participant($thread_id, $user_id))
|
||||
{
|
||||
$this->_participant_error(MSG_ERR_PARTICIPANT_EXISTS);
|
||||
}
|
||||
|
||||
if ( ! $this->application_user($user_id))
|
||||
{
|
||||
$this->_participant_error(MSG_ERR_PARTICIPANT_NONSYSTEM);
|
||||
}
|
||||
|
||||
$this->db->trans_start();
|
||||
|
||||
$participants[] = array('thread_id' => $thread_id,'user_id' => $user_id);
|
||||
|
||||
$this->_insert_participants($participants);
|
||||
|
||||
// Get Messages by Thread
|
||||
$messages = $this->_get_messages_by_thread_id($thread_id);
|
||||
|
||||
foreach ($messages as $message)
|
||||
{
|
||||
$statuses[] = array('message_id' => $message['id'], 'user_id' => $user_id, 'status' => MSG_STATUS_UNREAD);
|
||||
}
|
||||
|
||||
$this->_insert_statuses($statuses);
|
||||
|
||||
$this->db->trans_complete();
|
||||
|
||||
if ($this->db->trans_status() === FALSE)
|
||||
{
|
||||
$this->db->trans_rollback();
|
||||
return $this->_general_error();
|
||||
}
|
||||
|
||||
return $this->_success(NULL, MSG_PARTICIPANT_ADDED);
|
||||
}
|
||||
|
||||
|
||||
/** ---------------------------------------------------------------
|
||||
* Remove a Participant
|
||||
* remove_participant() - removes user from existing thread
|
||||
*
|
||||
* @param integer $thread_id REQUIRED
|
||||
* @param integer $user_id REQUIRED
|
||||
* @return array
|
||||
*/
|
||||
function remove_participant($thread_id, $user_id)
|
||||
{
|
||||
// Validate
|
||||
if (empty($thread_id))
|
||||
{
|
||||
return $this->_invalid_id(MSG_ERR_INVALID_THREAD_ID);
|
||||
}
|
||||
|
||||
if (empty($user_id))
|
||||
{
|
||||
return $this->_invalid_id(MSG_ERR_INVALID_USER_ID);
|
||||
}
|
||||
|
||||
$this->db->trans_start();
|
||||
|
||||
$this->_delete_participant($thread_id, $user_id);
|
||||
$this->_delete_statuses($thread_id, $user_id);
|
||||
|
||||
$this->db->trans_complete();
|
||||
|
||||
if ($this->db->trans_status() === FALSE)
|
||||
{
|
||||
$this->db->trans_rollback();
|
||||
return $this->_success(NULL, MSG_PARTICIPANT_REMOVED);
|
||||
}
|
||||
|
||||
return $this->_general_error();
|
||||
}
|
||||
|
||||
/** ----------------------------------------------------------------
|
||||
* Send a New Message
|
||||
* send_new_message() - sends new internal message. This function will create a new thread
|
||||
*
|
||||
* @param integer $sender_id REQUIRED
|
||||
* @param mixed $recipients REQUIRED - a single integer or an array of integers, representing user_ids
|
||||
* @param string $subject
|
||||
* @param string $body
|
||||
* @param integer $priority
|
||||
* @return array
|
||||
*/
|
||||
function send_new_message($sender_id, $recipients, $subject, $body, $priority)
|
||||
{
|
||||
// Validate
|
||||
if (empty($sender_id))
|
||||
{
|
||||
return $this->_invalid_id(MSG_ERR_INVALID_SENDER_ID);
|
||||
}
|
||||
|
||||
if (empty($recipients))
|
||||
{
|
||||
return array(
|
||||
'err' => 1,
|
||||
'code' => MSG_ERR_INVALID_RECIPIENTS,
|
||||
'msg' => lang('mahana_'.MSG_ERR_INVALID_RECIPIENTS)
|
||||
);
|
||||
}
|
||||
|
||||
$this->db->trans_start();
|
||||
|
||||
$thread_id = $this->_insert_thread($subject);
|
||||
$msg_id = $this->_insert_message($thread_id, $sender_id, $body, $priority);
|
||||
|
||||
// Create batch inserts
|
||||
$participants[] = array('thread_id' => $thread_id,'user_id' => $sender_id);
|
||||
$statuses[] = array('message_id' => $msg_id, 'user_id' => $sender_id,'status' => MSG_STATUS_READ);
|
||||
|
||||
if ( ! is_array($recipients) )
|
||||
{
|
||||
if ($sender_id != $recipients)
|
||||
{
|
||||
$participants[] = array('thread_id' => $thread_id,'user_id' => $recipients);
|
||||
$statuses[] = array('message_id' => $msg_id, 'user_id' => $recipients, 'status' => MSG_STATUS_UNREAD);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach ($recipients as $recipient)
|
||||
{
|
||||
if ($sender_id != $recipient)
|
||||
{
|
||||
$participants[] = array('thread_id' => $thread_id,'user_id' => $recipient);
|
||||
$statuses[] = array('message_id' => $msg_id, 'user_id' => $recipient, 'status' => MSG_STATUS_UNREAD);
|
||||
}
|
||||
}
|
||||
}
|
||||
$participants=array_unique($participants, SORT_REGULAR); // Clean if sender and recipient is the same
|
||||
$this->_insert_participants($participants);
|
||||
$this->_insert_statuses($statuses);
|
||||
|
||||
$this->db->trans_complete();
|
||||
|
||||
if ($this->db->trans_status() === FALSE)
|
||||
{
|
||||
$this->db->trans_rollback();
|
||||
return $this->_general_error();
|
||||
}
|
||||
|
||||
return $this->_success($thread_id, MSG_MESSAGE_SENT);
|
||||
}
|
||||
|
||||
/** --------------------------------------------------------------
|
||||
* Reply to Message
|
||||
* reply_to_message() - replies to internal message. This function will NOT create a new thread or participant list
|
||||
*
|
||||
* @param integer $msg_id REQUIRED
|
||||
* @param integer $sender_id REQUIRED
|
||||
* @param string $subject
|
||||
* @param string $body
|
||||
* @param integer $priority
|
||||
* @return array
|
||||
*/
|
||||
function reply_to_message($reply_msg_id, $sender_id, $body, $priority)
|
||||
{
|
||||
// Validate
|
||||
if (empty($sender_id))
|
||||
{
|
||||
return $this->_invalid_id(MSG_ERR_INVALID_SENDER_ID);
|
||||
}
|
||||
|
||||
if (empty($msg_id))
|
||||
{
|
||||
return $this->_invalid_id(MSG_ERR_INVALID_MSG_ID);
|
||||
}
|
||||
|
||||
$this->db->trans_start();
|
||||
|
||||
// Get the thread id to keep messages together
|
||||
if ( ! $thread_id = $this->_get_thread_id_from_message($reply_msg_id))
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
// Add this message
|
||||
$msg_id = $this->_insert_message($thread_id, $sender_id, $body, $priority);
|
||||
|
||||
if ($recipients = $this->_get_thread_participants($thread_id, $sender_id))
|
||||
{
|
||||
$statuses[] = array('message_id' => $msg_id, 'user_id' => $sender_id,'status' => MSG_STATUS_READ);
|
||||
|
||||
foreach ($recipients as $recipient)
|
||||
{
|
||||
$statuses[] = array('message_id' => $msg_id, 'user_id' => $recipient['user_id'], 'status' => MSG_STATUS_UNREAD);
|
||||
}
|
||||
|
||||
$this->_insert_statuses($statuses);
|
||||
}
|
||||
|
||||
$this->db->trans_complete();
|
||||
|
||||
if ($this->db->trans_status() === FALSE)
|
||||
{
|
||||
$this->db->trans_rollback();
|
||||
return $this->_general_error();
|
||||
}
|
||||
|
||||
return $this->_success($msg_id, MSG_MESSAGE_SENT);
|
||||
}
|
||||
|
||||
/** ----------------------------------------------------------------
|
||||
* Get Participant List
|
||||
* get_participant_list() - returns list of participants on given thread. If sender_id set, sender_id will be left off list
|
||||
*
|
||||
* @param integer $thread_id REQUIRED
|
||||
* @param integer $sender_id REQUIRED
|
||||
* @return array
|
||||
*/
|
||||
function get_participant_list($thread_id, $sender_id = 0)
|
||||
{
|
||||
// Validate
|
||||
if (empty($thread_id))
|
||||
{
|
||||
return $this->_invalid_id(MSG_ERR_INVALID_THREAD_ID);
|
||||
}
|
||||
|
||||
if ($results = $this->_get_thread_participants($thread_id, $sender_id))
|
||||
return $this->_success($results);
|
||||
else
|
||||
return $this->_general_error();
|
||||
}
|
||||
|
||||
/** ----------------------------------------------------------------
|
||||
* Get Message Count
|
||||
* get_msg_count() - returns integer with count of message for user, by status. defaults to new messages
|
||||
*
|
||||
* @param integer $user_id REQUIRED
|
||||
* @param integer $status_id OPTIONAL - defaults to "Unread"
|
||||
* @return array
|
||||
*/
|
||||
function get_msg_count($user_id, $status_id = MSG_STATUS_UNREAD)
|
||||
{
|
||||
if (empty($user_id))
|
||||
{
|
||||
return $this->_invalid_id(MSG_ERR_INVALID_USER_ID);
|
||||
}
|
||||
|
||||
$result = $this->db->select('COUNT(*) AS msg_count')->where(array('user_id' => $user_id, 'status' => $status_id ))->get('msg_status');
|
||||
$rows = $result->row()->msg_count;
|
||||
|
||||
if (is_numeric($rows))
|
||||
return $this->_success($rows);
|
||||
else
|
||||
return $this->_general_error();
|
||||
}
|
||||
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Valid New Participant - because of CodeIgniter's DB Class return style,
|
||||
* it is safer to check for uniqueness first
|
||||
*
|
||||
* @param integer $thread_id
|
||||
* @param integer $user_id
|
||||
* @return boolean
|
||||
*/
|
||||
function valid_new_participant($thread_id, $user_id)
|
||||
{
|
||||
$sql = 'SELECT COUNT(*) AS count ' .
|
||||
' FROM ' . $this->db->dbprefix . 'msg_participants p ' .
|
||||
' WHERE p.thread_id = ? ' .
|
||||
' AND p.user_id = ? ';
|
||||
|
||||
$query = $this->db->query($sql, array($thread_id, $user_id));
|
||||
|
||||
if ($query->row()->count)
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/** ---------------------------------------------------------------
|
||||
* Application User
|
||||
*
|
||||
* @param integer $user_id`
|
||||
* @return boolean
|
||||
*/
|
||||
function application_user($user_id)
|
||||
{
|
||||
$sql = 'SELECT COUNT(*) AS count ' .
|
||||
' FROM ' . $this->db->dbprefix . USER_TABLE_TABLENAME .
|
||||
' WHERE ' . USER_TABLE_ID . ' = ?' ;
|
||||
|
||||
$query = $this->db->query($sql, array($user_id));
|
||||
|
||||
if ($query->row()->count)
|
||||
{
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
// Private Functions from here out!
|
||||
// ------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Insert Thread
|
||||
*
|
||||
* @param string $subject
|
||||
* @return integer
|
||||
*/
|
||||
private function _insert_thread($subject)
|
||||
{
|
||||
$insert_id = $this->db->insert('msg_threads', array('subject' => $subject));
|
||||
|
||||
return $this->db->insert_id();
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert Message
|
||||
*
|
||||
* @param integer $thread_id
|
||||
* @param integer $sender_id
|
||||
* @param string $body
|
||||
* @param integer $priority
|
||||
* @return integer
|
||||
*/
|
||||
private function _insert_message($thread_id, $sender_id, $body, $priority)
|
||||
{
|
||||
$insert['thread_id'] = $thread_id;
|
||||
$insert['sender_id'] = $sender_id;
|
||||
$insert['body'] = $body;
|
||||
$insert['priority'] = $priority;
|
||||
|
||||
$insert_id = $this->db->insert('msg_messages', $insert);
|
||||
|
||||
return $this->db->insert_id();
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert Participants
|
||||
*
|
||||
* @param array $participants
|
||||
* @return bool
|
||||
*/
|
||||
private function _insert_participants($participants)
|
||||
{
|
||||
return $this->db->insert_batch('msg_participants', $participants);
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert Statuses
|
||||
*
|
||||
* @param array $statuses
|
||||
* @return bool
|
||||
*/
|
||||
private function _insert_statuses($statuses)
|
||||
{
|
||||
return $this->db->insert_batch('msg_status', $statuses);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Thread ID from Message
|
||||
*
|
||||
* @param integer $msg_id
|
||||
* @return integer
|
||||
*/
|
||||
private function _get_thread_id_from_message($msg_id)
|
||||
{
|
||||
$query = $this->db->select('thread_id')->get_where('msg_messages', array('id' => $msg_id));
|
||||
|
||||
if ($query->num_rows())
|
||||
{
|
||||
return $query->row()->thread_id;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Messages by Thread
|
||||
*
|
||||
* @param integer $thread_id
|
||||
* @return array
|
||||
*/
|
||||
private function _get_messages_by_thread_id($thread_id)
|
||||
{
|
||||
$query = $this->db->get_where('msg_messages', array('thread_id' => $thread_id));
|
||||
|
||||
return $query->result_array();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get Thread Particpiants
|
||||
*
|
||||
* @param integer $thread_id
|
||||
* @param integer $sender_id
|
||||
* @return array
|
||||
*/
|
||||
private function _get_thread_participants($thread_id, $sender_id = 0)
|
||||
{
|
||||
$array['thread_id'] = $thread_id;
|
||||
|
||||
if ($sender_id) // If $sender_id 0, no one to exclude
|
||||
{
|
||||
$array['msg_participants.user_id != '] = $sender_id;
|
||||
}
|
||||
|
||||
$this->db->select('msg_participants.user_id, '.USER_TABLE_USERNAME, FALSE);
|
||||
$this->db->join(USER_TABLE_TABLENAME, 'msg_participants.user_id = ' . USER_TABLE_ID);
|
||||
|
||||
$query = $this->db->get_where('msg_participants', $array);
|
||||
|
||||
return $query->result_array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete Participant
|
||||
*
|
||||
* @param integer $thread_id
|
||||
* @param integer $user_id
|
||||
* @return boolean
|
||||
*/
|
||||
private function _delete_participant($thread_id, $user_id)
|
||||
{
|
||||
$this->db->delete('msg_participants', array('thread_id' => $thread_id, 'user_id' => $user_id));
|
||||
|
||||
if ($this->db->affected_rows() > 0)
|
||||
{
|
||||
return TRUE;
|
||||
}
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete Statuses
|
||||
*
|
||||
* @param integer $thread_id
|
||||
* @param integer $user_id
|
||||
* @return boolean
|
||||
*/
|
||||
private function _delete_statuses($thread_id, $user_id)
|
||||
{
|
||||
$sql = 'DELETE s FROM msg_status s ' .
|
||||
' JOIN ' . $this->db->dbprefix . 'msg_messages m ON (m.id = s.message_id) ' .
|
||||
' WHERE m.thread_id = ? ' .
|
||||
' AND s.user_id = ? ';
|
||||
|
||||
$query = $this->db->query($sql, array($thread_id, $user_id));
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
/** ---------------------------------------------------------------
|
||||
* Error Particpant Exists
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function _participant_error($error = '')
|
||||
{
|
||||
return array(
|
||||
'err' => 1,
|
||||
'code' => 1,
|
||||
'msg' => lang('mahana_' . $error)
|
||||
);
|
||||
}
|
||||
}
|
||||
/* end of file message_model.php */
|
||||
@@ -0,0 +1,511 @@
|
||||
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
|
||||
|
||||
class Message_model extends DB_Model
|
||||
{
|
||||
/**
|
||||
* Send a New Message
|
||||
*
|
||||
* @param integer $sender_id
|
||||
* @param mixed $recipients A single integer or an array of integers
|
||||
* @param string $subject
|
||||
* @param string $body
|
||||
* @param integer $priority
|
||||
* @return integer $new_thread_id
|
||||
*/
|
||||
function send_new_message($sender_id, $recipients, $subject, $body, $priority)
|
||||
{
|
||||
$this->db->trans_start();
|
||||
|
||||
$thread_id = $this->_insert_thread($subject);
|
||||
$msg_id = $this->_insert_message($thread_id, $sender_id, $body, $priority);
|
||||
|
||||
// Create batch inserts
|
||||
$participants[] = array('thread_id' => $thread_id,'user_id' => $sender_id);
|
||||
$statuses[] = array('message_id' => $msg_id, 'user_id' => $sender_id,'status' => MSG_STATUS_READ);
|
||||
|
||||
if ( ! is_array($recipients))
|
||||
{
|
||||
$participants[] = array('thread_id' => $thread_id,'user_id' => $recipients);
|
||||
$statuses[] = array('message_id' => $msg_id, 'user_id' => $recipients, 'status' => MSG_STATUS_UNREAD);
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach ($recipients as $recipient)
|
||||
{
|
||||
$participants[] = array('thread_id' => $thread_id,'user_id' => $recipient);
|
||||
$statuses[] = array('message_id' => $msg_id, 'user_id' => $recipient, 'status' => MSG_STATUS_UNREAD);
|
||||
}
|
||||
}
|
||||
|
||||
$this->_insert_participants($participants);
|
||||
$this->_insert_statuses($statuses);
|
||||
|
||||
$this->db->trans_complete();
|
||||
|
||||
if ($this->db->trans_status() === FALSE)
|
||||
{
|
||||
$this->db->trans_rollback();
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
return $thread_id;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Reply to Message
|
||||
*
|
||||
* @param integer $reply_msg_id
|
||||
* @param integer $sender_id
|
||||
* @param string $body
|
||||
* @param integer $priority
|
||||
* @return integer $new_msg_id
|
||||
*/
|
||||
function reply_to_message($reply_msg_id, $sender_id, $body, $priority)
|
||||
{
|
||||
$this->db->trans_start();
|
||||
|
||||
// Get the thread id to keep messages together
|
||||
if ( ! $thread_id = $this->_get_thread_id_from_message($reply_msg_id))
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
// Add this message
|
||||
$msg_id = $this->_insert_message($thread_id, $sender_id, $body, $priority);
|
||||
|
||||
if ($recipients = $this->_get_thread_participants($thread_id, $sender_id))
|
||||
{
|
||||
$statuses[] = array('message_id' => $msg_id, 'user_id' => $sender_id,'status' => MSG_STATUS_READ);
|
||||
|
||||
foreach ($recipients as $recipient)
|
||||
{
|
||||
$statuses[] = array('message_id' => $msg_id, 'user_id' => $recipient['user_id'], 'status' => MSG_STATUS_UNREAD);
|
||||
}
|
||||
|
||||
$this->_insert_statuses($statuses);
|
||||
}
|
||||
|
||||
$this->db->trans_complete();
|
||||
|
||||
if ($this->db->trans_status() === FALSE)
|
||||
{
|
||||
$this->db->trans_rollback();
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
return $msg_id;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Get a Single Message
|
||||
*
|
||||
* @param integer $msg_id
|
||||
* @param integer $user_id
|
||||
* @return array
|
||||
*/
|
||||
function get_message($msg_id, $user_id)
|
||||
{
|
||||
$sql = 'SELECT m.*, s.status, t.subject, ' . USER_TABLE_USERNAME .
|
||||
' FROM ' . $this->db->dbprefix . 'msg_messages m ' .
|
||||
' JOIN ' . $this->db->dbprefix . 'msg_threads t ON (m.thread_id = t.id) ' .
|
||||
' JOIN ' . $this->db->dbprefix . USER_TABLE_TABLENAME . ' ON (' . USER_TABLE_ID . ' = m.sender_id) '.
|
||||
' JOIN ' . $this->db->dbprefix . 'msg_status s ON (s.message_id = m.id AND s.user_id = ? ) ' .
|
||||
' WHERE m.id = ? ' ;
|
||||
|
||||
$query = $this->db->query($sql, array($user_id, $msg_id));
|
||||
|
||||
return $query->result_array();
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Get a Full Thread
|
||||
*
|
||||
* @param integer $thread_id
|
||||
* @param integer $user_id
|
||||
* @param boolean $full_thread
|
||||
* @param string $order_by
|
||||
* @return array
|
||||
*/
|
||||
function get_full_thread($thread_id, $user_id, $full_thread = FALSE, $order_by = 'asc')
|
||||
{
|
||||
$sql = 'SELECT m.*, s.status, t.subject, '.USER_TABLE_USERNAME .
|
||||
' FROM ' . $this->db->dbprefix . 'msg_participants p ' .
|
||||
' JOIN ' . $this->db->dbprefix . 'msg_threads t ON (t.id = p.thread_id) ' .
|
||||
' JOIN ' . $this->db->dbprefix . 'msg_messages m ON (m.thread_id = t.id) ' .
|
||||
' JOIN ' . $this->db->dbprefix . USER_TABLE_TABLENAME . ' ON (' . USER_TABLE_ID . ' = m.sender_id) '.
|
||||
' JOIN ' . $this->db->dbprefix . 'msg_status s ON (s.message_id = m.id AND s.user_id = ? ) ' .
|
||||
' WHERE p.user_id = ? ' .
|
||||
' AND p.thread_id = ? ';
|
||||
|
||||
if ( ! $full_thread)
|
||||
{
|
||||
$sql .= ' AND m.cdate >= p.cdate';
|
||||
}
|
||||
|
||||
$sql .= ' ORDER BY m.cdate ' . $order_by;
|
||||
|
||||
$query = $this->db->query($sql, array($user_id, $user_id, $thread_id));
|
||||
|
||||
return $query->result_array();
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Get All Threads
|
||||
*
|
||||
* @param integer $user_id
|
||||
* @param boolean $full_thread
|
||||
* @param string $order_by
|
||||
* @return array
|
||||
*/
|
||||
function get_all_threads($user_id, $full_thread = FALSE, $order_by = 'asc')
|
||||
{
|
||||
$sql = 'SELECT m.*, s.status, t.subject, '.USER_TABLE_USERNAME .
|
||||
' FROM ' . $this->db->dbprefix . 'msg_participants p ' .
|
||||
' JOIN ' . $this->db->dbprefix . 'msg_threads t ON (t.id = p.thread_id) ' .
|
||||
' JOIN ' . $this->db->dbprefix . 'msg_messages m ON (m.thread_id = t.id) ' .
|
||||
' JOIN ' . $this->db->dbprefix . USER_TABLE_TABLENAME . ' ON (' . USER_TABLE_ID . ' = m.sender_id) '.
|
||||
' JOIN ' . $this->db->dbprefix . 'msg_status s ON (s.message_id = m.id AND s.user_id = ? ) ' .
|
||||
' WHERE p.user_id = ? ' ;
|
||||
|
||||
if (!$full_thread)
|
||||
{
|
||||
$sql .= ' AND m.cdate >= p.cdate';
|
||||
}
|
||||
|
||||
$sql .= ' ORDER BY t.id ' . $order_by. ', m.cdate '. $order_by;
|
||||
|
||||
$query = $this->db->query($sql, array($user_id, $user_id));
|
||||
|
||||
return $query->result_array();
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Change Message Status
|
||||
*
|
||||
* @param integer $msg_id
|
||||
* @param integer $user_id
|
||||
* @param integer $status_id
|
||||
* @return integer
|
||||
*/
|
||||
function update_message_status($msg_id, $user_id, $status_id)
|
||||
{
|
||||
$this->db->where(array('message_id' => $msg_id, 'user_id' => $user_id ));
|
||||
$this->db->update('msg_status', array('status' => $status_id ));
|
||||
|
||||
return $this->db->affected_rows();
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Add a Participant
|
||||
*
|
||||
* @param integer $thread_id
|
||||
* @param integer $user_id
|
||||
* @return boolean
|
||||
*/
|
||||
function add_participant($thread_id, $user_id)
|
||||
{
|
||||
$this->db->trans_start();
|
||||
|
||||
$participants[] = array('thread_id' => $thread_id,'user_id' => $user_id);
|
||||
|
||||
$this->_insert_participants($participants);
|
||||
|
||||
// Get Messages by Thread
|
||||
$messages = $this->_get_messages_by_thread_id($thread_id);
|
||||
|
||||
foreach ($messages as $message)
|
||||
{
|
||||
$statuses[] = array('message_id' => $message['id'], 'user_id' => $user_id, 'status' => MSG_STATUS_UNREAD);
|
||||
}
|
||||
|
||||
$this->_insert_statuses($statuses);
|
||||
|
||||
$this->db->trans_complete();
|
||||
|
||||
if ($this->db->trans_status() === FALSE)
|
||||
{
|
||||
$this->db->trans_rollback();
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Remove a Participant
|
||||
*
|
||||
* @param integer $thread_id
|
||||
* @param integer $user_id
|
||||
* @return boolean
|
||||
*/
|
||||
function remove_participant($thread_id, $user_id)
|
||||
{
|
||||
$this->db->trans_start();
|
||||
|
||||
$this->_delete_participant($thread_id, $user_id);
|
||||
$this->_delete_statuses($thread_id, $user_id);
|
||||
|
||||
$this->db->trans_complete();
|
||||
|
||||
if ($this->db->trans_status() === FALSE)
|
||||
{
|
||||
$this->db->trans_rollback();
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Valid New Participant - because of CodeIgniter's DB Class return style,
|
||||
* it is safer to check for uniqueness first
|
||||
*
|
||||
* @param integer $thread_id
|
||||
* @param integer $user_id
|
||||
* @return boolean
|
||||
*/
|
||||
function valid_new_participant($thread_id, $user_id)
|
||||
{
|
||||
$sql = 'SELECT COUNT(*) AS count ' .
|
||||
' FROM ' . $this->db->dbprefix . 'msg_participants p ' .
|
||||
' WHERE p.thread_id = ? ' .
|
||||
' AND p.user_id = ? ';
|
||||
|
||||
$query = $this->db->query($sql, array($thread_id, $user_id));
|
||||
|
||||
if ($query->row()->count)
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Application User
|
||||
*
|
||||
* @param integer $user_id`
|
||||
* @return boolean
|
||||
*/
|
||||
function application_user($user_id)
|
||||
{
|
||||
$sql = 'SELECT COUNT(*) AS count ' .
|
||||
' FROM ' . $this->db->dbprefix . USER_TABLE_TABLENAME .
|
||||
' WHERE ' . USER_TABLE_ID . ' = ?' ;
|
||||
|
||||
$query = $this->db->query($sql, array($user_id));
|
||||
|
||||
if ($query->row()->count)
|
||||
{
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Get Participant List
|
||||
*
|
||||
* @param integer $thread_id
|
||||
* @param integer $sender_id
|
||||
* @return mixed
|
||||
*/
|
||||
function get_participant_list($thread_id, $sender_id = 0)
|
||||
{
|
||||
if ($results = $this->_get_thread_participants($thread_id, $sender_id))
|
||||
{
|
||||
return $results;
|
||||
}
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Get Message Count
|
||||
*
|
||||
* @param integer $user_id
|
||||
* @param integer $status_id
|
||||
* @return integer
|
||||
*/
|
||||
function get_msg_count($user_id, $status_id = MSG_STATUS_UNREAD)
|
||||
{
|
||||
$query = $this->db->select('COUNT(*) AS msg_count')->where(array('user_id' => $user_id, 'status' => $status_id ))->get('msg_status');
|
||||
|
||||
return $query->row()->msg_count;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
// Private Functions from here out!
|
||||
// ------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Insert Thread
|
||||
*
|
||||
* @param string $subject
|
||||
* @return integer
|
||||
*/
|
||||
private function _insert_thread($subject)
|
||||
{
|
||||
$insert_id = $this->db->insert('msg_threads', array('subject' => $subject));
|
||||
|
||||
return $this->db->insert_id();
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert Message
|
||||
*
|
||||
* @param integer $thread_id
|
||||
* @param integer $sender_id
|
||||
* @param string $body
|
||||
* @param integer $priority
|
||||
* @return integer
|
||||
*/
|
||||
private function _insert_message($thread_id, $sender_id, $body, $priority)
|
||||
{
|
||||
$insert['thread_id'] = $thread_id;
|
||||
$insert['sender_id'] = $sender_id;
|
||||
$insert['body'] = $body;
|
||||
$insert['priority'] = $priority;
|
||||
|
||||
$insert_id = $this->db->insert('msg_messages', $insert);
|
||||
|
||||
return $this->db->insert_id();
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert Participants
|
||||
*
|
||||
* @param array $participants
|
||||
* @return bool
|
||||
*/
|
||||
private function _insert_participants($participants)
|
||||
{
|
||||
return $this->db->insert_batch('msg_participants', $participants);
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert Statuses
|
||||
*
|
||||
* @param array $statuses
|
||||
* @return bool
|
||||
*/
|
||||
private function _insert_statuses($statuses)
|
||||
{
|
||||
return $this->db->insert_batch('msg_status', $statuses);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Thread ID from Message
|
||||
*
|
||||
* @param integer $msg_id
|
||||
* @return integer
|
||||
*/
|
||||
private function _get_thread_id_from_message($msg_id)
|
||||
{
|
||||
$query = $this->db->select('thread_id')->get_where('msg_messages', array('id' => $msg_id));
|
||||
|
||||
if ($query->num_rows())
|
||||
{
|
||||
return $query->row()->thread_id;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Messages by Thread
|
||||
*
|
||||
* @param integer $thread_id
|
||||
* @return array
|
||||
*/
|
||||
private function _get_messages_by_thread_id($thread_id)
|
||||
{
|
||||
$query = $this->db->get_where('msg_messages', array('thread_id' => $thread_id));
|
||||
|
||||
return $query->result_array();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get Thread Particpiants
|
||||
*
|
||||
* @param integer $thread_id
|
||||
* @param integer $sender_id
|
||||
* @return array
|
||||
*/
|
||||
private function _get_thread_participants($thread_id, $sender_id = 0)
|
||||
{
|
||||
$array['thread_id'] = $thread_id;
|
||||
|
||||
if ($sender_id) // If $sender_id 0, no one to exclude
|
||||
{
|
||||
$array['msg_participants.user_id != '] = $sender_id;
|
||||
}
|
||||
|
||||
$this->db->select('msg_participants.user_id, '.USER_TABLE_USERNAME, FALSE);
|
||||
$this->db->join(USER_TABLE_TABLENAME, 'msg_participants.user_id = ' . USER_TABLE_ID);
|
||||
|
||||
$query = $this->db->get_where('msg_participants', $array);
|
||||
|
||||
return $query->result_array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete Participant
|
||||
*
|
||||
* @param integer $thread_id
|
||||
* @param integer $user_id
|
||||
* @return boolean
|
||||
*/
|
||||
private function _delete_participant($thread_id, $user_id)
|
||||
{
|
||||
$this->db->delete('msg_participants', array('thread_id' => $thread_id, 'user_id' => $user_id));
|
||||
|
||||
if ($this->db->affected_rows() > 0)
|
||||
{
|
||||
return TRUE;
|
||||
}
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete Statuses
|
||||
*
|
||||
* @param integer $thread_id
|
||||
* @param integer $user_id
|
||||
* @return boolean
|
||||
*/
|
||||
private function _delete_statuses($thread_id, $user_id)
|
||||
{
|
||||
$sql = 'DELETE s FROM msg_status s ' .
|
||||
' JOIN ' . $this->db->dbprefix . 'msg_messages m ON (m.id = s.message_id) ' .
|
||||
' WHERE m.thread_id = ? ' .
|
||||
' AND s.user_id = ? ';
|
||||
|
||||
$query = $this->db->query($sql, array($thread_id, $user_id));
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
}
|
||||
|
||||
/* end of file mahana_model.php */
|
||||
@@ -2,23 +2,21 @@
|
||||
|
||||
class Person_model extends DB_Model
|
||||
{
|
||||
|
||||
public function __construct($uid = null)
|
||||
{
|
||||
parent::__construct($uid);
|
||||
$this->dbTable = 'public.tbl_person';
|
||||
parent::__construct($uid);
|
||||
$this->dbTable = 'public.tbl_person';
|
||||
}
|
||||
|
||||
public function getPerson($person_id = null)
|
||||
{
|
||||
if (is_null($person_id))
|
||||
{
|
||||
$query = $this->db->get_where('public.tbl_person', array());
|
||||
return $query->result_object();
|
||||
}
|
||||
|
||||
$query = $this->db->get_where('public.tbl_person', array('person_id' => $person_id));
|
||||
return $query->row_object();
|
||||
if (is_null($person_id))
|
||||
{
|
||||
$query = $this->db->get_where('public.tbl_person', array());
|
||||
return $query->result_object();
|
||||
}
|
||||
$query = $this->db->get_where('public.tbl_person', array('person_id' => $person_id));
|
||||
return $query->row_object();
|
||||
}
|
||||
|
||||
public function getPersonByCodeAndEmail($code, $email)
|
||||
@@ -44,19 +42,20 @@ class Person_model extends DB_Model
|
||||
*/
|
||||
public function getPersonFromBenutzerUID($uid)
|
||||
{
|
||||
if (!$this->fhc_db_acl->bb->isBerechtigt('person', 's'))
|
||||
{
|
||||
$this->db->select('tbl_person.*');
|
||||
$this->db->from('public.tbl_person JOIN public.tbl_benutzer USING (person_id)');
|
||||
$query = $this->db->get_where(null, array('uid' => $uid));
|
||||
return $query->result_object();
|
||||
}
|
||||
|
||||
if (!$this->fhc_db_acl->bb->isBerechtigt('person', 's'))
|
||||
{
|
||||
$this->db->select('tbl_person.*');
|
||||
$this->db->from('public.tbl_person JOIN public.tbl_benutzer USING (person_id)');
|
||||
$query = $this->db->get_where(null, array('uid' => $uid));
|
||||
return $query->result_object();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public function savePerson($person)
|
||||
{
|
||||
//TODO check berechtigung
|
||||
// if ($this->fhc_db_acl->bb->isBerechtigt('person', 'sui'))
|
||||
// if($this->fhc_db_acl->bb->isBerechtigt('person', 'sui'))
|
||||
// {
|
||||
$data = array(
|
||||
"vorname"=>$person["vorname"],
|
||||
@@ -69,11 +68,11 @@ class Person_model extends DB_Model
|
||||
"insertvon"=>$person["insertvon"],
|
||||
);
|
||||
if($this->db->insert("public.tbl_person", $data)){
|
||||
return $this->db->insert_id();
|
||||
return $this->db->insert_id();
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
return false;
|
||||
}
|
||||
// }
|
||||
// else
|
||||
@@ -84,64 +83,64 @@ class Person_model extends DB_Model
|
||||
|
||||
public function checkBewerbung($email, $studiensemester_kurzbz=NULL)
|
||||
{
|
||||
$this->db->distinct();
|
||||
|
||||
if(is_null($studiensemester_kurzbz))
|
||||
{
|
||||
$this->db->select("p.person_id, p.zugangscode, p.insertamum")
|
||||
->from("public.tbl_person p")
|
||||
->join("public.tbl_kontakt k", "p.person_id=k.person_id")
|
||||
->join("public.tbl_benutzer b", "p.person_id=b.person_id", "left")
|
||||
->where("k.kontakttyp", 'email')
|
||||
->where("(kontakt='".$email."'".
|
||||
" OR alias ||'@technikum-wien.at'='".$email."'".
|
||||
" OR uid ||'@technikum-wien.at'='".$email."')")
|
||||
->order_by("p.insertamum", "DESC")
|
||||
->limit(1)
|
||||
;
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->db->select("p.person_id,p.zugangscode,p.insertamum")
|
||||
->from("public.tbl_person p")
|
||||
->join("public.tbl_kontakt k", "p.person_id=k.person_id")
|
||||
->join("public.tbl_benutzer b", "p.person_id=b.person_id", "left")
|
||||
->join("public.tbl_prestudent ps", "p.person_id=ps.person_id")
|
||||
->join("public.tbl_prestudentstatus pst", "pst.prestudent_id=ps.prestudent_id")
|
||||
->where("k.kontakttyp", 'email')
|
||||
->where("(kontakt='".$email."'".
|
||||
" OR alias ||'@technikum-wien.at'='".$email."'".
|
||||
" OR uid ||'@technikum-wien.at'='".$email."')")
|
||||
->where("studiensemester_kurzbz='".$studiensemester_kurzbz."'")
|
||||
->order_by("p.insertamum", "DESC")
|
||||
->limit(1)
|
||||
;
|
||||
}
|
||||
return $this->db->get()->result_array();
|
||||
$this->db->distinct();
|
||||
|
||||
if(is_null($studiensemester_kurzbz))
|
||||
{
|
||||
$this->db->select("p.person_id, p.zugangscode, p.insertamum")
|
||||
->from("public.tbl_person p")
|
||||
->join("public.tbl_kontakt k", "p.person_id=k.person_id")
|
||||
->join("public.tbl_benutzer b", "p.person_id=b.person_id", "left")
|
||||
->where("k.kontakttyp", 'email')
|
||||
->where("(kontakt='".$email."'".
|
||||
" OR alias ||'@technikum-wien.at'='".$email."'".
|
||||
" OR uid ||'@technikum-wien.at'='".$email."')")
|
||||
->order_by("p.insertamum", "DESC")
|
||||
->limit(1)
|
||||
;
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->db->select("p.person_id,p.zugangscode,p.insertamum")
|
||||
->from("public.tbl_person p")
|
||||
->join("public.tbl_kontakt k", "p.person_id=k.person_id")
|
||||
->join("public.tbl_benutzer b", "p.person_id=b.person_id", "left")
|
||||
->join("public.tbl_prestudent ps", "p.person_id=ps.person_id")
|
||||
->join("public.tbl_prestudentstatus pst", "pst.prestudent_id=ps.prestudent_id")
|
||||
->where("k.kontakttyp", 'email')
|
||||
->where("(kontakt='".$email."'".
|
||||
" OR alias ||'@technikum-wien.at'='".$email."'".
|
||||
" OR uid ||'@technikum-wien.at'='".$email."')")
|
||||
->where("studiensemester_kurzbz='".$studiensemester_kurzbz."'")
|
||||
->order_by("p.insertamum", "DESC")
|
||||
->limit(1)
|
||||
;
|
||||
}
|
||||
return $this->db->get()->result_array();
|
||||
}
|
||||
|
||||
|
||||
public function checkZugangscodePerson($code)
|
||||
{
|
||||
$this->db->select("p.person_id")
|
||||
->from("public.tbl_person p")
|
||||
->where("p.zugangscode", $code);
|
||||
return $this->db->get()->result_array();
|
||||
$this->db->select("p.person_id")
|
||||
->from("public.tbl_person p")
|
||||
->where("p.zugangscode", $code);
|
||||
return $this->db->get()->result_array();
|
||||
}
|
||||
|
||||
|
||||
public function updatePerson($person)
|
||||
{
|
||||
//TODO check berechtigung
|
||||
// if ($this->fhc_db_acl->bb->isBerechtigt('person', 'sui'))
|
||||
// if($this->fhc_db_acl->bb->isBerechtigt('person', 'sui'))
|
||||
// {
|
||||
//TODO set other columns to be updated
|
||||
$this->db->set("zugangscode", $person["zugangscode"]);
|
||||
$this->db->where("person_id", $person["person_id"]);
|
||||
if($this->db->update("public.tbl_person")){
|
||||
return true;
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
return false;
|
||||
}
|
||||
// }
|
||||
// else
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
defined('BASEPATH') OR exit('No direct script access allowed');
|
||||
|
||||
class Seed_Person
|
||||
{
|
||||
|
||||
public function seed($limit = 25)
|
||||
{
|
||||
echo "Seeding $limit persons ";
|
||||
$this->fhc =& get_instance();
|
||||
$this->fhc->load->model('person/Person_model');
|
||||
|
||||
for ($i = 0; $i < $limit; $i++)
|
||||
{
|
||||
echo ".";
|
||||
$data = array(
|
||||
// 'username' => $this->faker->unique()->userName, // get a unique nickname
|
||||
'vorname' => $this->fhc->faker->firstName,
|
||||
'vornamen' => $this->fhc->faker->firstName,
|
||||
'nachname' => $this->fhc->faker->lastName,
|
||||
//'address' => $this->faker->streetAddress,
|
||||
'gebort' => $this->fhc->faker->city,
|
||||
//'state' => $this->faker->state,
|
||||
//'country' => $this->faker->country,
|
||||
//'postcode' => $this->faker->postcode,
|
||||
//'email' => $this->faker->email,
|
||||
//'email_verified' => mt_rand(0, 1) ? '0' : '1',
|
||||
//'phone' => $this->faker->phoneNumber,
|
||||
'gebdatum' => $this->fhc->faker->dateTimeThisCentury->format('Y-m-d H:i:s'),
|
||||
//'registration_date' => $this->faker->dateTimeThisYear->format('Y-m-d H:i:s'),
|
||||
//'ip_address' => mt_rand(0, 1) ? $this->faker->ipv4 : $this->faker->ipv6,
|
||||
);
|
||||
|
||||
$this->fhc->Person_model->insert($data);
|
||||
}
|
||||
|
||||
echo PHP_EOL;
|
||||
|
||||
}
|
||||
|
||||
public function truncate()
|
||||
{
|
||||
//$this->db->query('EMPTY TABLE public.person;');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
<frameset rows="55px,*" frameborder="0" framespacing="0">
|
||||
<frameset id="frameset-vilesci" rows="55px,*" frameborder="0" framespacing="0">
|
||||
<frame src="<?php echo base_url('vilesci/top.php')?>" id="top" name="top" scrolling="No"/>
|
||||
<frameset border="4" frameborder="1" framespacing="0" cols="200px,*" >
|
||||
<frame style="border-right: 3px; border-right-style:solid; border-right-color: grey;" src="<?php echo base_url('vilesci/left.php')?>" id="nav" name="nav" />
|
||||
|
||||
Reference in New Issue
Block a user