<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>jesal gadhia &#187; codeigniter</title>
	<atom:link href="http://jesal.us/tag/codeigniter/feed/" rel="self" type="application/rss+xml" />
	<link>http://jesal.us</link>
	<description></description>
	<lastBuildDate>Thu, 14 Jul 2011 18:51:24 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>Integrating CodeIgniter with WordPress</title>
		<link>http://jesal.us/2010/12/integrating-codeigniter-with-wordpress/</link>
		<comments>http://jesal.us/2010/12/integrating-codeigniter-with-wordpress/#comments</comments>
		<pubDate>Wed, 01 Dec 2010 20:13:58 +0000</pubDate>
		<dc:creator>J</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[codeigniter]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://jesal.us/?p=242</guid>
		<description><![CDATA[Recently I was working on a website for a small business client. They wanted a simple system to allow them to easily update a section of their website. Since it was a small website, I wanted to accomplish this without putting too much time but still creating something user-friendly and easy to use that can [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I was working on a website for a small business client. They wanted a simple system to allow them to easily update a section of their website. Since it was a small website, I wanted to accomplish this without putting too much time but still creating something user-friendly and easy to use that can get the job done. The main site was made using CodeIgniter. So we decided to create a WordPress blog for the client where they could put their content and I would pull that content inside their CI website. Here&#8217;s how:</p>
<p>First of all, install WordPress in a folder called blog in your website. It would be located at the top level directory of the website. So it would be alongside the system folder of the CI installation. If you already have a DB for your site, have WordPress add tables to that same DB with a prefix to make things simple (although this is not a requirement).</p>
<p>Once that&#8217;s done. All you have to do is add the following line in the at the top of your CodeIgniter&#8217;s index.php file. Change the path to wp-blog-header.php as needed to point to your wordpress&#8217;s root directory.</p>
<pre>
&lt;?php
require('blog/wp-blog-header.php');
</pre>
<p>One you get that dialed in, rest is pretty straight forward. All of WordPress&#8217; native API functions should now be accessible to you. So you could throw in a WordPress loop in any of the CI views like for example:</p>
<p><b>Articles.php (View):</b></p>
<pre>
&lt;?php
if ($type == 'Tag') { query_posts('tag='.$id.'&#038;posts_per_page=-1'); }
else if ($type == 'Category') { query_posts('category_name='.$id.'&#038;posts_per_page=-1'); }
else { query_posts('posts_per_page=-1'); }
while (have_posts()) : the_post(); // Loop
?&gt;
	&lt;a href="&lt;?=base_url();?&gt;Research/Article/&lt;?php the_ID(); ?&gt;"&gt;&lt;h4&gt;&lt;?php the_title(); ?&gt;&lt;/h4&gt;&lt;/a&gt;
	By &lt;?php the_author(); ?&gt;
	&lt;p class="details"&gt;&lt;? the_time('F jS, Y') ?&gt;&lt;/p&gt;
	&lt;?php the_excerpt(); ?&gt;
	&lt;hr/&gt;
&lt;?php
	endwhile; // End Loop
?&gt;
</pre>
<p>&#8230;and a sidebar on the same which which would list out all the tags and categories to select from:</p>
<pre>
&lt;!-- SIDE BAR --&gt;
&lt;div id="sideBar"&gt;
	&lt;h3&gt;Categories:&lt;/h3&gt;
	&lt;?php
	$args = array(
	'show_option_all'    =&gt; '',
	'orderby'            =&gt; 'name',
	'order'              =&gt; 'ASC',
	'show_last_update'   =&gt; '0',
	'style'              =&gt; 'none',
	'show_count'         =&gt; '0',
	'hide_empty'         =&gt; '1',
	'use_desc_for_title' =&gt; '1',
	'child_of'           =&gt; '0',
	'feed'               =&gt; '',
	'feed_type'          =&gt; '',
	'feed_image'         =&gt; '',
	'exclude'            =&gt; '',
	'exclude_tree'       =&gt; '',
	'include'            =&gt; '',
	'current_category'   =&gt; '0',
	'hierarchical'       =&gt; 'true',
	'title_li'           =&gt; __( 'Categories' ),
	'number'             =&gt; NULL,
	'echo'               =&gt; '1',
	'depth'              =&gt; '0');
	wp_list_categories( $args ); ?&gt;
	&lt;br&gt;

	&lt;h3&gt;Tags:&lt;/h3&gt;
	&lt;?php
	$args = array(
	'smallest'  =&gt; '12',
	'largest'   =&gt; '12',
	'unit'      =&gt; 'px',
	'number'    =&gt; '45',
	'format'    =&gt; 'flat',
	'separator' =&gt; ' ',
	'orderby'   =&gt; 'name',
	'order'     =&gt; 'ASC',
	'exclude'   =&gt; '',
	'include'   =&gt; '',
	'link'      =&gt; 'custom',
	'taxonomy'  =&gt; 'post_tag',
	'echo'      =&gt; true,
	'link_url'  =&gt; base_url()."Research/Articles/Tag/");
	wp_tag_cloud( $args ); ?&gt;

&lt;/div&gt;
&lt;!-- /SIDE BAR --&gt;
</pre>
<p><b>Article.php (View):</b></p>
<pre>
&lt;!-- CONTENT --&gt;
&lt;div id="mainContent"&gt;

    &lt;!-- LEFT NAV --&gt;
    &lt;div id="leftNav"&gt;
        &lt;a href="&lt;?=base_url();?&gt;Research/Articles" class="selected"&gt;Articles&lt;/a&gt;
    &lt;/div&gt;
    &lt;!-- /LEFT NAV --&gt;

    &lt;!-- MAIN COL --&gt;
    &lt;div id="mainColWide"&gt;
        &lt;h1&gt;&lt;?= $post-&gt;post_title ?&gt;&lt;/h1&gt;
        &lt;p class="details"&gt;&lt;?= the_time('F j, Y') ?&gt;&lt;/p&gt;
        &lt;?= wpautop($post-&gt;post_content) ?&gt;
        &lt;br/&gt;&lt;br/&gt;
        &lt;a href="&lt;?=base_url();?&gt;Research/Articles"&gt;back&lt;/a&gt;
    &lt;/div&gt;
    &lt;!-- /MAIN COL --&gt;
    &lt;br class="clearFloat" /&gt;
&lt;/div&gt;
&lt;!-- /CONTENT --&gt;
</pre>
<p><b>Research.php (Controller):</b></p>
<pre>
&lt;?php

class Research extends Base_Controller {

	function Research()
	{
        parent::Base_Controller();
	}

	function Index()
	{
        $this-&gt;data['content'] = $this-&gt;load-&gt;view('Research/index.php', $this-&gt;data, true);
        $this-&gt;load-&gt;view('layouts/master' ,$this-&gt;data);
	}

    function Articles($type = '', $id = '')
	{
        $this-&gt;data['type'] = $type;
        $this-&gt;data['id'] = $id;
        $this-&gt;data['content'] = $this-&gt;load-&gt;view('Research/articles.php', $this-&gt;data, true);
        $this-&gt;load-&gt;view('layouts/master' ,$this-&gt;data);
	}

    function Article($id)
	{
        $this-&gt;data['post'] = get_post($id);
        $this-&gt;data['id'] = $id;
        $this-&gt;data['content'] = $this-&gt;load-&gt;view('Research/article.php', $this-&gt;data, true);
        $this-&gt;load-&gt;view('layouts/master' ,$this-&gt;data);
	}
}
</pre>
<p>That&#8217;s it! This would allow us to browse all the blog entries without leaving the CI site. Other helper functions can also be found in WordPress&#8217;s documentation which can assist you in integrating the design.</p>
]]></content:encoded>
			<wfw:commentRss>http://jesal.us/2010/12/integrating-codeigniter-with-wordpress/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>CodeIgniter + Facebook Connect</title>
		<link>http://jesal.us/2010/01/codeigniter-facebook-connect/</link>
		<comments>http://jesal.us/2010/01/codeigniter-facebook-connect/#comments</comments>
		<pubDate>Tue, 26 Jan 2010 20:11:41 +0000</pubDate>
		<dc:creator>J</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[codeigniter]]></category>
		<category><![CDATA[facebook]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://jesal.us/?p=183</guid>
		<description><![CDATA[In this post I will talk about how to create a Facebook Connect site using the CodeIgniter framework. So first of all, before you being, download the Facebook Platform client library from here. Extract the files and copy everything inside /php folder (including the &#8216;jsonwrapper&#8217; folder) to the /system/plugins folder of your CI application. Once [...]]]></description>
			<content:encoded><![CDATA[<p>In this post I will talk about how to create a Facebook Connect site using the CodeIgniter framework. </p>
<p>So first of all, before you being, download the Facebook Platform client library from <a href="http://wiki.developers.facebook.com/index.php/PHP" target="_blank">here</a>. Extract the files and copy everything inside /php folder (including the &#8216;jsonwrapper&#8217; folder) to the <strong>/system/plugins</strong> folder of your CI application. Once you do that, rename the <strong>facebook.php file to facebook_pi.php</strong>. Doing so will make CI recognize the library as a plugin. Now you will be able to reference the library just how you would reference any other plugins as you will see below.</p>
<p>Once you&#8217;ve done that. We are all set to write some code. The first key step to this is to extend the base controller and create a Facebook Controller that can handle user authentication and pass other needed variables. This extended controller would have to reside in <strong>/system/application/libraries/MY_Controller.php</strong>:</p>
<pre>
class Facebook_Controller extends Controller {

    var $facebook;
    var $fb_uid;
    var $fb_user_details;
    var $user;
    var $permissions_to_request;
    var $logged_in;

    public $data;

	function Facebook_Controller() {

	parent::Controller();

        $this->output->set_header("Cache-Control: private, no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
        $this->output->set_header("Pragma: no-cache");

        // Created some Facebook helper functions to make things easier and pull API Key, Secret Key, Base URLs, etc from the Facebook config file.
        $this->__fbApiKey = get_facebook_api_key();
        $this->__fbSecret = get_facebook_secret_key();

        // Prevent the 'Undefined index: facebook_config' notice from being thrown.
        $GLOBALS['facebook_config']['debug'] = NULL;

        // Create a Facebook client API object.
        $this->facebook = new Facebook($this->__fbApiKey, $this->__fbSecret);
        $this->fb_uid = $this->facebook->get_loggedin_user();

        //Check to see if the user is logged in with facebook
        if($this->fb_uid)
        {
            $fb_uid = $this->fb_uid;

            //If this is the first login with the site
            if(!$this->_get_user_session_object($fb_uid))
            {
                //Check to see if user is in the db
                $this->load->model('User_model');
                $this->user = $this->User_model->get_user_by_fb_uid($fb_uid);
                $this->fb_user_details = $this->Facebook_model->get_user_details($fb_uid);

                //If its not in the db, do an insert
                if(!$this->user)
                {
                   $user_data = array('fb_uid' => $fb_uid, 'created_at' => date('Y-m-d H:i:s'));
                   $user_id = $this->User_model->insert_user($user_data);

				   //Give them a default username
                   $user_data = array('username' => 'user'.$user_id);
				   $this->User_model->update_user($user_id, $user_data);

                    $this->user = $this->User_model->get_user_by_user_id($user_id);
                }

                $this->_set_user_session_object($fb_uid, $this->user);
                $this->_set_user_details_session_object($fb_uid, $this->fb_user_details);

                //Check to see if user has all the required permissions
                $has_publish_stream_permission = $this->facebook->api_client->call_method('Users.hasAppPermission',array('ext_perm'=>'publish_stream', 'uid'=>$fb_uid));
                $has_email_permission = $this->facebook->api_client->call_method('Users.hasAppPermission',array('ext_perm'=>'email', 'uid'=>$fb_uid));

                //Request the missing permissions
                $permissions_to_request = $has_publish_stream_permission == 0 ? "publish_stream" : null;
                if($permissions_to_request)
                    $permissions_to_request .= ",";
                $permissions_to_request .= $has_email_permission == 0 ? "email" : null;

                // I wanted to use this approach but it left a trailing comma in the end so it didn't work out. I'm sure there is a way around it.
                //$arry_permissions_to_request = array($has_publish_stream_permission == 0 ? "publish_stream" : null, $has_email_permission == 0 ? ",email" : null);
                //$permissions_to_request = implode(",", $arry_permissions_to_request);

                $this->permissions_to_request = $permissions_to_request;
            }
            else
            {
                //Load the user object back from the session
                $this->user = $this->_get_user_session_object($this->fb_uid);
                $this->fb_user_details = $this->_get_user_details_session_object($this->fb_uid);
            }
        }

        //Set master page data variables
        $this->data['user'] = $this->user;
        $this->data['fb_user_details'] = $this->fb_user_details;
        $this->logged_in = ($this->user &#038;&#038; $this->fb_uid)? true : false;
        $this->data['logged_in'] = $this->logged_in;
        $this->data['request_permissions'] = $this->permissions_to_request ? true : false;
        $this->data['permissions_to_request'] = $this->permissions_to_request;
        $this->data['fb_login_prompt'] = false;
        $this->data['current_controller'] = get_class($this);
	}

    function _get_user_session_object($fb_uid)
    {
        return $this->session->userdata($fb_uid.'_user_object');
    }

    function _get_user_details_session_object($fb_uid)
    {
        return $this->session->userdata($fb_uid.'_user_details_object');
    }

    function _set_user_session_object($fb_uid, $user)
    {
        $this->session->set_userdata($fb_uid.'_user_object', $user);
    }

    function _set_user_details_session_object($fb_uid, $fb_user_details)
    {
        $this->session->set_userdata($fb_uid.'_user_details_object', $fb_user_details);
    }
}
</pre>
<p>Once we have that setup, we can go ahead and create a master page which will load the header, footer, navigation and all the other site-wide global elements. Part of which, the header, could look something like this, it would show the user&#8217;s profile pic &#038; name when logged in and show a Facebook Connect button when logged out. You can put this master page inside <strong>/system/application/views/layouts/master.php</strong>:</p>
<pre>
&lt;div id="header"&gt;
&lt;?php if (!$logged_in) { ?&gt;
&lt;div style="margin-top:18px"&gt;
	&lt;a href="#" id="fbconnect_login_button" class="fbconnect_login_button" onclick="FB.Connect.requireSession(); return false;" &gt;
		&lt;img id="fb_login_image" src="http://static.ak.fbcdn.net/images/fbconnect/login-buttons/connect_light_medium_long.gif" style="border:0;" alt="Connect"/&gt;
	&lt;/a&gt;
&lt;/div&gt;
&lt;?php } else { ?&gt;
	&lt;div class="floatR"&gt;
		&lt;fb:profile-pic uid="&lt;?=$user['fb_uid']?&gt;" size="square" facebook-logo="true"&gt;&lt;/fb:profile-pic&gt;
	&lt;/div&gt;
	&lt;div class="floatR" style="margin-top:13px"&gt;
	Welcome, &lt;?=$fb_user_details[0]['name']?&gt;
	&lt;br /&gt;
	&lt;a href="#" onclick="FB.Connect.logout(function() { facebook_onlogout(); }); facebook_onlogout()"&gt;Logout of Facebook&lt;/a&gt;
	&lt;/div&gt;
	&lt;br class="clearFloat" /&gt;
&lt;?php } ?&gt;
&lt;/div&gt;
</pre>
<p>Content area would just be one variable which will load the content from the page controller as we will see further on:</p>
<pre>
&lt;div id="mainContent"&gt;
	&lt;?=$content?&gt;
&lt;/div&gt;
</pre>
<p>And finally, the most important part, the footer which would load &#038; initialize the Facebook library, take care of prompting for the right permissions, and require the user to login on certain pages based on the controller:</p>
<pre>
&lt;div id="footer"&gt;
	&lt;script type="text/javascript"&gt;
		function setFocus() { window.document.&lt;?=$current_controller?&gt;.focus(); }
	&lt;/script&gt;
	&lt;script src="&lt;?=get_static_facebook_root();?&gt;/js/api_lib/v0.4/FeatureLoader.js.php/en_US" type="text/javascript"&gt;&lt;/script&gt;
	&lt;script type="text/javascript"&gt;
		FB.init("&lt;?=get_facebook_api_key();?&gt;", "&lt;?=base_url();?&gt;xd_receiver.htm", {"reloadIfSessionStateChanged":true});
	&lt;/script&gt;
	&lt;script src="&lt;?=base_url();?&gt;shared/js/fbconnect.js" type="text/javascript"&gt;&lt;/script&gt;
	&lt;script type="text/javascript"&gt;
		window.onload = function() {
			facebook_onload(&lt;?=$logged_in?&gt;);
			&lt;?php if ($request_permissions) { ?&gt;facebook_prompt_permission("&lt;?=$permissions_to_request?&gt;");&lt;?php } ?&gt;
		};
	&lt;/script&gt;
	&lt;?php if ($fb_login_prompt &#038;&#038; !$logged_in) { ?&gt;
	&lt;script type="text/javascript"&gt;
		FB.ensureInit(function() {
			FB.Connect.requireSession(null, function(){ location.href = "&lt;?=base_url();?&gt;home"; });
		});
	&lt;/script&gt;
	&lt;?php } ?&gt;
&lt;/div&gt;
</pre>
<p>As you can see, I&#8217;ve used some JavaScript in fbconnect.js to help us invoke the permissions and/or dialog box as needed:</p>
<pre>
/*
 * The facebook_onload statement is printed out in the PHP. If the user's logged in
 * status has changed since the last page load, then refresh the page to pick up
 * the change.
 *
 * This helps enforce the concept of "single sign on", so that if a user is signed into
 * Facebook when they visit your site, they will be automatically logged in -
 * without any need to click the login button.
 *
 * @param already_logged_into_facebook  reports whether the server thinks the user
 *                                      is logged in, based on their cookies
 *
 */
function facebook_onload(already_logged_into_facebook) {
  // user state is either: has a session, or does not.
  // if the state has changed, detect that and reload.
  FB.ensureInit(function() {
      FB.Facebook.get_sessionState().waitUntilReady(function(session) {
          var is_now_logged_into_facebook = session ? true : false;

          // if the new state is the same as the old (i.e., nothing changed)
          // then do nothing
          if (is_now_logged_into_facebook == already_logged_into_facebook) {
            return;
          }

          // otherwise, refresh to pick up the state change
          refresh_page();
        });
    });
}

function facebook_onlogin_ready() {
  refresh_page();
}

function facebook_onlogout() {
    $.post("./Logout", null, null);
}

function refresh_page() {
    location.reload(true);
}

function facebook_prompt_permission(permission) {
  FB.ensureInit(function() {
    FB.Connect.showPermissionDialog(permission);
  });
}
</pre>
<p>Now that we have the master page setup, we can inject our content into the master page from any page controller:</p>
<pre>
class Test extends Facebook_Controller {

   function Test()
   {
        parent::Facebook_Controller();
   }

   function Index()
   {
        $this->data['fb_login_prompt'] = true;
        $this->data['content'] = $this->load->view('test', $this->data, true);
        $this->load->view('layouts/master' ,$this->data);
   }
</pre>
<p>So, thats it! Now you should have a fully functional Facebook connect site in CodeIgniter!</p>
<p>Inspired by:<br />
<a href="http://junal.wordpress.com/2008/01/20/a-sample-facebook-application-with-codeigniter/" target="_blank">http://junal.wordpress.com/2008/01/20/a-sample-facebook-application-with-codeigniter/</a><br />
<a href="http://www.simpleprojectz.com/2008/10/facebook-codeigniter/" target="_blank">http://www.simpleprojectz.com/2008/10/facebook-codeigniter/</a><br />
<a href="http://codeigniter.com/forums/viewthread/120393/" target="_blank">http://codeigniter.com/forums/viewthread/120393/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://jesal.us/2010/01/codeigniter-facebook-connect/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
	</channel>
</rss>

