Created
October 10, 2013 15:48
-
-
Save digitalsadhu/6920628 to your computer and use it in GitHub Desktop.
Html helper class, extends CHtml to provide other useful view methods.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<? | |
/** | |
* URL-ify | |
* Returns the value in a URL-ified version. | |
* @return string | |
*/ | |
function urlify($string) { | |
$string = strtolower($string); | |
$string = str_replace(array('*', '!', '&', '=', '/', '?'), '', $string); | |
$string = str_replace(array(' '), '-', $string); | |
return $string; | |
} | |
/** | |
* Un URL-ify | |
* Returns the value with some of the urlify effects reversed! | |
* @return string | |
*/ | |
function unUrlify($string) { | |
$string = ucwords($string); | |
$string = str_replace(array('-'), ' ', $string); | |
return $string; | |
} | |
/** | |
* Class to provide additional CHtml style methods for use in view | |
* files | |
*/ | |
class Html extends CHtml { | |
/** | |
* Creates a link for a result that links to the detailed view for the | |
* result. | |
* | |
* @param string $type - 'inventory', 'part', 'series', 'model' or 'make' | |
* @param array $data - data needed to create url, not all are required, depends on type | |
* @example [ | |
* 'inventory_id' => 1, | |
* 'part_type_id' => $this->part_types_id, | |
'part_type_name' => $this->partType->name, | |
'series_id' => $this->request->vehicleSeries->id, | |
'series_name' => $this->request->vehicleSeries->name, | |
'model_name' => $this->request->vehicleSeries->model->name, | |
'make_name' => $this->request->vehicleSeries->make->name | |
* ] | |
* | |
* @return string A url for the part/inventory item/series/make or model | |
*/ | |
public static function partPageUrl($type, array $data) { | |
$reverseLinkParts = []; | |
// Fall through collecting all the parts for the url | |
switch ($type) { | |
case 'inventory': | |
$reverseLinkParts[] = $data['inventory_id']; | |
case 'part': | |
$reverseLinkParts[] = $data['part_type_id'] . | |
'-' . $data['part_type_name']; | |
case 'series': | |
$reverseLinkParts[] = $data['series_id'] . | |
'-' . $data['series_name']; | |
case 'model': | |
$reverseLinkParts[] = $data['model_name']; | |
case 'make': | |
$reverseLinkParts[] = $data['make_name']; | |
default: | |
$reverseLinkParts[] = '/parts'; | |
} | |
// Reverse the link parts list and join on / | |
return implode('/', array_reverse($reverseLinkParts)); | |
} | |
/** | |
* Creates a link to a part on partsworld | |
* | |
* @param string $text - the link text | |
* @param string $type - the type of link being created | |
* @param array $data - additional link data | |
* | |
* @return string - html a tag | |
*/ | |
public static function partLink($text, $type, array $data) { | |
$url = Yii::app()->createUrl(self::partPageUrl($type, $data)); | |
return Chtml::link($text, $url); | |
} | |
/** | |
* Creates a link to a part on partsworld with an absolute url | |
* | |
* @param string $text - the link text | |
* @param string $type - the type of link being created | |
* @param array $data - additional link data | |
* | |
* @return string - html a tag | |
*/ | |
public static function absolutePartLink($text, $type, array $data) { | |
$url = Yii::app()->createAbsoluteUrl(self::partPageUrl($type, $data)); | |
return Chtml::link($text, $url); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment