public function control()
 {
     /*
      * Check if the view is cached and, if it is, return the cached version before any of the application login
      * is executed.
      */
     if ($this->view_mgr->isViewCached()) {
         if ($this->view_mgr->is_cached('json.tpl', $this->getCacheKeyString())) {
             // set the json data to keep the ThinkUpController happy.
             $this->setJsonData(array());
             return $this->generateView();
         }
     }
     /*
      * Check if the API is disabled and, if it is, throw the appropriate exception.
      *
      * Docs: http://thinkupapp.com/docs/userguide/api/errors/apidisabled.html
      */
     $is_api_disabled = Config::getInstance()->getValue('is_api_disabled');
     if ($is_api_disabled) {
         throw new APIDisabledException();
     }
     // fetch the correct PostDAO and UserDAO from the DAOFactory
     $this->post_dao = DAOFactory::getDAO('PostDAO');
     $this->user_dao = DAOFactory::getDAO('UserDAO');
     /*
      * Use the information gathered from the query string to retrieve a
      * User object. This will be the standard object with which to get
      * User information from in API calls.
      */
     if ($this->user_id != null) {
         $this->user = $this->user_dao->getDetails($this->user_id, $this->network);
     } else {
         if ($this->username != null) {
             $this->user = $this->user_dao->getUserByName($this->username, $this->network);
         } else {
             $this->user = null;
         }
     }
     //Privacy checks
     if (substr($this->type, 0, 4) == 'user') {
         //user-related API call
         if (is_null($this->user)) {
             // Check why the User object is null. Could be missing required fields or not found.
             if (is_null($this->user_id) && is_null($this->username)) {
                 $m = 'A request of type ' . $this->type . ' requires a user_id or username to be specified.';
                 throw new RequiredArgumentMissingException($m);
             } else {
                 throw new UserNotFoundException();
             }
         } elseif ($this->user->is_protected) {
             //user is protected on originating network
             throw new UserNotFoundException();
         } else {
             $instance_dao = DAOFactory::getDAO('InstanceDAO');
             $instance = $instance_dao->getByUsernameOnNetwork($this->user->username, $this->user->network);
             if (isset($instance)) {
                 if (!$instance->is_public) {
                     //user is protected on ThinkUp
                     throw new UserNotFoundException();
                 }
             }
         }
     } else {
         //post-related API call
         if ($this->network == "facebook") {
             //assume all Facebook posts are private
             throw new PostNotFoundException();
         }
     }
     /*
      * This switch statement is the main part of this function. It decides
      * what type of posts will be fetched depending on the "type" GET
      * variable and use the PostDAO to fetch the appropriate posts from
      * the database.
      *
      * If a required field is missing it will create an error field to
      * output in JSON.
      */
     switch ($this->type) {
         /*
          * Gets a post.
          *
          * Required arguments: post_id
          *
          * Optional arguments: network, include_entities, include_replies, trim_user
          *
          * Docs: http://thinkupapp.com/docs/userguide/api/posts/post.html
          */
         case 'post':
             if (is_null($this->post_id)) {
                 $m = 'A request of type ' . $this->type . ' requires a post_id to be specified.';
                 throw new RequiredArgumentMissingException($m);
             } else {
                 $data = $this->post_dao->getPost($this->post_id, $this->network, $this->is_public);
             }
             break;
             /*
              * Gets all retweets to a post.
              *
              * Required arguments: post_id
              *
              * Optional arguments: network, order_by, unit, count, page, include_entities, include_replies,
              * trim_user
              *
              * Docs: http://thinkupapp.com/docs/userguide/api/posts/post_retweets.html
              */
         /*
          * Gets all retweets to a post.
          *
          * Required arguments: post_id
          *
          * Optional arguments: network, order_by, unit, count, page, include_entities, include_replies,
          * trim_user
          *
          * Docs: http://thinkupapp.com/docs/userguide/api/posts/post_retweets.html
          */
         case 'post_retweets':
             if (is_null($this->post_id)) {
                 $m = 'A request of type ' . $this->type . ' requires a post_id to be specified.';
                 throw new RequiredArgumentMissingException($m);
             } else {
                 $data = $this->post_dao->getRetweetsOfPost($this->post_id, $this->network, $this->order_by, $this->unit, $this->is_public, $this->count, $this->page);
             }
             break;
             /**
              * Gets replies to a post.
              *
              * Required arguments: post_id
              *
              * Optional arguments: network, order_by, unit, count, page, include_entities, include_replies,
              * trim_user
              *
              * Ordering can only be done by either location or follower count.
              *
              * Docs: http://thinkupapp.com/docs/userguide/api/posts/post_replies.html
              */
         /**
          * Gets replies to a post.
          *
          * Required arguments: post_id
          *
          * Optional arguments: network, order_by, unit, count, page, include_entities, include_replies,
          * trim_user
          *
          * Ordering can only be done by either location or follower count.
          *
          * Docs: http://thinkupapp.com/docs/userguide/api/posts/post_replies.html
          */
         case 'post_replies':
             if (is_null($this->post_id)) {
                 $m = 'A request of type ' . $this->type . ' requires a post_id to be specified.';
                 throw new RequiredArgumentMissingException($m);
             } else {
                 $data = $this->post_dao->getRepliesToPost($this->post_id, $this->network, $this->order_by, $this->unit, $this->is_public, $this->count, $this->page);
             }
             break;
             /*
              * Gets replies to a post within a date range.
              *
              * Required arguments: post_id, from and until
              *
              * Optional arguments: network, order_by, unit, count, page, include_entities, include_replies,
              * trim_user
              *
              * Ordering can only be done by either location or follower count.
              *
              * Docs: http://thinkupapp.com/docs/userguide/api/posts/post_replies.html
              */
         /*
          * Gets replies to a post within a date range.
          *
          * Required arguments: post_id, from and until
          *
          * Optional arguments: network, order_by, unit, count, page, include_entities, include_replies,
          * trim_user
          *
          * Ordering can only be done by either location or follower count.
          *
          * Docs: http://thinkupapp.com/docs/userguide/api/posts/post_replies.html
          */
         case 'post_replies_in_range':
             if (is_null($this->post_id) || is_null($this->from) || is_null($this->until)) {
                 $m = 'A request of type ' . $this->type . ' requires a post_id to be specified.';
                 throw new RequiredArgumentMissingException($m);
             } else {
                 $data = $this->post_dao->getRepliesToPostInRange($this->post_id, $this->network, $this->from, $this->until, $this->order_by, $this->unit, $this->is_public, $this->count, $this->page);
             }
             break;
             /*
              * Get posts related to a post (replies to it, retweets of it).
              *
              * Required arguments: post_id
              *
              * Optional arguments: network, count, page, geo_encoded_only, include_original_post, include_entities,
              * include_replies, trim_user
              *
              * Docs: http://thinkupapp.com/docs/userguide/api/posts/related_posts.html
              */
         /*
          * Get posts related to a post (replies to it, retweets of it).
          *
          * Required arguments: post_id
          *
          * Optional arguments: network, count, page, geo_encoded_only, include_original_post, include_entities,
          * include_replies, trim_user
          *
          * Docs: http://thinkupapp.com/docs/userguide/api/posts/related_posts.html
          */
         case 'related_posts':
             if (is_null($this->post_id)) {
                 $m = 'A request of type ' . $this->type . ' requires a post_id to be specified.';
                 throw new RequiredArgumentMissingException($m);
             } else {
                 $data = $this->post_dao->getRelatedPosts($this->post_id, $this->network, $this->is_public, $this->count, $this->page, $geo_encoded_only = false, $include_original_post = false);
             }
             break;
             /*
              * Gets the user's most replied to posts.
              *
              * Required arguments: user_id or username
              *
              * Optional arguments: network, count, page, include_entities, include_replies, trim_user
              *
              * Docs: http://thinkupapp.com/docs/userguide/api/posts/user_posts_most_replied_to.html
              */
         /*
          * Gets the user's most replied to posts.
          *
          * Required arguments: user_id or username
          *
          * Optional arguments: network, count, page, include_entities, include_replies, trim_user
          *
          * Docs: http://thinkupapp.com/docs/userguide/api/posts/user_posts_most_replied_to.html
          */
         case 'user_posts_most_replied_to':
             $data = $this->post_dao->getMostRepliedToPosts($this->user->user_id, $this->network, $this->count, $this->page, $this->is_public);
             break;
             /*
              * Gets the user's most retweeted posts.
              *
              * Required arguments: user_id or username
              *
              * Optional arguments: network, count, page, include_entities, include_replies, trim_user
              *
              * Docs: http://thinkupapp.com/docs/userguide/api/posts/user_posts_most_retweeted.html
              */
         /*
          * Gets the user's most retweeted posts.
          *
          * Required arguments: user_id or username
          *
          * Optional arguments: network, count, page, include_entities, include_replies, trim_user
          *
          * Docs: http://thinkupapp.com/docs/userguide/api/posts/user_posts_most_retweeted.html
          */
         case 'user_posts_most_retweeted':
             $data = $this->post_dao->getMostRetweetedPosts($this->user->user_id, $this->network, $this->count, $this->page, $this->is_public);
             break;
             /*
              * Gets posts a user has made.
              *
              * Required arguments: user_id or username
              *
              * Optional arguments: network, count, page, order_by, direction, include_entities, include_replies,
              * trim_user
              *
              * Docs: http://thinkupapp.com/docs/userguide/api/posts/user_posts.html
              */
         /*
          * Gets posts a user has made.
          *
          * Required arguments: user_id or username
          *
          * Optional arguments: network, count, page, order_by, direction, include_entities, include_replies,
          * trim_user
          *
          * Docs: http://thinkupapp.com/docs/userguide/api/posts/user_posts.html
          */
         case 'user_posts':
             $data = $this->post_dao->getAllPosts($this->user->user_id, $this->network, $this->count, $this->page, true, $this->order_by, $this->direction, $this->is_public);
             break;
             /*
              * Gets posts a user has made.
              *
              * Required arguments: user_id or username, from and until
              *
              * Optional arguments: network, order_by, direction, include_entities, include_replies,
              * trim_user
              *
              * Docs: http://thinkupapp.com/docs/userguide/api/posts/user_posts_in_range.html
              */
         /*
          * Gets posts a user has made.
          *
          * Required arguments: user_id or username, from and until
          *
          * Optional arguments: network, order_by, direction, include_entities, include_replies,
          * trim_user
          *
          * Docs: http://thinkupapp.com/docs/userguide/api/posts/user_posts_in_range.html
          */
         case 'user_posts_in_range':
             if (is_null($this->from) || is_null($this->until)) {
                 $m = 'A request of type ' . $this->type . ' requires valid from and until parameters to be ';
                 $m .= 'specified.';
                 throw new RequiredArgumentMissingException($m);
             } else {
                 $data = $this->post_dao->getPostsByUserInRange($this->user->user_id, $this->network, $this->from, $this->until, $this->order_by, $this->direction, $iterator = false, $this->is_public);
             }
             break;
             /*
              * Gets posts a user is mentioned in.
              *
              * Required arguments: user_id or username
              *
              * Optional arguments: network, count, page, include_rts, include_entities, include_replies, trim_user
              *
              * Docs: http://thinkupapp.com/docs/userguide/api/posts/user_mentions.html
              */
         /*
          * Gets posts a user is mentioned in.
          *
          * Required arguments: user_id or username
          *
          * Optional arguments: network, count, page, include_rts, include_entities, include_replies, trim_user
          *
          * Docs: http://thinkupapp.com/docs/userguide/api/posts/user_mentions.html
          */
         case 'user_mentions':
             $data = $this->post_dao->getAllMentions($this->user->username, $this->count, $this->network, $this->page, $this->is_public, $this->include_rts, $this->order_by, $this->direction);
             break;
             /*
              * Gets posts a user is mentioned in.within a date range
              *
              * Required arguments: user_id or username, from and until
              *
              * Optional arguments: network, count, page, include_rts, include_entities, include_replies, trim_user
              */
         /*
          * Gets posts a user is mentioned in.within a date range
          *
          * Required arguments: user_id or username, from and until
          *
          * Optional arguments: network, count, page, include_rts, include_entities, include_replies, trim_user
          */
         case 'user_mentions_in_range':
             if (is_null($this->from) || is_null($this->until)) {
                 $m = 'A request of type ' . $this->type . ' requires valid from and until parameters to be ';
                 $m .= 'specified.';
                 throw new RequiredArgumentMissingException($m);
             } else {
                 $data = $this->post_dao->getAllMentionsInRange($this->user->username, $this->count, $this->network, $this->from, $this->until, $this->page, $this->is_public, $this->include_rts, $this->order_by, $this->direction);
             }
             break;
             /*
              * Gets question posts a user has made.
              *
              * Required arguments: user_id or username
              *
              * Optional arguments: network, count, page, order_by, direction, include_entities, include_replies,
              * trim_user
              *
              * Docs: http://thinkupapp.com/docs/userguide/api/posts/user_questions.html
              */
         /*
          * Gets question posts a user has made.
          *
          * Required arguments: user_id or username
          *
          * Optional arguments: network, count, page, order_by, direction, include_entities, include_replies,
          * trim_user
          *
          * Docs: http://thinkupapp.com/docs/userguide/api/posts/user_questions.html
          */
         case 'user_questions':
             $data = $this->post_dao->getAllQuestionPosts($this->user->user_id, $this->network, $this->count, $this->page, $this->order_by, $this->direction, $this->is_public);
             break;
             /*
              * Gets question posts a user has made within a date range
              *
              * Required arguments: user_id or username, from and until
              *
              * Optional arguments: network, count, page, order_by, direction, include_entities, include_replies,
              * trim_user
              *
              * Docs: http://thinkupapp.com/docs/userguide/api/posts/user_questions.html
              */
         /*
          * Gets question posts a user has made within a date range
          *
          * Required arguments: user_id or username, from and until
          *
          * Optional arguments: network, count, page, order_by, direction, include_entities, include_replies,
          * trim_user
          *
          * Docs: http://thinkupapp.com/docs/userguide/api/posts/user_questions.html
          */
         case 'user_questions_in_range':
             $data = $this->post_dao->getAllQuestionPostsInRange($this->user->user_id, $this->network, $this->count, $this->from, $this->until, $this->page, $this->order_by, $this->direction, $this->is_public);
             break;
             /*
              * Gets replies to a user.
              *
              * Required arguments: user_id or username
              *
              * Optional arguments: network, count, page, order_by, direction, include_entities, include_replies,
              * trim_user
              *
              * http://thinkupapp.com/docs/userguide/api/posts/user_replies.html
              */
         /*
          * Gets replies to a user.
          *
          * Required arguments: user_id or username
          *
          * Optional arguments: network, count, page, order_by, direction, include_entities, include_replies,
          * trim_user
          *
          * http://thinkupapp.com/docs/userguide/api/posts/user_replies.html
          */
         case 'user_replies':
             $data = $this->post_dao->getAllReplies($this->user->user_id, $this->network, $this->count, $this->page, $this->order_by, $this->direction, $this->is_public);
             break;
             /*
              * Gets replies to a user within a date range.
              *
              * Required arguments: user_id or username, from and until
              *
              * Optional arguments: network, count, page, order_by, direction, include_entities, include_replies,
              * trim_user
              *
              * http://thinkupapp.com/docs/userguide/api/posts/user_replies.html
              */
         /*
          * Gets replies to a user within a date range.
          *
          * Required arguments: user_id or username, from and until
          *
          * Optional arguments: network, count, page, order_by, direction, include_entities, include_replies,
          * trim_user
          *
          * http://thinkupapp.com/docs/userguide/api/posts/user_replies.html
          */
         case 'user_replies_in_range':
             $data = $this->post_dao->getAllRepliesInRange($this->user->user_id, $this->network, $this->count, $this->from, $this->until, $this->page, $this->order_by, $this->direction, $this->is_public);
             break;
             /*
              * Generate an error because the API call type was not recognized.
              *
              * Docs: http://thinkupapp.com/docs/userguide/api/errors/apicalltypenotrecognised.html
              */
         /*
          * Generate an error because the API call type was not recognized.
          *
          * Docs: http://thinkupapp.com/docs/userguide/api/errors/apicalltypenotrecognised.html
          */
         default:
             throw new APICallTypeNotRecognizedException($this->type);
             break;
     }
     if (is_null($data)) {
         throw new PostNotFoundException();
     }
     switch ($this->network) {
         case 'twitter':
             if (is_array($data)) {
                 foreach ($data as $key => $post) {
                     $data[$key] = $this->convertPostToTweet($post);
                 }
             } else {
                 $data = $this->convertPostToTweet($data);
             }
             break;
         case 'facebook':
             // TODO: write a function here to convert to Facebook API style
             break;
         default:
             break;
     }
     // if no posts were found, $data is null. Set it to an empty array.
     if (is_null($data)) {
         $data = array();
     }
     $this->setJsonData($data);
     return $this->generateView();
 }