Skip to content

Commit

Permalink
some updates to contacts including better addresses and vcf downloader
Browse files Browse the repository at this point in the history
  • Loading branch information
dogmatic69 committed Feb 18, 2010
1 parent ed00044 commit 7da7898
Show file tree
Hide file tree
Showing 13 changed files with 328 additions and 15 deletions.
6 changes: 3 additions & 3 deletions app_controller.php
Expand Up @@ -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');
Expand Down
1 change: 1 addition & 0 deletions config/routes.php
Expand Up @@ -13,6 +13,7 @@
);

Router::parseExtensions('rss');
Router::parseExtensions('vcf');

/**
* redirect to the installer if there is nothing
Expand Down
10 changes: 9 additions & 1 deletion infinitas/contact/contact_app_controller.php
@@ -1,7 +1,15 @@
<?php
class ContactAppController extends AppController {
var $helpers = array(
'Filter.Filter'
'Filter.Filter',
'Contact.Vcf',
'Html'
);

function beforeFilter(){
parent::beforeFilter();

$this->RequestHandler->setContent('vcf', 'text/x-vcard');
}
}
?>
46 changes: 46 additions & 0 deletions infinitas/contact/controllers/branches_controller.php
Expand Up @@ -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;

Expand All @@ -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){
Expand All @@ -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'));
}
}
?>
1 change: 0 additions & 1 deletion infinitas/contact/controllers/contacts_controller.php
Expand Up @@ -49,7 +49,6 @@ function admin_add(){
$this->redirect(array('action' => 'index'));
}
}

$branches = $this->Contact->Branch->find('list');
$this->set(compact('branches'));
}
Expand Down
43 changes: 37 additions & 6 deletions infinitas/contact/models/branch.php
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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;
}
}
?>
10 changes: 6 additions & 4 deletions infinitas/contact/models/contact.php
Expand Up @@ -33,7 +33,7 @@ class Contact extends ContactAppModel{

var $belongsTo = array(
'Contact.Branch'
);
);

/**
* Construct for validation.
Expand Down Expand Up @@ -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),
Expand Down
20 changes: 20 additions & 0 deletions infinitas/contact/views/branches/vcf/view.ctp
@@ -0,0 +1,20 @@
<?php
echo $this->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();

?>
6 changes: 6 additions & 0 deletions infinitas/contact/views/branches/view.ctp
@@ -0,0 +1,6 @@
TODO

<?php
pr($branch);
pr($_SERVER);
?>
151 changes: 151 additions & 0 deletions infinitas/contact/views/helpers/vcf.php
@@ -0,0 +1,151 @@
<?php
/**
*
* vCard Helper
*
* Helps with the creation of vCard files
*/
class VcfHelper extends AppHelper {
/**
* Element map
* map element names to vcard elements
*
* @var array
*/
protected $_elements = array(
'name' => '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);
}

}
?>

0 comments on commit 7da7898

Please sign in to comment.