mirror of
https://github.com/FH-Complete/FHC-Core.git
synced 2026-07-24 10:22:18 +00:00
Merge branch 'ci' of https://github.com/FH-Complete/FHC-Core into ci
This commit is contained in:
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
|
||||
|
||||
Reference in New Issue
Block a user