Skip to content

Commit

Permalink
Merge pull request #4 from vlad-ghita/master
Browse files Browse the repository at this point in the history
Cleanup & Symphony 2.3 compatiblity
  • Loading branch information
John Porter committed May 24, 2012
2 parents f29cc0c + 08b0887 commit 580fb0c
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 96 deletions.
2 changes: 1 addition & 1 deletion LICENCE
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ 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 -----
----- end license block -----
15 changes: 5 additions & 10 deletions README → README.markdown
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@

Incremental Number Field
------------------------------------
======================

Version: 1.0
Author: John Porter (john.porter@designermonkey.co.uk)
Build Date: 18th July 2011
Requirements: Symphony 2.2
## 1 About ##

A field that automatically increments it's value by one for each new entry.

[INSTALLATION]

## 1 Installation ##

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

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

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


[CHANGES]
46 changes: 23 additions & 23 deletions extension.driver.php
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
<?php

Class extension_incremental_number extends Extension{

public function about(){
return array('name' => 'Field: Incremental Number',
'version' => '1.0',
'release-date' => '2011-07-18',
'author' => array('name' => 'John Porter',
'email' => 'contact@designermonkey.co.uk')
);
}
Class extension_incremental_number extends Extension
{

public function uninstall(){
Symphony::Database()->query("DROP TABLE `tbl_fields_incremental_number`");
try{
Symphony::Database()->query("DROP TABLE `tbl_fields_incremental_number`");
}
catch( Exception $e ){
}

return true;
}


public function install(){

return Symphony::Database()->query("CREATE TABLE `tbl_fields_incremental_number` (
`id` int(11) unsigned NOT NULL auto_increment,
`field_id` int(11) unsigned NOT NULL,
`start_number` int(11) unsigned NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `field_id` (`field_id`)
) TYPE=MyISAM");

try{
Symphony::Database()->query(
"CREATE TABLE `tbl_fields_incremental_number` (
`id` int(11) unsigned NOT NULL auto_increment,
`field_id` int(11) unsigned NOT NULL,
`start_number` int(11) unsigned NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `field_id` (`field_id`)
) TYPE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;"
);
} catch( Exception $e ){
}

return true;
}

}

?>
20 changes: 18 additions & 2 deletions extension.meta.xml
Original file line number Diff line number Diff line change
@@ -1,19 +1,35 @@
<?xml version="1.0" encoding="UTF-8"?>
<extension id="incremental_number">
<extension id="incremental_number" status="released" xmlns="http://symphony-cms.com/schemas/extension/1.0">
<name>Incremental Number Field</name>

<description>Increment a number per entry in a section</description>

<repo type="github">https://github.com/designermonkey/incremental_number</repo>

<url type="discuss">http://symphony-cms.com/discuss/thread/81151/</url>

<types>
<type>Field Types</type>
</types>

<authors>
<author>
<name github="designermonkey" symphony="designermonkey">John Porter</name>
<website>http://designermonkey.co.uk</website>
</author>
<author>
<name github="vlad-ghita" symphony="vladG">Vlad Ghita</name>
<email>vlad_micutul@yahoo.com</email>
<website>http://www.xanderadvertising.com</website>
</author>
</authors>

<releases>
<release version="1.1.0" date="2012-05-24" min="2.3"><![CDATA[
* Cleanup & Symphony 2.3 compatibility.
]]></release>

<release version="1.0.0" date="2011-11-28" min="2.2" />
</releases>
</extension>

</extension>
104 changes: 44 additions & 60 deletions fields/field.incremental_number.php
Original file line number Diff line number Diff line change
@@ -1,36 +1,33 @@
<?php

Class fieldincremental_number extends Field{
Class fieldincremental_number extends Field
{

function __construct(){
parent::__construct();

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

function isSortable()
{
function isSortable(){
return true;
}

function canFilter()
{
function canFilter(){
return true;
}

function allowDatasourceParamOutput()
{
function allowDatasourceParamOutput(){
return true;
}

function canPrePopulate()
{
function canPrePopulate(){
return true;
}

public function displaySettingsPanel(&$wrapper, $errors=NULL)
{
public function displaySettingsPanel(&$wrapper, $errors = NULL){
parent::displaySettingsPanel($wrapper, $errors);

$label = Widget::Label(__('Start Number'));
Expand All @@ -39,50 +36,46 @@ public function displaySettingsPanel(&$wrapper, $errors=NULL)
$this->appendShowColumnCheckbox($wrapper);
}

public function displayPublishPanel(&$wrapper, $data=NULL, $flagWithError=NULL, $fieldnamePrefix=NULL, $fieldnamePostfix=NULL)
{
public function displayPublishPanel(&$wrapper, $data = NULL, $flagWithError = NULL, $fieldnamePrefix = NULL, $fieldnamePostfix = NULL){
$value = $data['value'];
$label = Widget::Label($this->get('label'));

//var_dump($data);
//var_dump($this->get('start_number'));die;

$label->appendChild(Widget::Input('fields'.$fieldnamePrefix.'['.$this->get('element_name').']'.$fieldnamePostfix, (strlen($value) != 0 ? $value : $this->getNewNumber()), null, array('readonly' => 'readonly')));
$label->appendChild(
Widget::Input(
'fields'.$fieldnamePrefix.'['.$this->get('element_name').']'.$fieldnamePostfix,
(string) (strlen($value) != 0 ? $value : $this->getNewNumber()),
'text',
array('readonly' => 'readonly')
)
);

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

public function processRawFieldData($data, &$status, $simulate=false, $entry_id=null) {
public function processRawFieldData($data, &$status, $message, $simulate = false, $entry_id = null){
if( !$data ) $data = $this->getNewNumber();

if (!$data) $data = $this->getNewNumber();

return parent::processRawFieldData($data, &$status, $simulate, $entry_id);
return parent::processRawFieldData($data, &$status, $message, $simulate, $entry_id);
}

public function getNewNumber()
{
public function getNewNumber(){
$last_num = Symphony::Database()->fetch("
SELECT `value`
FROM `tbl_entries_data_".$this->get('id')."`
ORDER BY `value` DESC LIMIT 1
");
//var_dump($last_num[0]);die;
if(!empty($last_num)){
return (int)$last_num[0]['value']+1;
}else{
return (int)$this->get('start_number');
}

return (int) (!empty($last_num)) ? $last_num[0]['value'] + 1 : $this->get('start_number');
}

public function commit()
{
if(!parent::commit()) return false;
public function commit(){
if( !parent::commit() ) return false;

$id = $this->get('id');
$value = $this->get('start_number');
$id = $this->get('id');
$value = $this->get('start_number');

if($id === false) return false;
if( $id === false ) return false;

$fields = array();

Expand All @@ -91,13 +84,12 @@ public function commit()

Symphony::Database()->query("DELETE FROM `tbl_fields_".$this->handle()."` WHERE `field_id` = '$id' LIMIT 1");

return Symphony::Database()->insert($fields, 'tbl_fields_' . $this->handle());
return Symphony::Database()->insert($fields, 'tbl_fields_'.$this->handle());

}

public function displayDatasourceFilterPanel(&$wrapper, $data=NULL, $errors=NULL, $fieldnamePrefix=NULL, $fieldnamePostfix=NULL)
{
$wrapper->appendChild(new XMLElement('h4', $this->get('label') . ' <i>'.$this->Name().'</i>'));
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);
Expand All @@ -106,58 +98,50 @@ public function displayDatasourceFilterPanel(&$wrapper, $data=NULL, $errors=NULL

}

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

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

if(strlen($data) > 0 && !is_numeric($data)){
if( strlen($data) > 0 && !is_numeric($data) ){
$message = 'Must be a number.';
return self::__INVALID_FIELDS__;
}

return self::__OK__;
}

public function createTable()
{
public function createTable(){
return Symphony::Database()->query(

"CREATE TABLE IF NOT EXISTS `tbl_entries_data_" . $this->get('id') . "` (
"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,
`value` int(11) default NULL,
PRIMARY KEY (`id`),
KEY `entry_id` (`entry_id`),
KEY `value` (`value`)
) TYPE=MyISAM;"
) TYPE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;"

);
}

function buildDSRetrivalSQL($data, &$joins, &$where, $andOperation=false)
{
if(preg_match('/^mysql:/i', $data[0])){
function buildDSRetrievalSQL($data, &$joins, &$where, $andOperation = false){
if( preg_match('/^mysql:/i', $data[0]) ){

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

$expression = str_replace(array('mysql:', 'value'), array('', " `t$field_id`.`value` " ), $data[0]);
$expression = str_replace(array('mysql:', 'value'), array('', " `t$field_id`.`value` "), $data[0]);

$joins .= " LEFT JOIN `tbl_entries_data_$field_id` AS `t$field_id` ON (`e`.`id` = `t$field_id`.entry_id) ";
$where .= " AND $expression ";

}

else parent::buildDSRetrivalSQL($data, $joins, $where, $andOperation);
else parent::buildDSRetrievalSQL($data, $joins, $where, $andOperation);

return true;

}

}

?>

0 comments on commit 580fb0c

Please sign in to comment.