Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Signed-off-by: sharkgr8 <shariqueit@yahoo.com>

  • Loading branch information...
commit 1ee3fb4c75642388fdc5cc013c9e9b3bf4be6fcc 0 parents
@sharkgr8 authored
Showing with 178 additions and 0 deletions.
  1. +89 −0 CheckPage.php
  2. +89 −0 README.txt
89 CheckPage.php
@@ -0,0 +1,89 @@
+<?php
+/**
+ * Project Name PageChecker
+ * Project Page https://github.com/sharkgr8/PageChecker
+ * Description This extension is a simple behavior component that is attached to your 'web application' to check whether the current * page is the specified page or not.
+ * @author Syed Sharique <shariqueit@yahoo.com>
+ * @version 1.1
+ */
+
+class PageChecker extends CBehavior {
+
+
+ /**
+ * Checks whether the specified page is currently shown by our application
+ * @param mixed the page to be checked for. You could pass pre-defined values for this parameter are the following:
+ * - `homepage` ---> To check for homepage
+ * - `contact` ---> To check for contact page
+ * - `about` ---> To check for about us page
+ * - `login` ---> To check for login page
+ * You could also pass the controller route in this parameter.
+ * @return boolean whether the current page is the specified page
+ */
+ public function checkSpecifiedPage($page = 'homepage') {
+
+ if(!empty($page))
+ {
+ //Normalizes the page url
+ $url = CHtml::normalizeUrl($page);
+
+ //if the url passed is the string from the pre-defined keywords
+ if (!is_array($url))
+ {
+ switch($url){
+ case 'homepage':
+ $pageUrl = array('site/index'); //set the homepage url
+ break;
+ case 'contact':
+ $pageUrl = array('site/contact'); //set the contact page url
+ break;
+ case 'about':
+ $pageUrl = array('site/page'); //set the about url
+ break;
+ case 'login':
+ $pageUrl = array('site/login'); //set the login url
+ break;
+ }
+ } else {
+ //if the passed value for page parameter is the controller route
+ $pageUrl = array($url[0]);
+ }
+
+ if(sizeof($pageUrl)==1){
+ //Get the current page route
+ $route=Yii::app()->getController()->getRoute();
+
+ //now check whether the specified url is active or not
+ return $this->isPageActive($pageUrl,$route);
+ }
+ }
+ return false;
+
+ }
+
+ /**
+ * Checks whether the specified page url is the same as that of the current page.
+ * This is done by checking if the currently requested URL is same as the requested route url.
+ * Note that the GET parameters not specified in the 'url' option will be ignored.
+ * @param array $url the page url to be checked
+ * @param string $route the route of the current request
+ * @return boolean whether the page url is active
+ */
+ protected function isPageActive($url,$route)
+ {
+ if(isset($url) && is_array($url) && !strcasecmp(trim($url[0],'/'),$route))
+ {
+ unset($url['#']);
+ if(count($url)>1)
+ {
+ foreach(array_splice($url,1) as $name=>$value)
+ {
+ if(!isset($_GET[$name]) || $_GET[$name]!=$value)
+ return false;
+ }
+ }
+ return true;
+ }
+ return false;
+ }
+}
89 README.txt
@@ -0,0 +1,89 @@
+CheckSpecificPage Yii Behaviour
+================================
+
+General
+--------
+
+This extension is a simple behavior component that is attached to your 'web application' to check whether the current page is the specified page or not. Its a very simple and short yet important stuff in anyone's project. Usually we want to filter some portion in our view page or take coding decisions according to the page displayed, which you could do easily using this. This extension is basically inspired from [pisfrontpage](http://www.yiiframework.com/extension/pisfrontpage/ "pisfrontpage") extension.
+
+
+
+Requirements
+
+
+------------
+
+Yii 1.0 or above
+
+Installation
+------------
+
+1. Extract the contents of this package.
+2. Put the resulted php class file under /protected/components
+
+3. Inside protected/config/main.php config file:
+
+
+
+<?php
+// ...
+
+'behaviors' => array(
+
+ 'isFrontPageTeller' => array(
+
+ 'class' => 'application.components.PageChecker',
+
+ )
+),
+
+// ...
+?>
+
+Usage
+-----
+Where you want to check for the specific page rendered, place following code and pass either the following predefined strings:
+ -`homepage` for Hompage/frontpage,
+ -`contact` for Contact Page,
+ -`about` for About Us page and
+ -`login` for Login page
+or the controller action route itself.
+
+<?php
+if (Yii::app()->checkSpecifiedPage('<pagename or controller route>')) {
+ // do something specific for the page
+}
+?>
+
+For example to check whether the rendered page is the homepage or frontpage then check with the following code:
+
+<?php
+if (Yii::app()->checkSpecifiedPage('homepage')) {
+ // do something specific for the homepage
+}
+?>
+
+Likewise for contact page, you could use
+
+<?php
+if (Yii::app()->checkSpecifiedPage('contact')) {
+ // do something specific for the homepage
+}
+?>
+
+OR you could pass the controller action route
+<?php
+if (Yii::app()->checkSpecifiedPage('exampleController/exampleAction')) {
+ // do something specific for the exampleAction page in the exampleController Controller
+}
+?>
+
+Project Page
+------------
+
+https://github.com/sharkgr8/PageChecker
+
+TODOs and BUGS
+--------------
+
+See: https://github.com/sharkgr8/PageChecker/issues
Please sign in to comment.
Something went wrong with that request. Please try again.