Skip to content

Commit

Permalink
Add not-regexp support to all Core fields, updated Field::isFilterReg…
Browse files Browse the repository at this point in the history
…ex. Thanks to wisolman :)
  • Loading branch information
brendo committed Oct 30, 2010
1 parent cc0c6f2 commit c6c7fd6
Show file tree
Hide file tree
Showing 9 changed files with 670 additions and 610 deletions.
324 changes: 166 additions & 158 deletions symphony/lib/toolkit/class.field.php

Large diffs are not rendered by default.

12 changes: 10 additions & 2 deletions symphony/lib/toolkit/fields/field.author.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,22 @@ public function buildDSRetrivalSQL($data, &$joins, &$where, $andOperation = fals

if (self::isFilterRegex($data[0])) {
$this->_key++;
$pattern = str_replace('regexp:', '', $this->cleanValue($data[0]));

if (preg_match('/^regexp:/i', $data[0])) {
$pattern = preg_replace('/regexp:/i', null, $this->cleanValue($data[0]));
$regex = 'REGEXP';
} else {
$pattern = preg_replace('/not-?regexp:/i', null, $this->cleanValue($data[0]));
$regex = 'NOT REGEXP';
}

$joins .= "
LEFT JOIN
`tbl_entries_data_{$field_id}` AS t{$field_id}_{$this->_key}
ON (e.id = t{$field_id}_{$this->_key}.entry_id)
";
$where .= "
AND t{$field_id}_{$this->_key}.author_id REGEXP '{$pattern}'
AND t{$field_id}_{$this->_key}.author_id {$regex} '{$pattern}'
";

} elseif ($andOperation) {
Expand Down
117 changes: 58 additions & 59 deletions symphony/lib/toolkit/fields/field.checkbox.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php

Class fieldCheckbox extends Field {
function __construct(&$parent){
parent::__construct($parent);
Expand All @@ -9,67 +9,66 @@ function __construct(&$parent){
function canToggle(){
return true;
}

function allowDatasourceOutputGrouping(){
return true;
}

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')][$handle])){
$groups[$this->get('element_name')][$handle] = array('attr' => array('value' => $value),
'records' => array(), 'groups' => array());
}
}

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

}

return $groups;
}
}

function canFilter(){
return true;
}

public function canImport(){
return true;
}

function getToggleStates(){
return array('yes' => __('Yes'), 'no' => __('No'));
}

function toggleFieldData($data, $newState){
$data['value'] = $newState;
return $data;
}

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

$status = self::__OK__;

return array(
'value' => (strtolower($data) == 'yes' || strtolower($data) == 'on' ? 'yes' : 'no')
);

}

function buildSortingSQL(&$joins, &$where, &$sort, $order='ASC'){
$joins .= "LEFT OUTER JOIN `tbl_entries_data_".$this->get('id')."` AS `ed` ON (`e`.`id` = `ed`.`entry_id`) ";
$sort = 'ORDER BY ' . (in_array(strtolower($order), array('random', 'rand')) ? 'RAND()' : "`ed`.`value` $order");
}


public function buildDSRetrivalSQL($data, &$joins, &$where, $andOperation = false) {
$field_id = $this->get('id');
$default_state = ($this->get('default_state') == "on") ? 'yes' : 'no';
Expand All @@ -88,7 +87,7 @@ public function buildDSRetrivalSQL($data, &$joins, &$where, $andOperation = fals
$where .= "
AND (
t{$field_id}_{$this->_key}.value = '{$value}'
OR
OR
t{$field_id}_{$this->_key}.value IS NULL
)
";
Expand All @@ -102,19 +101,19 @@ public function buildDSRetrivalSQL($data, &$joins, &$where, $andOperation = fals
}
else {
if (!is_array($data)) $data = array($data);

foreach ($data as &$value) {
$value = $this->cleanValue($value);
}

$this->_key++;
$data = implode("', '", $data);
$joins .= "
LEFT JOIN
`tbl_entries_data_{$field_id}` AS t{$field_id}_{$this->_key}
ON (e.id = t{$field_id}_{$this->_key}.entry_id)
";

if(strpos($data, $default_state) !== false) {
$where .= "
AND (
Expand All @@ -130,105 +129,105 @@ public function buildDSRetrivalSQL($data, &$joins, &$where, $andOperation = fals
";
}
}

return true;
}

function displayDatasourceFilterPanel(&$wrapper, $data=NULL, $errors=NULL, $fieldnamePrefix=NULL, $fieldnamePostfix=NULL){

parent::displayDatasourceFilterPanel($wrapper, $data, $errors, $fieldnamePrefix, $fieldnamePostfix);

$existing_options = array('yes', 'no');

if(is_array($existing_options) && !empty($existing_options)){
$optionlist = new XMLElement('ul');
$optionlist->setAttribute('class', 'tags');

foreach($existing_options as $option) $optionlist->appendChild(new XMLElement('li', $option));

$wrapper->appendChild($optionlist);
}

}

function displayPublishPanel(&$wrapper, $data=NULL, $flagWithError=NULL, $fieldnamePrefix=NULL, $fieldnamePostfix=NULL){

if(!$data){
## TODO: Don't rely on $_POST
if(isset($_POST) && !empty($_POST)) $value = 'no';
elseif($this->get('default_state') == 'on') $value = 'yes';
else $value = 'no';
}

else $value = ($data['value'] == 'yes' ? 'yes' : 'no');

$label = Widget::Label();
$input = Widget::Input('fields'.$fieldnamePrefix.'['.$this->get('element_name').']'.$fieldnamePostfix, 'yes', 'checkbox', ($value == 'yes' ? array('checked' => 'checked') : NULL));

$label->setValue($input->generate(false) . ' ' . ($this->get('description') != NULL ? $this->get('description') : $this->get('label')));

$wrapper->appendChild($label);
$wrapper->appendChild($label);
}

public function appendFormattedElement(&$wrapper, $data, $encode=false, $mode=NULL, $entry_id=NULL) {
$value = ($data['value'] == 'yes' ? 'Yes' : 'No');

$wrapper->appendChild(new XMLElement($this->get('element_name'), ($encode ? General::sanitize($value) : $value)));
}

public function prepareTableValue($data, XMLElement $link=NULL){
return ($data['value'] == 'yes' ? __('Yes') : __('No'));
}

function isSortable(){
return true;
}

function commit(){

if(!parent::commit()) return false;

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

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

$fields = array();

$fields['field_id'] = $id;
$fields['default_state'] = ($this->get('default_state') ? $this->get('default_state') : 'off');
if(trim($this->get('description')) != '') $fields['description'] = $this->get('description');
Symphony::Database()->query("DELETE FROM `tbl_fields_".$this->handle()."` WHERE `field_id` = '$id' LIMIT 1");

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

}

function findDefaults(&$fields){
if(!isset($fields['default_state'])) $fields['default_state'] = 'off';
}

public function displaySettingsPanel(&$wrapper, $errors = null) {
parent::displaySettingsPanel($wrapper, $errors);
## Long Description

## Long Description
$label = Widget::Label(__('Long Description <i>Optional</i>'));
$label->appendChild(Widget::Input('fields['.$this->get('sortorder').'][description]', $this->get('description')));
$wrapper->appendChild($label);
$wrapper->appendChild($label);

## Checkbox Default State
$label = Widget::Label();
$input = Widget::Input('fields['.$this->get('sortorder').'][default_state]', 'on', 'checkbox');
if($this->get('default_state') == 'on') $input->setAttribute('checked', 'checked');
if($this->get('default_state') == 'on') $input->setAttribute('checked', 'checked');
$label->setValue(__('%s Checked by default', array($input->generate())));
$wrapper->appendChild($label);

$this->appendShowColumnCheckbox($wrapper);
$this->appendShowColumnCheckbox($wrapper);
}

function createTable(){

return Symphony::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,
Expand All @@ -237,14 +236,14 @@ function createTable(){
KEY `entry_id` (`entry_id`),
KEY `value` (`value`)
) ENGINE=MyISAM;"

);
}
}

public function getExampleFormMarkup(){
$label = Widget::Label($this->get('label'));
$label->appendChild(Widget::Input('fields['.$this->get('element_name').']', NULL, 'checkbox', ($this->get('default_state') == 'on' ? array('checked' => 'checked') : NULL)));

return $label;
}

Expand Down

0 comments on commit c6c7fd6

Please sign in to comment.