/app/Controller/TwitsController.php
PHP | 255 lines | 139 code | 36 blank | 80 comment | 30 complexity | 5a23619e346e6ffd06c1d3d3a695f66a MD5 | raw file
- <?php
-
- /* $
- Document : fktwitter
- Created on : Dec 26, 2011, 7:12:34 PM
- Author : udeshika
- Description: fktwitter
- */
-
- class TwitsController extends AppController {
-
- public $name = 'Twits';
- public $helpers = array('Html', 'Form', 'Js', 'Custom');
- public $components = array('Session');
-
- /**
- * get all twits everyone else X
- * @access public
- * @param
- * @return posts array pass to the view
- */
- public function index() {
- if ($this->check_users()) {
-
- $results = $this->Twit->query("SELECT PO.* , US.username ,US.fname ,US.lname ,US.image FROM `twits` as PO LEFT
- JOIN users as US ON US.username = PO.username WHERE PO.username!='" . $this->Session->read('User') . "' ORDER BY UNIX_TIMESTAMP(PO.created) DESC");
- $this->set('posts', $results);
- } else {
- $this->redirect('../users/login');
- }
- }
-
- /**
- * get all twits everyone else
- * @access public
- * @param $user login user or all user profile $user name
- * @return posts array pass to the view, userdata array returns profile data
- */
- public function stream($user='') {
-
- if ($this->check_users()) {
-
- if ($user == 'everyone') {
- $results = $this->Twit->query("SELECT PO.* , US.username ,US.fname ,US.lname ,US.image FROM `twits` as PO LEFT
- JOIN users as US ON US.username = PO.username WHERE PO.username!='" . $this->Session->read('User') . "' ORDER BY UNIX_TIMESTAMP(PO.created) DESC");
- } else {
- $results = $this->Twit->query("SELECT PO.* , US.username ,US.fname ,US.lname ,US.image FROM `twits` as PO LEFT
- JOIN users as US ON US.username = PO.username WHERE US.username='" . $user . "' OR LOCATE ('@" . $user . "', PO.twitt) > 0 ORDER BY UNIX_TIMESTAMP(PO.created) DESC");
-
- $user_results = $this->Twit->query("SELECT US.* FROM `users` as US WHERE US.username='" . $user . "'");
- $this->set('userdata', $user_results);
- }
- //$this->set('sidecontent_for_layout', 'sidecontent');
- $this->set('posts', $results);
- } else {
- $this->redirect('../users/login');
- }
- }
-
- /**
- * get last insert twitt using AJAX
- * @access public
- * @param last insert id $id
- * @return posts array pass to the view
- */
- public function getstream($id='') {
-
- if ($this->check_users()) {
-
- $this->layout = '';
-
- if ($id > 0) {
- $results = $this->Twit->query("SELECT PO.* , US.username ,US.fname ,US.lname ,US.image FROM `twits` as PO LEFT
- JOIN users as US ON US.username = PO.username WHERE PO.id='" . $id . "'");
- } else {
- $results = $this->Twit->query("SELECT PO.* , US.username ,US.fname ,US.lname ,US.image FROM `twits` as PO LEFT
- JOIN users as US ON US.username = PO.username ORDER BY PO.created DESC");
- }
-
- $this->set('posts', $results);
- } else {
- $this->redirect('../users/login');
- }
- }
-
- /**
- * All status stream refreshed every 2 second using AJAX
- * @access public
- * @param select data acording to last twitt ($id) and username ($user)
- * @return posts array pass to the view
- */
- public function refreshe($id='', $user='') {
-
- if ($this->check_users()) {
-
- $this->layout = '';
- if ($user != '') {
-
- if ($id > 0) {
- $results = $this->Twit->query("SELECT PO.* , US.username ,US.fname ,US.lname ,US.image FROM `twits` as PO LEFT
- JOIN users as US ON US.username = PO.username WHERE PO.id >'" . $id . "' AND
- (US.username='" . $user . "' OR LOCATE ('@" . $user . "', PO.twitt) > 0 ) ORDER BY UNIX_TIMESTAMP(PO.created) DESC");
- $this->set('posts', $results);
- }
- } else {
- if ($id > 0) {
- $results = $this->Twit->query("SELECT PO.* , US.username ,US.fname ,US.lname ,US.image FROM `twits` as PO LEFT
- JOIN users as US ON US.username = PO.username WHERE PO.username!='" . $this->Session->read('User') . "' AND PO.id >'" . $id . "' ORDER BY UNIX_TIMESTAMP(PO.created) DESC");
- $this->set('posts', $results);
- }
- }
- } else {
- $this->redirect('../users/login');
- }
- }
-
- /**
- * more info twitt via jq ajax
- * @access public
- * @param int twitt $id
- * @return posts array pass to the view
- */
- public function view($id) {
- $this->layout = '';
- $results = $this->Twit->query("SELECT PO.* , US.username ,US.fname ,US.lname ,US.image FROM `twits` as PO LEFT
- JOIN users as US ON US.username = PO.username WHERE PO.id='".$id."'");
- $this->set('posts', $results);
- }
-
- /**
- * add new twitt via jq ajax
- * @access public
- * @param url convert to shorten and post will save
- * @return redirect twitt strim
- */
- public function ajaxadd() {
- if ($this->request->is('post')) {
- $this->layout = '';
- $this->request->data['twitt'] = $this->set_url($this->request->data['twitt']);
- if ($this->Twit->save($this->request->data)) {
- echo $this->Twit->getLastInsertId();
- // echo 'Your post has been saved.';
- } else {
- echo 'Unable to add your post.';
- }
- }
- }
-
- /**
- * add new twitt
- * @access public
- * @param url convert to shorten and post will save
- * @return redirect twitt strim
- */
- public function add() {
- if ($this->check_users()) {
- if ($this->request->is('post')) {
-
- $this->request->data['twitt'] = $this->set_url($this->request->data['twitt']);
-
- if ($this->Twit->save($this->request->data)) {
- $this->Session->setFlash('Your post has been saved.');
- $this->redirect('stream/everyone');
- } else {
- echo 'Unable to add your post.';
- }
- }
- } else {
- $this->redirect('../users/login');
- }
- }
-
- /**
- * delete twitter
- * @access public
- * @param delete twitt acording to $id
- * @return redirect to user profile
- */
- function delete($id) {
- if ($this->check_users()) {
- if ($this->request->is('get')) {
- throw new MethodNotAllowedException();
- }
- if ($this->Twit->delete($id)) {
- $this->Session->setFlash('The twitter with id: ' . $id . ' has been deleted.');
- $this->redirect(array('action' => 'stream/' . $this->Session->read('User') . ''));
- }
- } else {
- $this->redirect('../users/login');
- }
- }
-
- /**
- * string replase with <a href= and shortener url
- * @access public
- * @param string $text replase
- * @return text
- */
- public function set_url($text) {
-
- $urlset = $this->geturls($text);
-
- foreach ($urlset[0] as $k => $url) {
-
- $url_shortener = $this->random_alpha(5);
- $text = str_replace($url, '<a href="' . $this->base . '/t/url/' . $url_shortener . '">' . $url . '</a>', $text);
- $this->insert_url($url, $url_shortener);
- }
- return $text;
- }
-
- /**
- * preg_match_all urls
- * @access public
- * @param string $text to $matches array
- * @return array
- */
- public function geturls($text) {
-
- $matches = array();
- preg_match_all('/((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[.\!\/\\w]*))?)/', $text, $matches);
- return $matches;
- }
-
- /**
- * Inserts a URL to the database and returns the whole dataset
- * @access public
- * @param string $url Url to shorten
- * @return array Array with all available data inside, or empty one
- */
- function insert_url($url, $shortener_url) {
- $ret = array();
- $sql = 'INSERT INTO url_redirect (url,shortener_url)
- VALUES (
- \'' . $url . '\',
- \'' . $shortener_url . '\');';
-
- $results = $this->Twit->query($sql);
- }
-
- /**
- * randum 5 letter str
- * @access public
- * @param $alphNums all alph numeric values,$length length of new url
- * @return pass new shorten url
- */
- function random_alpha($length) {
-
- $alphNums = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
- $newString = str_shuffle(str_repeat($alphNums, rand(1, $length)));
-
- return substr($newString, rand(0, strlen($newString) - $length), $length);
- }
-
- }