Created
September 24, 2012 01:16
-
-
Save necenzurat/3773705 to your computer and use it in GitHub Desktop.
mta_model.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/*in acest fisier sunt funciile generale | |
*mta_model extinde fisierul predefinit al codeigniterului ci_model | |
*/ | |
class mta_Model extends CI_Model { | |
var $table = false; | |
var $list_fields = ""; | |
function load_form_validation() { | |
$this->load->library('form_validation'); | |
$this->form_validation->set_message('required', 'Campul %s este necesar!'); | |
$this->form_validation->set_message('matches', 'Campul %s trebuie sa fie identic cu campul %s!'); | |
$this->form_validation->set_message('is_unique', 'Exista deja acest %s in baza de date!'); | |
$this->form_validation->set_message('greater_than', 'Campul %s trebuie sa fie mai mare decat %s!'); | |
$this->form_validation->set_message('is_natural_no_zero', 'Campul %s trebuie selectat!'); | |
} | |
/** | |
* Function to handle the insertion in the database of new values | |
* @param string $table | |
* @param array $fields | |
* @param array $data | |
*/ | |
function insert($data = array()) { | |
if(empty($this->table) || empty($data)){ | |
$this->session->set_flashdata('admins_message_type', 'error'); | |
$this->session->set_flashdata('admins_message_content', 'A aparut o eroare la procesarea datelor'); | |
return false; | |
} else { | |
$data['created'] = date('Y-m-d H:i:s'); | |
$this->db | |
->insert($this->table, $data); | |
$this->session->set_flashdata('admins_message_type', 'success'); | |
$this->session->set_flashdata('admins_message_content', 'Obiectul a fost introdus cu succes'); | |
} | |
} | |
/*Verifică dacă variabila list_fields este goală, | |
dacă este atunci se afișează mesajul de eroare, | |
dacă nu, se extrag din baza de date câmpurile din list_fields și se ordonează după id ascendent. | |
*/ | |
function listing($order = "sortare asc") { | |
if(!empty($this->list_fields)){ | |
$this->db | |
->from($this->table) | |
->select($this->list_fields) | |
->order_by($order); | |
$q = $this->db->get(); | |
$result = $q->result(); | |
$q->free_result(); | |
if(!empty($result)) { | |
return $result; | |
} else { | |
return false; | |
} | |
} else { | |
$this->session->set_flashdata('admins_message_type', 'error'); | |
$this->session->set_flashdata('admins_message_content', 'Exista o eroare la afisarea rezultatelor'); | |
return false; | |
} | |
} | |
function delete($id = 0) { | |
if(!empty($id)) { | |
$this->db | |
->where('id', $id) | |
->delete($this->table); | |
$this->session->set_flashdata('admins_message_type', 'success'); | |
$this->session->set_flashdata('admins_message_content', 'Obiectul a fost sters din baza de date!'); | |
} else { | |
$this->session->set_flashdata('admins_message_type', 'error'); | |
$this->session->set_flashdata('admins_message_content', 'Obiectul nu a putut fi sters sau nu exista.'); | |
} | |
} | |
function get_edit($id) { | |
if(!empty($id)) { | |
$this->db | |
->select('*') | |
->from($this->table) | |
->where('id', $id) | |
->limit(1); | |
$q = $this->db->get(); | |
$row=$q->row(); | |
$q->free_result(); | |
return $row; | |
} else { | |
return false; | |
} | |
} | |
function edit($id = 0, $data = array()){ | |
if(!empty($id) && !empty($data)){ | |
$this->db | |
->where('id', $id) | |
->update($this->table, $data); | |
$this->session->set_flashdata('admins_message_type', 'success'); | |
$this->session->set_flashdata('admins_message_content', 'Datele au fost salvate cu succes!'); | |
} else { | |
$this->session->set_flashdata('admins_message_type', 'error'); | |
$this->session->set_flashdata('admins_message_content', 'Obiectul nu a putut fi editat sau nu exista.'); | |
} | |
} | |
/*Extrage toate datele din baza de date, | |
spre deosebire de functia listing care poate extrage anumite campuri din baza de date | |
*/ | |
function get_all() { | |
$this->db | |
->select('*') | |
->from($this->table); | |
$q = $this->db->get(); | |
$result = $q->result(); | |
$q->free_result(); | |
return $result; | |
} | |
function get_by_id($id){ | |
$this->db | |
->select('*') | |
->from($this->table) | |
->where('id', $id); | |
$q = $this->db->get(); | |
$row = $q->row(); | |
$q->free_result(); | |
return $row; | |
} | |
function sort_by($sort){ | |
$this->db | |
->select('*') | |
->from($this->table) | |
->order_by("sort", "asc"); | |
$q = $this->db->get(); | |
$result = $q->result(); | |
$q->free_result(); | |
return $result; | |
} | |
/*Functia verifica numarul intrarilor dintr-un tabel si este folosita pentru paginare. | |
*/ | |
function get_count() { | |
$this->db | |
->select('COUNT(*) AS cnt') | |
->from($this->table); | |
$q = $this->db->get(); | |
$row = $q->row(); | |
$q->free_result(); | |
return $row->cnt; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment