Archive for the ‘PHP programming’ Category
Symfony sfWidgetFormSelect with disabled options
The sfWidgetFormSelect doesn’t provide ability to render disabled options. It’s rarely used feature of a HTML select element, but sometimes it could save your life :-) In fact, we can achieve this feature by creating our own widget, like one listed below, which inherits from sfWidgetFormSelect. (more…)
Wykrywanie przeglądarek na urządzenia mobilne
Często bywa tak, że chcemy utworzyć mobilną wersję naszego serwisu. Cechuje się ona przeważnie skromniejszą szatą graficzną oraz umiarem w wykorzystaniu JavaScriptu. Aby wykryć przeglądarkę mobilną (używaną w telefonach komórkowych, smartphone lub palmtopach) wystarczy użyć poniższej klasy. W zaprezentowanym rozwiązaniu detekcja przeglądarki oparta jest o nagłówek HTTP_USER_AGENT lub o dodatkowe nagłówki wysyłane przez niektóre przeglądarki.
class MobileBrowserDetector
{
private static $mobileAgentPrefixes = array(
'w3c ','acs-','alav','alca','amoi','audi','avan','benq','bird','blac',
'blaz','brew','cell','cldc','cmd-','dang','doco','eric','hipt','inno',
'ipaq','java','jigs','kddi','keji','leno','lg-c','lg-d','lg-g','lge-',
'maui','maxo','midp','mits','mmef','mobi','mot-','moto','mwbp','nec-',
'newt','noki','oper','palm','pana','pant','phil','play','port','prox',
'qwap','sage','sams','sany','sch-','sec-','send','seri','sgh-','shar',
'sie-','siem','smal','smar','sony','sph-','symb','t-mo','teli','tim-',
'tosh','tsm-','upg1','upsi','vk-v','voda','wap-','wapa','wapi','wapp',
'wapr','webc','winw','winw','xda','xda-'
);
private static $mobileAgentParts = array(
'up.browser', 'up.link', 'mmp', 'symbian', 'mobile safari',
'opera mobi', 'android', 'smartphone', 'midp', 'wap', 'phone'
);
public static function isMobile($userAgent = '')
{
if (!strlen($userAgent)) {
$userAgent = $_SERVER['HTTP_USER_AGENT'];
}
$isMobile = false;
$isMobile |= in_array(strtolower(substr($userAgent, 0, 4)), self::$mobileAgentPrefixes);
$isMobile |= preg_match(sprintf('#(%s)#i', implode('|', self::$mobileAgentParts)), $userAgent);
$isMobile |= strpos(strtolower($_SERVER['HTTP_ACCEPT']),'application/vnd.wap.xhtml+xml') !== false;
$isMobile |= isset($_SERVER['HTTP_PROFILE']);
$isMobile |= isset($_SERVER['HTTP_X_WAP_PROFILE']);
$isMobile |= isset($_SERVER['ALL_HTTP']) && strpos(strtolower($_SERVER['ALL_HTTP']),'OperaMini') !== false;
return (bool)$isMobile;
}
}
Możemy łatwo użyć tej klasy na przykład w projekcie symfony, podmieniając główny layout na layout mobilny (apps/APLIKACJA/templates/layout_mobile.php) w filtrze, w poniższy sposób.
Zawartość apps/APLIKACJA/config/filters.yml
rendering: ~ security: class: sfGuardBasicSecurityFilter mobile: class: mobileFilter cache: ~ common: ~ execution: ~
Klasa filtra lib/MobileFilter.class.php:
class MobileFilter extends sfFilter
{
public function execute($filterChain)
{
if (MobileBrowserDetector::isMobile()) {
$this->getContext()->getActionStack()->getFirstEntry()->getActionInstance()->setLayout('layout_mobile');
}
$filterChain->execute();
}
}
0
komentarzy