Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Alistair Kearney committed Sep 19, 2008
0 parents commit 09128af
Show file tree
Hide file tree
Showing 4 changed files with 300 additions and 0 deletions.
26 changes: 26 additions & 0 deletions LICENCE
@@ -0,0 +1,26 @@
All source code included in the "Field: Time" Symphony Extension archive
is, unless otherwise specified, released under the MIT licence as follows:

----- begin license block -----

Copyright 2008 Alistair Kearney, Allen Chang, Scott Hughes

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

----- end license block -----
30 changes: 30 additions & 0 deletions README
@@ -0,0 +1,30 @@

Time Field
------------------------------------

Version: 1.4
Author: Symphony Team (team@symphony21.com)
Build Date: 15th April 2008
Requirements: Symphony Beta revision 5 or greater.


[INSTALLATION]

1. Upload the 'timefield' folder in this archive to your Symphony 'extensions' folder.

2. Enable it by selecting the "Field: Time", choose Enable from the with-selected menu, then click Apply.

3. You can now add the "Time" field to your sections.


[UPDATE]

- Run the follow queries and change XXX to be the ID's of any time field you have in the system:

ALTER TABLE `tbl_entries_data_XXX` CHANGE `value` `value` VARCHAR( 20 ) NULL DEFAULT NULL;
ALTER TABLE `tbl_entries_data_XXX` ADD `seconds` BIGINT( 20 ) UNSIGNED NOT NULL ;
ALTER TABLE `tbl_entries_data_XXX` ADD INDEX ( `seconds` ) ;

[USAGE]

- Filtering supports ranges via a {from} to {to} logic. E.G. 01:12:43 to 31:09:33.
32 changes: 32 additions & 0 deletions extension.driver.php
@@ -0,0 +1,32 @@
<?php

Class extension_timefield extends Extension{

public function about(){
return array('name' => 'Field: Time',
'version' => '1.4',
'release-date' => '2008-04-15',
'author' => array('name' => 'Symphony Team',
'website' => 'http://www.symphony21.com',
'email' => 'team@symphony21.com')
);
}

public function uninstall(){
$this->_Parent->Database->query("DROP TABLE `tbl_fields_time`");
}


public function install(){

return $this->_Parent->Database->query("CREATE TABLE `tbl_fields_time` (
`id` int(11) unsigned NOT NULL auto_increment,
`field_id` int(11) unsigned NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `field_id` (`field_id`)
) TYPE=MyISAM");

}

}

212 changes: 212 additions & 0 deletions fields/field.time.php
@@ -0,0 +1,212 @@
<?php

Class fieldTime extends Field{

const SIMPLE = 0;
const REGEXP = 1;
const RANGE = 3;
const ERROR = 4;

function __construct(&$parent){
parent::__construct($parent);
$this->_name = 'Time';
$this->_required = true;
$this->set('required', 'yes');
}

function isSortable(){
return true;
}

function canFilter(){
return true;
}

function allowDatasourceOutputGrouping(){
return true;
}

function allowDatasourceParamOutput(){
return true;
}

function canPrePopulate(){
return true;
}


public function buildSortingSQL(&$joins, &$where, &$sort, $order='ASC', $useIDFieldForSorting=false){

$sort_field = (!$useIDFieldForSorting ? 'ed' : 't' . $this->get('id'));

$joins .= "INNER JOIN `tbl_entries_data_".$this->get('id')."` AS `$sort_field` ON (`e`.`id` = `$sort_field`.`entry_id`) ";
$sort .= (strtolower($order) == 'random' ? 'RAND()' : "`$sort_field`.`seconds` $order");
}

function groupRecords($records){

if(!is_array($records) || empty($records)) return;

$groups = array($this->get('element_name') => array());

foreach($records as $r){
$data = $r->getData($this->get('id'));

$value = $data['value'];

if(!isset($groups[$this->get('element_name')][$value])){
$groups[$this->get('element_name')][$value] = array('attr' => array('value' => $value),
'records' => array(), 'groups' => array());
}

$groups[$this->get('element_name')][$value]['records'][] = $r;

}

return $groups;
}

function displaySettingsPanel(&$wrapper, $errors=NULL){
parent::displaySettingsPanel($wrapper, $errors);
$this->appendRequiredCheckbox($wrapper);
$this->appendShowColumnCheckbox($wrapper);
}

public function appendFormattedElement(&$wrapper, $data, $encode=false){
$element = new XMLElement($this->get('element_name'), $data['value']);
$element->setAttribute('seconds', (int)$data['seconds']);
$wrapper->appendChild($element);
}

public function prepareTableValue($data, XMLElement $link=NULL){

$value = $data['value'];

if($link){
$link->setValue($value);
return $link->generate();
}

else return $value;

}

function displayPublishPanel(&$wrapper, $data=NULL, $flagWithError=NULL, $fieldnamePrefix=NULL, $fieldnamePostfix=NULL){
$value = (int)$data['seconds'];

$label = Widget::Label($this->get('label'));
if($this->get('required') != 'yes') $label->appendChild(new XMLElement('i', 'Optional'));
$label->appendChild(Widget::Input('fields'.$fieldnamePrefix.'['.$this->get('element_name').']'.$fieldnamePostfix, self::timeIntToString($value)));

if($flagWithError != NULL) $wrapper->appendChild(Widget::wrapFormElementWithError($label, $flagWithError));
else $wrapper->appendChild($label);
}

public function displayDatasourceFilterPanel(&$wrapper, $data=NULL, $errors=NULL, $fieldnamePrefix=NULL, $fieldnamePostfix=NULL){
$wrapper->appendChild(new XMLElement('h4', $this->get('label') . ' <i>'.$this->Name().'</i>'));
$label = Widget::Label('Value');
$label->appendChild(Widget::Input('fields[filter]'.($fieldnamePrefix ? '['.$fieldnamePrefix.']' : '').'['.$this->get('id').']'.($fieldnamePostfix ? '['.$fieldnamePostfix.']' : ''), ($data ? General::sanitize($data) : NULL)));
$wrapper->appendChild($label);

$wrapper->appendChild(new XMLElement('p', 'To filter by ranges, use the format <code>HH:MM:SS to HH:MM:SS</code>', array('class' => 'help')));

}

public function checkPostFieldData($data, &$message, $entry_id=NULL){
$message = NULL;

if($this->get('required') == 'yes' && strlen($data) == 0){
$message = "This is a required field.";
return self::__MISSING_FIELDS__;
}

if(!preg_match('@^\d{1,2}:\d{1,2}(:\d+)?@', $data)){
$message = "Time must be entered in the format <code>HH:MM:SS</code>";
return self::__INVALID_FIELDS__;
}

return self::__OK__;
}

public function createTable(){

return $this->Database->query(

"CREATE TABLE IF NOT EXISTS `tbl_entries_data_" . $this->get('id') . "` (
`id` int(11) unsigned NOT NULL auto_increment,
`entry_id` int(11) unsigned NOT NULL,
`seconds` bigint(20) default NULL,
`value` varchar(20) default NULL,
PRIMARY KEY (`id`),
KEY `entry_id` (`entry_id`),
KEY `seconds` (`seconds`)
) TYPE=MyISAM;"

);
}

public function processRawFieldData($data, &$status, $simulate=false, $entry_id=NULL){

$status = self::__OK__;

return array(
'seconds' => self::timeStringToInt($data),
'value' => self::timeIntToString(self::timeStringToInt($data)),
);
}

public static function timeStringToInt($string){
preg_match_all('/^(\d{1,2}):(\d{1,2})(:(\d+))?$/', $string, $matches, PREG_SET_ORDER);
return (((int)$matches[0][1] * 3600) + ((int)$matches[0][2] * 60) + (int)$matches[0][4]);
}

public static function parseTimeInt($int){
$hours = floor(($int * (1/60)) * (1/60));
$minutes = floor(($int - ($hours * 3600)) * (1/60));
$seconds = $int - (($hours * 3600) + ($minutes * 60));

return array($hours, $minutes, $seconds);
}

public static function timeIntToString($int){

list($hours, $minutes, $seconds) = self::parseTimeInt($int);

$hours = max(0, (int)$hours);
$minutes = max(0, (int)$minutes);
$seconds = max(0, (int)$seconds);

return "$hours:".($minutes < 10 ? '0' : NULL)."$minutes:" . ($seconds < 10 ? '0' : NULL) . $seconds;
}

function buildDSRetrivalSQL($data, &$joins, &$where, $andOperation=false){

## Check its not a regexp
if(preg_match('/\bto\b/i', $data[0])){

$field_id = $this->get('id');

list($from, $to) = preg_split('/\bto\b/i', $data[0]);

$from = trim($from);
$to = trim($to);

$from_sec = self::timeStringToInt($from);
$to_sec = self::timeStringToInt($to);

$joins .= " LEFT JOIN `tbl_entries_data_$field_id` AS `t$field_id` ON (`e`.`id` = `t$field_id`.entry_id) ";
$where .= " AND (t$field_id.`value` > $from_sec AND t$field_id.`value` < $to_sec) ";

}

else{
$data[0] = self::timeStringToInt($data[0]);
parent::buildDSRetrivalSQL($data, $joins, $where, $andOperation);
}

return true;

}


}

0 comments on commit 09128af

Please sign in to comment.