Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Pages controller
- [code]
- <?php if(!defined('BASEPATH')) exit ('No direct script access allowed');
- class Pages extends MX_Controller{
- public function __construct() {
- parent::__construct();
- $this->load->model('pages_model');
- $this->load->library('form_validation');
- }
- function manage(){
- $data['pages'] = $this->pages_model->get();
- $data['view_file'] = "manage";
- $this->load->module('templates');
- $this->templates->admin($data);
- }
- public function edit($id = NULL){
- if($id){
- $data['page'] = $this->pages_model->get($id);
- count($data['page']) || $data['errors'][] = 'User could not be found';
- } else {
- $data['page'] = $this->pages_model->get_new();
- }
- //pages for dropdown
- $data['pages_no_parents'] = $this->pages_model->get_no_parents();
- dump($data['pages_no_parents']);
- //$id == NULL || $data['page'] = $this->pages_model->get($id);
- $rules = $this->pages_model->rules;
- $this->form_validation->set_rules($rules);
- if($this->form_validation->run() == TRUE){
- $data = $this->pages_model->array_from_post(array('title', 'slug', 'body'));
- $this->pages_model->save($data, $id);
- redirect('pages/manage');
- }
- $data['view_file'] = "edit";
- $this->load->module('templates');
- $this->templates->admin($data);
- }
- public function _unique_slug($str){
- $id = $this->uri->segment(3);
- $this->db->where('slug', $this->input->post('slug'));
- !$id || $this->db->where('id !=', $id);
- $page = $this->pages_model->get();
- if(count($page)){
- $this->form_validation->set_message('_unique_slug', '%s should be unique');
- return FALSE;
- }
- return TRUE;
- }
- public function delete($id){
- $this->pages_model->delete($id);
- redirect('pages/manage');
- }
- function get($order_by){
- $this->load->model('pages_model');
- $query = $this->pages_model->get($order_by);
- return $query;
- }
- function get_with_limit($limit, $offset, $order_by) {
- $this->load->model('pages_model');
- $query = $this->pages_model->get_with_limit($limit, $offset, $order_by);
- return $query;
- }
- function get_where($id){
- $this->load->model('pages_model');
- $query = $this->pages_model->get_where($id);
- return $query;
- }
- function get_where_custom($col, $value) {
- $this->load->model('pages_model');
- $query = $this->pages_model->get_where_custom($col, $value);
- return $query;
- }
- function _insert($data){
- $this->load->model('pages_model');
- $this->pages_model->_insert($data);
- }
- function _update($id, $data){
- $this->load->model('pages_model');
- $this->pages_model->_update($id, $data);
- }
- function _delete($id){
- $this->load->model('pages_model');
- $this->pages_model->_delete($id);
- }
- function count_where($column, $value) {
- $this->load->model('pages_model');
- $count = $this->pages_model->count_where($column, $value);
- return $count;
- }
- function get_max() {
- $this->load->model('pages_model');
- $max_id = $this->pages_model->get_max();
- return $max_id;
- }
- function _custom_query($mysql_query) {
- $this->load->model('pages_model');
- $query = $this->pages_model->_custom_query($mysql_query);
- return $query;
- }
- }
- [/code]
- and this is my pages_model
- [code]
- <?php if(!defined('BASEPATH')) exit ('No direct script access allowed');
- class Pages_model extends CI_Model {
- protected $_table_name = "pages";
- protected $_primary_key = "id";
- protected $_primary_filter = "intval";
- protected $_order_by = "order";
- public $rules = array(
- 'parent_id' => array(
- 'field' => 'parent_id',
- 'label' => 'Parnet',
- 'rules' => 'trim|intval'
- ),
- 'slug' => array(
- 'field' => 'slug',
- 'label' => 'Slug',
- 'rules' => 'trim|required|max_length[100]|url_title|callback__unique_slug|xss_clean'
- ),
- 'order' => array(
- 'field' => 'order',
- 'label' => 'Order',
- 'rules' => 'trim|is_natural|xss_clean'
- ),
- 'body' => array(
- 'field' => 'body',
- 'label' => 'Body',
- 'rules' => 'trim|required'
- ),
- );
- protected $_timestamps = "FALSE";
- public function __construct() {
- parent::__construct();
- }
- public function get($id = NULL, $single = FALSE){
- if($id != NULL){
- $filter = $this->_primary_filter;
- $id = $filter($id);
- $this->db->where($this->_primary_key, $id);
- $method = 'row';
- } elseif($single == TRUE) {
- $method = 'row';
- } else {
- $method = 'result';
- }
- return $this->db->get($this->_table_name)->$method();
- }
- public function get_new(){
- $page = new stdClass();
- $page->title = '';
- $page->slug = '';
- $page->body = '';
- $page->parent_id = 0;
- return $page;
- }
- public function get_by($where, $single = FALSE){
- $this->db->where($where);
- return $this->get(NULL, $single);
- }
- public function save($data, $id = NULL){
- //Insert
- if($id === NULL){
- !isset($data[$this->_primary_key]) || $data[$this->_primary_key] = NULL;
- $this->db->set($data);
- $this->db->insert($this->_table_name);
- $id = $this->db->insert_id();
- } else {
- //Update
- $filter = $this->_primary_filter;
- $id = $filter($id);
- $this->db->set($data);
- $this->db->where($this->_primary_key, $id);
- $this->db->update($this->_table_name);
- }
- return $id;
- }
- public function delete($id){
- $filter = $this->_primary_filter;
- $id = $filter($id);
- if(!$id){
- return FALSE;
- }
- $this->db->where($this->_primary_key, $id);
- $this->db->limit(1);
- $this->db->delete($this->_table_name);
- }
- public function hash($string){
- return hash('sha512', $string . config_item('encryption_key'));
- }
- public function array_from_post($fields){
- $data = array();
- foreach($fields as $field){
- $data[$field] = $this->input->post($field);
- }
- return $data;
- }
- public function get_no_parents(){
- //Fetch pages without parents
- $this->db->select('id', 'title');
- $this->db->where('parent_id', 0);
- //$pages = parent::get();
- $pages = $this->get();
- //echo $this->db->last_query();
- //Return key => value pair array
- $array = array(0 => 'No parent');
- if (count($pages)){
- foreach($pages as $page){
- $array[$page->id] = $page->title;
- }
- }
- }
- function get_where($id){
- $table = $this->get_table();
- $this->db->where('id', $id);
- $query=$this->db->get($this->_table_name);
- return $query;
- }
- function _insert($data){
- $table = $this->get_table();
- $this->db->insert($this->_table_name, $data);
- }
- }
- [/code]
Advertisement
Add Comment
Please, Sign In to add comment