CAFPE Docs
  • Class

Classes

  • Article_model
  • Articles
  • Auth
  • Auth_controller
  • Bcrypt
  • Hierarchical_controllers
  • Ion_auth
  • Ion_auth_model
  • MY_Controller
  • MY_Lang
  • MY_Model
  • Pages
  • Private_pages
  • Public_controller
  • Seeder
  • Slug
  • User_sessions
  • Users

Functions

  • action_result
  • actions_widget
  • admin_area_buttons
  • base64_current_url_encode
  • base64_url_decode
  • base64_url_encode
  • current_lang
  • jscript_tag
  • lang_switcher
  • logged_in
  • redirect_lang
  • site_url_lang
  • style_tag
  1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56  57  58  59  60  61  62  63  64  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88  89  90  91  92  93  94  95  96  97  98  99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 
<?php  defined('BASEPATH') or exit('No direct script access allowed');

 class Article_model extends MY_Model
 {
     public $table = 'articles';
     public $primary_key = 'id';
     public $protected = array('id');
     public $return_as = 'array';
     public $rules;

     public function __construct()
     {
         $config = array('table' => 'articles');

         $this->load->library('slug', $config);

        // article has to have titles and content in at least one language
        $this->rules['insert'] = array(
            'title_es_callable' => array(
                'field' => 'title_es_callable',
                'label' => 'title_es_callable',
                'rules' =>  array(
                    array(
                        'title_es_callable',
                        function () {
                            $valid_es = $this->input->post('title_es') && $this->input->post('content_es');
                            $valid_en = $this->input->post('title_en') && $this->input->post('content_en');
                            if (!$valid_es && !$valid_en) {
                                $this->form_validation->set_message('title_es_callable', lang('article_dependent_fields'));
                                return false;
                            }
                            return true;
                        }
                    )
                )
            ),
            'title_es' => array(
                'field' => 'title_es',
                'label' =>  lang('title'),
                'rules' => array('trim')
            ),
            'title_en' => array(
                'field' => 'title_en',
                'label' => lang('title'),
                'rules' => array('trim')
            ),
            'content_es' => array(
                'field' => 'content_es',
                'label' => lang('content'),
                'rules' =>  array('trim')
            ),
            'content_en' => array(
                'field' => 'content_en',
                'label' => lang('content'),
                'rules' =>  array('trim')
            ),
            'date' => array(
                'field' => 'date',
                'label' => lang('date'),
                'rules' =>  array(
                                'required',
                                array(
                                'date_callable',
                                function ($date) {
                                    if(!$date) {
                                        return true;
                                    }
                                    $d = DateTime::createFromFormat('d-m-Y', $date);
                                    if ($d && $d->format('d-m-Y') === $date) {
                                        return true;
                                    }
                                    $this->form_validation->set_message('date_callable', lang('date_invalid'));
                                    return false;
                                }
                        ))
            ),
            'main_pic' => array(
                'field' => 'main_pic',
                'label' => 'main_pic'
            )
        );

         $this->add_dynamic_rules();

         $this->rules['update'] = $this->rules['insert'];
         $this->before_create[] = 'create_slugs';
         $this->before_update[] = 'create_slugs';
         $this->before_create[] = 'get_main_pic';
         $this->before_update[] = 'get_main_pic';
         $this->before_create[] = 'date_toggle';
         $this->before_update[] = 'date_toggle';
         $this->after_get[] = 'date_toggle';
         parent::__construct();
     }

    /**
     * returns all articles with fields in given language
     * @param string $lang language
     * @return $this
     */
    public function get_all_lang($lang, $order_by = '')
    {
        $select = "
                    id,
                    title_{$lang} as title,
                    content_{$lang} as content,
                    date,
                    main_pic,
                    slug_{$lang} as slug
                ";
        $this->db->select($select, false)
                    ->where("title_{$lang} !=", '');
        if($order_by) {
            $this->db->order_by($order_by);
        }
        $query = $this->db->get($this->table);

        return $query ? $query->result_array() : false;
    }

    /**
     * add where clause to get slug in given language. If not language provided
     * searches in all slug fields
     * @param string $slug slug
     * @param string $lang language
     * @return $this
     */
    public function where_slug($slug, $lang = '')
    {
        if ($lang) {
            $this->where('slug_'.$lang, $slug);
        } else {
            $this->where('slug_es', $slug)->or_where('slug_en', $slug);
        }
        return $this;
    }

    /**
     * looks for a slug in DB, if it's found, returns its lang
     * @param string $slug slug
     * @return mixed lang or false, if not found
     */
    public function slug_lang($slug)
    {
        if($slug) {
            if($this->where('slug_es', $slug)->get()) {
                return 'es';
            } elseif ($this->where('slug_en', $slug)->get())  {
                return 'en';
            }
        }

        return false;
    }

    /**
     * Creates data slugs from titles
     * @param array $data original data
     * @return array $data modified data
     */
    protected function create_slugs($data)
    {
        $data['slug_es'] = $this->slug->create_uri(@$data['title_es'], 'slug_es');
        $data['slug_en'] = $this->slug->create_uri(@$data['title_en'], 'slug_en');
        return $data;
    }

    /**
     * Gets first picture in an article
     * @param array $data original data
     * @return array $data modified data
     */
    protected function get_main_pic($data)
    {
        if ($data['content_es']) {
            $content = $data['content_es'];
        } else {
            $content = $data['content_en'];
        }


        if (preg_match_all('/< *img[^>]*src *= *["\']?([^"\']*)/i', $content, $matches)) {
            $data['main_pic'] = $matches [1][0];
        } else {
            $data['main_pic'] = null;
        }

        return $data;
    }

    /**
     * Toggles date  in array of result(s) from DB
     * @param array $data original data
     * @return array $data modified data
     */
    protected function date_toggle($data)
    {
        // valid data, and not array of results
        if(isset($data['date'])) {
            $data['date'] = $this->_date_toggle($data['date']);
        }
        // array of results
        else {
            foreach ($data as &$record) {
                if(isset($record['date'])) {
                    $record['date'] = $this->_date_toggle($record['date']);
                }
            }
        }
        return $data;
    }

    /**
     * Toggles date format YYYY-MM-DD <-> DD-MM-YYYY
     * @param array $data original data
     * @return array $data modified data
     */
    private function _date_toggle($date)
    {
        return implode(array_reverse(explode('-', $date)),'-');
    }

    /**
     * Adds rules to rules array, which depend on posted fields content
     */
    private function add_dynamic_rules()
    {
        // if aticle title is posted, corresponding language content can't be empty, and vice versa
        if ($this->input->post('title_es')) {
            array_unshift($this->rules['insert']['content_es']['rules'], 'required');
        }
        if ($this->input->post('title_en')) {
            array_unshift($this->rules['insert']['content_en']['rules'], 'required');
        }
        if ($this->input->post('content_es')) {
            array_unshift($this->rules['insert']['title_es']['rules'], 'required');
        }
        if ($this->input->post('content_en')) {
            array_unshift($this->rules['insert']['title_en']['rules'], 'required');
        }
    }
 }
CAFPE Docs API documentation generated by ApiGen