<?php

if (!defined('BASEPATH')) {
    exit('No direct script access allowed');
}

/**
 * Description of alphagallery_api
 *
 * @author Igor Ratsun
 */
class alphagallery_api
{

    protected $main_table  = '';
    protected $foreign_key = '';

    public function __construct($options = array())
    {
        foreach ($options as $k => $v) {
            if (isset($this->{$k})) {
                $this->{$k} = $v;
            }
        }
    }

    public function get_photos($foreign_id = 0)
    {
        $this->check_options();
        
        $ci = & get_instance();
        
        $ci->load->model('alphagallery/alphagallery_admin_model');
        $config['main_table']  = $this->main_table;
        $config['foreign_key'] = $this->foreign_key;
        $ci->alphagallery_admin_model->set_gallery_config($config);
        $ci->alphagallery_admin_model->set_table($ci->config->item('main_table'));
        $ci->alphagallery_admin_model->set_pkey($ci->config->item('alphagallery_photo_pkey'));
        $ci->alphagallery_admin_model->set_lang_table($ci->config->item('lang_table'));

        $items = $ci->alphagallery_admin_model->fetch_by_fields(array($config['foreign_key'] => $foreign_id));

        return !empty($items) ? $items : array();
    }

    protected function check_options()
    {
        if (empty($this->main_table)) {
            show_error('main_table is empty');
        }
        if (empty($this->foreign_key)) {
            show_error('foreign_key is empty');
        }
        
        return TRUE;
    }

}
