From 7da78987ebabaabce9356cf097290de2fda24375 Mon Sep 17 00:00:00 2001 From: dogmatic Date: Thu, 18 Feb 2010 21:45:35 +0200 Subject: [PATCH] some updates to contacts including better addresses and vcf downloader --- app_controller.php | 6 +- config/routes.php | 1 + infinitas/contact/contact_app_controller.php | 10 +- .../controllers/branches_controller.php | 46 ++++++ .../controllers/contacts_controller.php | 1 - infinitas/contact/models/branch.php | 43 ++++- infinitas/contact/models/contact.php | 10 +- infinitas/contact/views/branches/vcf/view.ctp | 20 +++ infinitas/contact/views/branches/view.ctp | 6 + infinitas/contact/views/helpers/vcf.php | 151 ++++++++++++++++++ infinitas/management/models/address.php | 13 ++ infinitas/management/models/time_zone.php | 30 ++++ views/layouts/vcf/front.ctp | 6 + 13 files changed, 328 insertions(+), 15 deletions(-) create mode 100644 infinitas/contact/views/branches/vcf/view.ctp create mode 100644 infinitas/contact/views/branches/view.ctp create mode 100644 infinitas/contact/views/helpers/vcf.php create mode 100644 infinitas/management/models/address.php create mode 100644 infinitas/management/models/time_zone.php create mode 100644 views/layouts/vcf/front.ctp diff --git a/app_controller.php b/app_controller.php index 5a69fdc65..187d7468a 100644 --- a/app_controller.php +++ b/app_controller.php @@ -70,9 +70,9 @@ function beforeFilter() { $this->{$this->modelClass}->setUserData($this->Session->read('Auth')); } - if( $this->RequestHandler->isRss() ){ - Configure::write('debug', 0); - $this->theme = null; + if($this->RequestHandler->prefers('rss') || $this->RequestHandler->prefers('vcf')){ + //Configure::write('debug', 0); + //$this->theme = null; } $this->set('commentModel', 'Comment'); diff --git a/config/routes.php b/config/routes.php index 96996839e..b3f406de3 100644 --- a/config/routes.php +++ b/config/routes.php @@ -13,6 +13,7 @@ ); Router::parseExtensions('rss'); + Router::parseExtensions('vcf'); /** * redirect to the installer if there is nothing diff --git a/infinitas/contact/contact_app_controller.php b/infinitas/contact/contact_app_controller.php index c331797f1..03825e87d 100644 --- a/infinitas/contact/contact_app_controller.php +++ b/infinitas/contact/contact_app_controller.php @@ -1,7 +1,15 @@ RequestHandler->setContent('vcf', 'text/x-vcard'); + } } ?> \ No newline at end of file diff --git a/infinitas/contact/controllers/branches_controller.php b/infinitas/contact/controllers/branches_controller.php index 63ec78f5d..481c7373b 100644 --- a/infinitas/contact/controllers/branches_controller.php +++ b/infinitas/contact/controllers/branches_controller.php @@ -27,6 +27,46 @@ class BranchesController extends ContactAppController{ 'Filter.Filter' ); + function view(){ + if (!isset($this->params['slug'])) { + $this->Session->setFlash( __('A problem occured', true) ); + $this->redirect($this->referer()); + } + + $branch = $this->Branch->find( + 'first', + array( + 'conditions' => array( + 'Branch.slug' => $this->params['slug'], + 'Branch.active' => 1 + ), + 'contain' => array( + 'Address' => array( + 'fields' => array( + 'Address.name', + 'Address.street', + 'Address.city', + 'Address.postal', + 'Address.province' + ), + 'Country' => array( + 'fields' => array( + 'Country.name' + ) + ) + ), + ) + ) + ); + + if (empty($branch)) { + $this->Session->setFlash( __('The branch does not exsit', true) ); + $this->redirect($this->referer()); + } + + $this->set(compact('branch')); + } + function admin_index(){ $this->Branch->recursive = 0; @@ -52,6 +92,9 @@ function admin_add(){ $this->redirect(array('action' => 'index')); } } + + $timeZones = $this->Contact->TimeZone->find('list'); + $this->set(compact('timeZones')); } function admin_edit($id = null){ @@ -72,6 +115,9 @@ function admin_edit($id = null){ if ($id && empty($this->data)) { $this->data = $this->Branch->read(null, $id); } + + //$timeZones = $this->Branch->TimeZone->find('list'); + $this->set(compact('timeZones')); } } ?> \ No newline at end of file diff --git a/infinitas/contact/controllers/contacts_controller.php b/infinitas/contact/controllers/contacts_controller.php index bf63a5203..2ea4f9921 100644 --- a/infinitas/contact/controllers/contacts_controller.php +++ b/infinitas/contact/controllers/contacts_controller.php @@ -49,7 +49,6 @@ function admin_add(){ $this->redirect(array('action' => 'index')); } } - $branches = $this->Contact->Branch->find('list'); $this->set(compact('branches')); } diff --git a/infinitas/contact/models/branch.php b/infinitas/contact/models/branch.php index e558335bb..c16633b9c 100644 --- a/infinitas/contact/models/branch.php +++ b/infinitas/contact/models/branch.php @@ -41,13 +41,22 @@ class Branch extends ContactAppModel{ ) ), - 'Libs.Sequence' + 'Libs.Sequence', + 'Libs.Sluggable' => array( + 'label' => array( + 'name' + ) + ) ); var $hasMany = array( 'Contact.Contact' ); + var $belongsTo = array( + 'Management.Address' + ); + /** * Construct for validation. * @@ -84,15 +93,37 @@ function __construct($id = false, $table = null, $ds = null) { ) ), 'fax' => array( - 'rule' => 'phone', - 'message' => __('Please enter a valid fax number', true), - 'allowEmpty' => true + 'phone' => array( + 'rule' => 'phone', + 'message' => __('Please enter a valid fax number', true), + 'allowEmpty' => true + ) ), 'map' => array( - 'rule' => 'url', - 'message' => __('Please enter a valid url for the map', true) + 'map' => array( + 'rule' => 'url', + 'message' => __('Please enter a valid url for the map', true) + ) + ), + 'time_zone_id' => array( + 'comparison' => array( + 'rule' => array('comparison', '>', 0), + 'message' => __('Please select your time zone', true) + ) ) ); } + + function beforeFind($queryData){ + $this->bindModel( + array( + 'belongsTo' => array( + //'Management.TimeZone' + ) + ) + ); + + return true; + } } ?> \ No newline at end of file diff --git a/infinitas/contact/models/contact.php b/infinitas/contact/models/contact.php index 2bc23e3fa..a22fbff1b 100644 --- a/infinitas/contact/models/contact.php +++ b/infinitas/contact/models/contact.php @@ -33,7 +33,7 @@ class Contact extends ContactAppModel{ var $belongsTo = array( 'Contact.Branch' - ); + ); /** * Construct for validation. @@ -71,9 +71,11 @@ function __construct($id = false, $table = null, $ds = null) { ) ), 'mobile' => array( - 'rule' => 'phone', - 'message' => __('Please enter a valid mobile number', true), - 'allowEmpty' => true + 'phone' => array( + 'rule' => 'phone', + 'message' => __('Please enter a valid mobile number', true), + 'allowEmpty' => true + ) ), 'branch_id' => array( 'rule' => array('comparison', '>', 0), diff --git a/infinitas/contact/views/branches/vcf/view.ctp b/infinitas/contact/views/branches/vcf/view.ctp new file mode 100644 index 000000000..8191718a6 --- /dev/null +++ b/infinitas/contact/views/branches/vcf/view.ctp @@ -0,0 +1,20 @@ +Vcf->begin(); + echo $this->Vcf->attr('organization', Configure::read('Website.name')); + echo $this->Vcf->attr('fullName', $branch['Branch']['name']); + echo $this->Vcf->attr('email', $branch['Branch']['name']); + echo $this->Vcf->attr('url', env('SERVER_NAME')); + echo $this->Vcf->attr('image', $this->Html->url('/img/content/contact/branch/'.$branch['Branch']['image'], true)); + echo $this->Vcf->address( + $branch['Address']['name'], + array( + 'country' => $branch['Address']['Country']['name'], + 'province' => $branch['Address']['province'], + 'postal' => $branch['Address']['postal'], + 'city' => $branch['Address']['city'], + 'street' => $branch['Address']['street'] + ) + ); + echo $this->Vcf->end(); + +?> \ No newline at end of file diff --git a/infinitas/contact/views/branches/view.ctp b/infinitas/contact/views/branches/view.ctp new file mode 100644 index 000000000..30472d1c4 --- /dev/null +++ b/infinitas/contact/views/branches/view.ctp @@ -0,0 +1,6 @@ +TODO + + \ No newline at end of file diff --git a/infinitas/contact/views/helpers/vcf.php b/infinitas/contact/views/helpers/vcf.php new file mode 100644 index 000000000..8cf6728a0 --- /dev/null +++ b/infinitas/contact/views/helpers/vcf.php @@ -0,0 +1,151 @@ + 'N:%last%;%first%;%middle%;%title%', + 'fullName' => 'FN:%value%', + 'organization' => 'ORG:%value%', + 'title' => 'TITLE:%value%', + 'workPhone' => 'TEL;WORK:%value%', + 'homePhone' => 'TEL;HOME:%value%', + 'cellPhone' => 'TEL;CELL:%value%', + 'address' => 'ADR', + 'birthday' => 'BDAY:%value%', + 'email' => 'EMAIL;INTERNET:%value%', + 'timezone' => 'TZ:%value%', + 'url' => 'URL:%value%', + 'version' => 'VERSION:%value%', + 'image' => 'PHOTO;VALUE=URL:%value%', + ); + /** + * Separator between values. + * + * @var string + **/ + protected $_separator = ':'; + /** + * End of line character + * + * @var string + **/ + protected $_eol = "\n"; + /** + * End of attribute terminator. + * + * @var string + **/ + protected $_terminator = ';'; + /** + * Overloaded call method + * + * @param string $method Name of method called + * @param mixed $params Params for method. + * @return mixed + **/ + public function __call($method, $params) { + if (isset($this->_elements[$method])) { + array_unshift($params, $method); + return $this->dispatchMethod('attr', $params); + } + trigger_error($method . ' is not a valid element.', E_USER_WARNING); + } + /** + * begin a vcard + * + * @return string + */ + public function begin() { + return "BEGIN:VCARD" . $this->_eol; + } + /** + * End a vcard + * + * @return string + */ + public function end() { + return "END:VCARD" . $this->_eol; + } + /** + * Create a new attribute for the vCard + * + * @param string $type Type of element to make + * @param string $value Value to put into the card + * @return mixed False on non-existant type or empty values, string on success + */ + public function attr($type, $values) { + if (empty($values)) { + return false; + } + if (is_string($values)) { + $values = array('value' => $values); + } + + if ($type != 'image') { + $values = $this->_escape($values); + } + + if (!isset($this->_elements[$type])) { + return false; + } + $out = String::insert($this->_elements[$type], + $values, array('clean' => true, 'before' => '%', 'after' => '%') + ); + return $out . $this->_eol; + } + + /** + * Create an Address element. Takes the following keys + * + * - street + * - city + * - province + * - postal + * - country + * + * @param string $type The type of address you are making + * @param array $values Array of values for the address see above + */ + public function address($type, $values = array()) { + $empty = array( + 'street' => '', + 'city' => '', + 'province' => '', + 'postal' => '', + 'country' => '', + ); + $values = array_merge($empty, $values); + $values['key'] = $this->_elements['address']; + $values['type'] = strtoupper($type); + + $format = "%key%;%type%:;;%street%;%city%;%province%;%postal%;%country%;"; + $adrEl = String::insert($format, $values, array('before' => '%', 'after' => '%', 'clean' => true)); + $labelFormat = "LABEL;POSTAL;%type%;ENCODING=QUOTED-PRINTABLE:%street%=0D=0A%city%, %province% %postal%=0D=0A%country%"; + $labelEl = String::insert($labelFormat, $values, array('before' => '%', 'after' => '%', 'clean' => true)); + + return $adrEl . $this->_eol . $labelEl . $this->_eol; + } + /** + * Escape values for vcard + * + * @param mixed $values Values either string or array. + * @return string Escaped string + **/ + protected function _escape($values) { + $find = array(':'); + $replace = array('\:'); + return is_array($values) ? array_map(array($this, '_escape'), $values) : str_replace($find, $replace, $values); + } + + } +?> \ No newline at end of file diff --git a/infinitas/management/models/address.php b/infinitas/management/models/address.php new file mode 100644 index 000000000..e70bb5ff1 --- /dev/null +++ b/infinitas/management/models/address.php @@ -0,0 +1,13 @@ + \ No newline at end of file diff --git a/infinitas/management/models/time_zone.php b/infinitas/management/models/time_zone.php new file mode 100644 index 000000000..21e05e8ca --- /dev/null +++ b/infinitas/management/models/time_zone.php @@ -0,0 +1,30 @@ + $value){ + $data[] = array( + 'TimeZone' => array( + 'id' => $key, + 'name' => $value + ) + ); + } + return $data; + break; + } // switch + } + } +?> \ No newline at end of file diff --git a/views/layouts/vcf/front.ctp b/views/layouts/vcf/front.ctp new file mode 100644 index 000000000..05736cf58 --- /dev/null +++ b/views/layouts/vcf/front.ctp @@ -0,0 +1,6 @@ + \ No newline at end of file