Skip to content

Commit

Permalink
Updated no_results
Browse files Browse the repository at this point in the history
- Updated no_results with conditions with _prep_no_results() function
- EE4 compatible
  • Loading branch information
ignetic committed Apr 22, 2020
1 parent a77eaa0 commit 75dd284
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 76 deletions.
11 changes: 7 additions & 4 deletions in_array/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ and pipe or comma delimited values in the array parameter:

{exp:in_array value="Cow" array="The|Cow|Jumped|Over|The|Moon"}

Typical use:

#### Typical use: ####

{if '{exp:in_array value="2" array="1|2|3"}'}
We found your value
Expand All @@ -20,22 +21,24 @@ Typical use:
We did not find your value
{/if}

Tag Pair:

#### Tag Pair: ####

{exp:in_array:pair value="2" array="1|2|3"}
{if no_results}Value not found{/if}
We found your value
{/exp:in_array:pair}

Alternative no_results: Note that this only works with no "if" conditions inside it.

#### Alternative no_results ####

{exp:in_array:pair value="2" array="1|2|3"}
{if in_array:no_results}Value not found{/if}
We found your value
{/exp:in_array:pair}


Available parameters:
#### Available parameters: ####

value="X" : The value to find in the array
not="X" : The value not to find in the array
Expand Down
2 changes: 1 addition & 1 deletion in_array/addon.setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
'author_url' => '',
'name' => 'In Array',
'description' => 'Searches for a value within given pipe or comma separated values',
'version' => '1.4',
'version' => '1.5',
'namespace' => '\\',
'docs_url' => ''
);
5 changes: 5 additions & 0 deletions in_array/changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ Changelog for In Array
==================================


## 1.5

- Updated no_results with conditions with _prep_no_results() function
- EE4 compatible

## 1.4

- ExpressionEngine 3 compatibility
Expand Down
105 changes: 34 additions & 71 deletions in_array/pi.in_array.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

$plugin_info = array(
'pi_name' => 'In Array',
'pi_version' => '1.4',
'pi_version' => '1.5',
'pi_author' => 'Simon Andersohn',
'pi_author_url' => '',
'pi_description' => 'Searches for a value within given pipe or comma separated values',
Expand Down Expand Up @@ -32,13 +32,15 @@ public function pair()

$return = $this->compare_array();

$this->_prep_no_results();

if ($return === TRUE)
{
return $this->EE->TMPL->tagdata;
}
else
{
return $this->no_results();
return $this->EE->TMPL->no_results();
}
}

Expand Down Expand Up @@ -108,27 +110,32 @@ private function check_bool($var) {
return false;
}
}


private function no_results()
private function _prep_no_results()
{

$tagdata = $this->EE->TMPL->tagdata;
$tag_name = strtolower(get_class($this)).':no_results';

$pattern = '#' .LD .'if ' .$tag_name .RD .'(.*?)' .LD .'/if' .RD .'#s';

if (is_string($tagdata) && is_string($tag_name)
&& preg_match($pattern, $tagdata, $matches)
)
{
return $matches[1];
// Shortcut to tagdata
$open = strtolower(get_class($this)).':no_results';
$td = ee()->TMPL->tagdata;
$open = 'if '. $open;
$close = '/if';
// Check if there is a custom no_results conditional

if (strpos($td, $open) !== FALSE
&& preg_match('#' .LD .$open .RD .'(.*?)' .LD .$close .RD .'#s', $td, $match)
){
ee()->TMPL->log_item("Prepping {$open} conditional");
// Check if there are conditionals inside of that
if (stristr($match[1], LD.'if'))
{
$match[0] = ee()->functions->full_tag($match[0], $td, LD.'if', LD.'\/if'.RD);
}
// Set template's no_results data to found chunk
ee()->TMPL->no_results = substr($match[0], strlen(LD.$open.RD), -strlen(LD.$close.RD));
// Remove no_results conditional from tagdata
$td = str_replace($match[0], '', $td);
}

return $this->EE->TMPL->no_results();
}




// --------------------------------------------------------------------

Expand All @@ -140,60 +147,16 @@ private function no_results()
* @access public
* @return string
*/

public static function usage()
{
ob_start();
?>

Searches for a value within given pipe or comma separated values

To use this plugin, enter the value to search for in the value parameter
and pipe or comma delimited values in the array parameter:

{exp:in_array value="3" array="2|4|5|20"}

{exp:in_array value="Cow" array="The|Cow|Jumped|Over|The|Moon"}

Typical use:

{if '{exp:in_array value="2" array="1|2|3"}'}
We found your value
{/if}

{if '{exp:in_array not="4|5" array="1|2|3"}'}
We did not find your value
{/if}

Tag Pair:

{exp:in_array:pair value="2" array="1|2|3"}
{if no_results}Value not found{/if}
We found your value
{/exp:in_array:pair}

Alternative no_results: Note that this only works with no "if" conditions inside it.

{exp:in_array:pair value="2" array="1|2|3"}
{if in_array:no_results}Value not found{/if}
We found your value
{/exp:in_array:pair}


Available parameters:

value="X" : The value to find in the array
not="X" : The value not to find in the array
array="1|2|3|4" : the values in the array to search
delimiter="|" : change the default pipe delimiter
case_insensitive="y" : make the search case insensitive


<?php
$buffer = ob_get_contents();

ob_end_clean();

return $buffer;
$output = '';
$file = __DIR__ .'/README.md';
if (file_exists($file))
{
$output = file_get_contents( __DIR__ .'/README.md');
}
return $output;
}

// --------------------------------------------------------------------
Expand Down

0 comments on commit 75dd284

Please sign in to comment.