CodeIgniter Forums
Am i coding the/a right way ? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Am i coding the/a right way ? (/showthread.php?tid=13151)



Am i coding the/a right way ? - El Forum - 11-12-2008

[eluser]___seb[/eluser]
Hi,

because i'm not familiar with CI (neither any framework, MVC and object style), I don't know if i code the right way.
Here is the i made to test CI. Note that the templates caching (smarty) is not yet used.

this code uses datamapper.
What does it do :
- display bookmarks related to tag in the url (requested)
+ a list of subtags (tags associated with the found links) (less bookmarks)
+ links to the bookmarks, each without one of the found tags. (more bookmarks)
model : nothing special
Code:
class Bookmark extends DataMapper
{
    var $has_many = array('tag');

    function Bookmark()
    {
        parent::DataMapper();
    }

}

Controler : (file attached)
Code:
define('SEPARATEUR','~');
define('ITEMS_PER_PAGE',10);

class Bookmark_ctrl extends Controller
{

    /**
    * Constructor
    */
    function Bookmark_ctrl() // constructeur : appellé a chaque fois
    {
        parent::Controller();
        $this->load->model('Bookmark');
        $this->load->model('Tag');
        $this->load->library('smarty_ci');
    }
    
    /**
    * Actions
    */
    function index($tags_inline = false, $page = 1) // appellé si pas d'action de définie
    {
        $CI =& get_instance();
        $base_url = dirname($CI->config->system_url()).'/';

        $requested_tags_array = array();        // array of tags requested in url     [string]
        $requested_tags_found_array = array();    // array of tags requested in url that exist in db  [id => int] [tag => string]
        $requested_tags_found_array_flat = array();    // array of tags requested in url that exist in db  [id => int] [tag => string]
        $requested_tags_with_links = array();
        $requested_tags_not_found_array = array();    // array of tags requested in url that do not exist in db     [string]
        $requested_tags_found_str = '';        // string of requested tags'id     | ('1,6,9...')

        $requested_tags_array = $this->_get_request_tags(&$tags_inline);
        $this->_filter_requested_tags(    $requested_tags_array,
                        &$requested_tags_found_array,
                        &$requested_tags_not_found_array,
                        &$requested_tags_found_array_flat);
        unset($requested_tags_array, $inline);
        

        $bookmark = new bookmark();
        $bookmarks = array();
        $bookmarks_out = array();


        // --- the bookmarks ---

        if(empty($requested_tags_found_array)) // no requested tags found
        {
            $bookmarks = $bookmark->limit(ITEMS_PER_PAGE)->get()->all;
        }
        else
        {
            $bookmarks_ids_array = $this->_get_booksmarks_ids_by_tags($requested_tags_found_array);
            $bookmarks = $bookmark->where_in('id',$bookmarks_ids_array)->get()->all;
        }
        // out as array
        $bookmark_fields = $this->db->list_fields('bookmarks');
        foreach($bookmarks as $bookmark_obj)
        {
            $array = array();
            foreach($bookmark_obj as $k => $v)
            {
                if(in_array($k, $bookmark_fields))
                    $array[$k] = $v; //array
            }
            // add its tags
            $tags = $bookmark_obj->tag->get()->all;
            $tags_out = array();
// ---------------
            foreach($tags as $tag_obj)
            {
                $tags_out[] = array('id' => $tag_obj->id , 'tag' => $tag_obj->tag, 'url' => $this->make_link_refine( $tag_obj->tag , $requested_tags_found_array_flat) );
            }
            $array['tags'] = $tags_out;
// ---------------
            $bookmarks_out[] = $array; //array('id' => $bookmark_obj->id , 'url' => $bookmark_obj->url  );
        }


        // --- (bookmarks) related tags ---

        $tag = new tag();
        $tags = array();
        $tags_out = array();

        
        if(empty($requested_tags_found_array))
        {
            $tags = $tag->get()->all;
        }
        else
        {
            $tags_ids_array = $this->_get_tags_ids_by_bookmarks($bookmarks_ids_array);
            $tags = $tag->where_in('id',$tags_ids_array)->get()->all;
        }

        // add url to tags
        $tags_out = array();
        foreach($tags as $tag_obj)
        {
            $tags_out[] = array('id' => $tag_obj->id , 'tag' => $tag_obj->tag, 'url' => $this->make_link_refine( $tag_obj->tag , $requested_tags_found_array_flat) );
        }

        // --- requested tags with link to remove them ---
        foreach($requested_tags_found_array_flat as $tag)
            $requested_tags_with_links[] = array('tag' => $tag, 'url' => $this->make_link_enlarge( $tag , $requested_tags_found_array_flat) );

        $this->smarty_ci->assign('tags',$tags_out);
        $this->smarty_ci->assign('bookmarks',$bookmarks_out);
        $this->smarty_ci->assign('requested_tags',$requested_tags_with_links);

        $this->smarty_ci->display('base.tpl');
        $this->output->enable_profiler(TRUE);

    }

    /**
    * privates functions
    */

    /**
    * array of requested tags (requested by url)
    * @param string tags in url ( 'tag1SEPARATEURtag2SEPARATEURtag3' => tag1~tag2~tag3 )
    * @return array ( 'tag1', 'tag2', ... )
    * @version 0.1
    */
    private function _get_request_tags(&$tags_inline)
    {
        $tags_inline = trim ( $tags_inline, SEPARATEUR );
        $requested_tags_array = explode(SEPARATEUR ,$tags_inline);
        return array_unique($requested_tags_array);
    }

// other functions removed because of message size limit, files attached
}