Skip to content

Instantly share code, notes, and snippets.

@digitalsadhu
Created October 10, 2013 15:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save digitalsadhu/6920628 to your computer and use it in GitHub Desktop.
Save digitalsadhu/6920628 to your computer and use it in GitHub Desktop.
Html helper class, extends CHtml to provide other useful view methods.
<?
/**
* 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