Browse Source

added missing components

euromark 14 years ago
parent
commit
953cd78fa4

+ 153 - 0
Controller/Component/CalendarComponent.php

@@ -0,0 +1,153 @@
+<?php
+
+App::uses('Component', 'Controller');
+
+/**
+ * Calendar Component
+ *
+ * inspired by http://www.flipflops.org/2007/09/21/a-simple-php-calendar-function/
+ * 
+ * @author Mark Scherer
+ * @copyright 2012 Mark Scherer
+ * @license MIT
+ * 
+ * 2012-02-08 ms
+ */
+class CalendarComponent extends Component {
+
+	public $Controller = null;
+
+	public $monthList = array(
+		'january', 'february', 'march', 'april', 'may', 'june', 'july', 'august', 'september', 'october', 'november', 'december');
+		
+	public $dayList = array(
+		'mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun'
+	);
+
+	public $year = null;
+	public $month = null;
+	public $day = null;
+
+	/**
+	 * Startup controller
+	 * 
+	 * @param object $Controller Controller instance
+	 * @access public
+	 * @return void
+	 */
+	public function startup(Controller $Controller) {
+		$this->Controller = $Controller;
+	}
+
+	/**
+	 * @return bool $success
+	 */
+	function ensureCalendarConsistency($year, $month, $span = 10) {
+		if (!is_numeric($month)) {
+			$monthKeys = array_keys($this->monthList, $month);
+			$month = array_shift($monthKeys);
+			if ($month === null) {
+				$month = -1;
+			}
+		}
+
+		if (!$year || !$month) {
+			$year = date('Y');
+			$month = date('n');
+			$item = null;
+		}
+		$year = (int)$year;
+		$month = (int)$month;
+
+		$current = date('Y');
+
+		if (empty($month) || $year < $current - $span || $year > $current + $span) {
+			$this->Controller->Common->flashMessage(__('invalid date'), 'error');
+			$this->Controller->redirect(array('action' => 'index'));
+		}
+
+		$this->year = $year;
+		$this->month = $month;
+
+		if (empty($this->Controller->request->params['pass'])) {
+			return true;
+		}
+
+		if ($month < 1 || $month > 12) {
+			$this->Controller->Common->flashMessage(__('invalid date'), 'error');
+			$this->Controller->redirect(array('action' => 'index'));
+		}
+		return true;
+	}
+
+	public function year() {
+		return $this->year;
+	}
+
+	public function month($asString = false) {
+		return $this->month;
+	}
+
+
+	/**
+	 * month as integer value 1..12 or 0 on error
+	 * february => 2
+	 * 2009-12-26 ms
+	 */
+	public function retrieveMonth($string) {
+		if (empty($string)) {
+			return 0;
+		}
+		$string = mb_strtolower($string);
+		if (in_array($string, $this->monthList)) {
+			$keys = array_keys($this->monthList, $string);
+			return $keys[0] + 1;
+		}
+		return 0;
+	}
+
+	/**
+	 * day as integer value 1..31 or 0 on error
+	 * february => 2
+	 * 2009-12-26 ms
+	 */
+	public function retrieveDay($string, $month = null) {
+		if (empty($string)) {
+			return 0;
+		}
+		$string = (int)$string;
+		if ($string < 1 || $string > 31) {
+			return 0;
+		}
+
+		# check on month days!
+		return $string;
+
+		return 0;
+	}
+
+
+	public function months() {
+		return $this->monthList;
+	}
+
+	public function days() {
+		return $this->dayList;
+	}
+
+	/**
+	 * converts integer to x-digit string
+	 * 1 => 01, 12 => 12
+	 * 2009-12-26 ms
+	 */
+	public function asString($number, $digits = 2) {
+		$number = (string )$number;
+		$count = mb_strlen($number);
+		while ($count < $digits) {
+			$number = '0' . $number;
+			$count++;
+		}
+		return $number;
+	}
+
+}

File diff suppressed because it is too large
+ 1663 - 0
Controller/Component/CommonComponent.php


+ 141 - 0
Controller/Component/MobileComponent.php

@@ -0,0 +1,141 @@
+<?php
+App::uses('Component', 'Controller');
+
+/**
+ * Uses Session: User.mobile and User.nomobile
+ * - mobile is the auto-detection (true/false)
+ * - nomobile can be set by the user and overrides the default behavior/detection
+ *   (1=true/0=false or -1=null which will remove the override) 
+ * 
+ * TODO: differentaite between "isMobile" and "has/wants mobile"
+ * @author Mark Scherer
+ * @license MIT
+ * 2011-12-28 ms
+ */
+class MobileComponent extends Component {
+	
+	public $components = array('Session');
+	
+	public $isMobile = null;
+	
+	public $setMobile = null;
+	
+	public $Controller = null;
+	
+	protected $_defaults = array(
+		'engine' => 'cake',
+	);
+
+	public function __construct(ComponentCollection $collection, $settings = array()) {
+		$settings = am($this->_defaults, $settings);
+		parent::__construct($collection, $settings);
+	}
+	
+	public function initialize(Controller $Controller) {
+		parent::initialize($Controller);
+		$this->Controller = $Controller;
+	
+		if (isset($this->Controller->request->params['named']['mobile'])) {
+			if ($this->Controller->request->params['named']['mobile'] == '-1') {
+				$noMobile = null;
+			} else {
+				$wantsMobile = (bool) $this->Controller->request->params['named']['mobile'];
+				$noMobile = (int) (!$wantsMobile);
+			}
+			$this->Session->write('User.nomobile', $noMobile);
+		
+		}
+		$this->setMobile();
+		
+		$urlParams = Router::getParams(true);
+		if (!isset($urlParams['named'])) {
+			$urlParams['named'] = array();
+		}
+		if (!isset($urlParams['pass'])) {
+			$urlParams['pass'] = array();
+		}
+		$urlParams = am($urlParams, $urlParams['named'], $urlParams['pass']);
+		unset($urlParams['named']);
+		unset($urlParams['pass']);
+		if (isset($urlParams['prefix'])) {
+			unset($urlParams['prefix']);
+		}
+		
+		if ($this->setMobile) {
+			$url = Router::url(am($urlParams, array('mobile'=>0)));
+			$this->Controller->set('desktopUrl', $url);
+			
+		} else {
+			$url = Router::url(am($urlParams, array('mobile'=>1)));
+			$this->Controller->set('mobileUrl', $url);			
+		}
+		
+		Configure::write('User.mobile', $this->isMobile);
+		Configure::write('User.setMobile', $this->setMobile);
+	}
+	
+	public function setMobile() {
+		if ($this->isMobile === null) {
+			$mobile = $this->isMobile();
+			$this->isMobile = $mobile;
+		}
+		$noMobile = $this->Session->read('User.nomobile');
+		if (!$this->isMobile && $noMobile === null || $noMobile) {
+			$this->setMobile = false;
+			return;
+		}
+		$this->setMobile = true;
+		$this->Controller->viewClass = 'Theme';
+		$this->Controller->theme = 'Mobile';
+		//$this->Controller->layoutPath = 'mobile';
+	}
+	
+	/**
+	 * 
+	 * @return bool $success
+	 */
+	public function isMobile() {
+		$isMobile = $this->Session->read('User.mobile');
+		if ($isMobile !== null) {
+			return $isMobile;
+		}
+		if ($this->settings['engine'] !== 'cake') {
+ 			throw new CakeException(__('Engine %s not available', $this->settings['engine']));
+			//TODO
+		}
+		$isMobile = (int)$this->detect();
+		$this->Session->write('User.mobile', $isMobile);
+		return $isMobile;
+	}
+	
+	public function detect() {
+		$this->Controller->request->addDetector('mobile', array('options' => array('OMNIA7')));
+		return $this->Controller->request->is('mobile');	
+	}
+	
+	
+	
+	public function detectByTools() {
+		$isMobile = $this->Session->read('Session.mobile');
+		if ($isMobile !== null) {
+			return $isMobile;
+		}
+		App::uses('UserAgentLib', 'Tools.Lib');
+		$UserAgentLib = new UserAgentLib();
+		$mobile = (int)$UserAgentLib->isMobile();
+		$this->Session->write('Session.mobile', $mobile);
+		return $mobile;
+	}
+	
+	public function detectByWurfl() {
+		App::import('Vendor', 'WURFL', array('file' => 'WURFLManagerProvider.php'));
+		$wurflConfigFile = APP . 'Config' . DS . 'wurfl ' . DS . 'config.xml';
+		$wurflManager = WURFL_WURFLManagerProvider::getWURFLManager($wurflConfigFile);
+
+		$requestingDevice = $wurflManager->getDeviceForHttpRequest($_SERVER);
+		if ($requestingDevice->getCapability('is_wireless_device') == 'true') {
+			return true;
+		}
+		return false;
+	}
+}