Fix bug in PayPal PHP SDK Core - php

I am having trouble find the problem in figuring out the problem using merchant-sdk-php I found on github. For some reason its throwing this error. I tried looking at the code but I dont under stand it or see the problem.
Fatal error: Call to undefined method stdClass::toXMLString() in /home/content/08/10639508/html/wp-content/plugins/donation-manager/library/vendor/paypal/paypal-sdk-core-php-bc7822a/lib/PPXmlMessage.php on line 89
<?php
/**
* #author
*/
abstract class PPXmlMessage
{
/**
* #return string
*/
public function toSOAP()
{
return $this->toXMLString();
}
/**
* #return string
*/
public function toXMLString()
{
if (count($properties = get_object_vars($this)) >= 2 && array_key_exists('value', $properties)) {
$attributes = array();
foreach (array_keys($properties) as $property) {
if ($property === 'value') continue;
if (($annots = PPUtils::propertyAnnotations($this, $property)) && isset($annots['attribute'])) {
if (($propertyValue = $this->{$property}) === NULL || $propertyValue == NULL) {
$attributes[] = NULL;
continue;
}
$attributes[] = $property . '="' . PPUtils::escapeInvalidXmlCharsRegex($propertyValue) . '"';
}
}
if (count($attributes)) {
return implode(' ', $attributes) . '>' . PPUtils::escapeInvalidXmlCharsRegex($this->value);
}
}
$xml = array();
foreach ($properties as $property => $defaultValue) {
if (($propertyValue = $this->{$property}) === NULL || $propertyValue == NULL) {
continue;
}
if (is_array($defaultValue) || is_array($propertyValue)) {
foreach ($propertyValue as $item) {
if (!is_object($item)) {
$xml[] = $this->buildProperty($property, $item);
}else{
$xml[] = $this->buildProperty($property, $item);
}
}
} else {
$xml[] = $this->buildProperty($property, $propertyValue);
}
}
return implode($xml);
}
/**
* #param string $property
* #param PPXmlMessage|string $value
* #param string $namespace
* #return string
*/
private function buildProperty($property, $value, $namespace = 'ebl')
{
$annotations = PPUtils::propertyAnnotations($this, $property);
if (!empty($annotations['namespace'])) {
$namespace = $annotations['namespace'];
}
if (!empty($annotations['name'])) {
$property = $annotations['name'];
}
$el = '<' . $namespace . ':' . $property;
if (!is_object($value)) {
$el .= '>' . PPUtils::escapeInvalidXmlCharsRegex($value);
} else {
if (substr($value = $value->toXMLString(), 0, 1) === '<' || $value=='') {
$el .= '>' . $value;
} else {
$el .= ' ' . $value;
}
}
return $el . '</' . $namespace . ':' . $property . '>';
}
/**
* #param array $map
* #param string $prefix
*/
public function init(array $map = array(), $prefix = '')
{
if (empty($map)) {
return;
}
if (($first = reset($map)) && !is_array($first) && !is_numeric(key($map))) {
parent::init($map, $prefix);
return;
}
$propertiesMap = PPUtils::objectProperties($this);
$arrayCtr = array();
foreach ($map as $element) {
if (empty($element) || empty($element['name'])) {
continue;
} elseif (!array_key_exists($property = strtolower($element['name']), $propertiesMap)) {
if (!preg_match('~^(.+)[\[\(](\d+)[\]\)]$~', $property, $m)) {
continue;
}
$element['name'] = $m[1];
$element['num'] = $m[2];
}
$element['name'] = $propertiesMap[strtolower($element['name'])];
if(PPUtils::isPropertyArray($this, $element['name'])) {
$arrayCtr[$element['name']] = isset($arrayCtr[$element['name']]) ? ($arrayCtr[$element['name']]+1) : 0;
$element['num'] = $arrayCtr[$element['name']];
}
if (!empty($element["attributes"]) && is_array($element["attributes"])) {
foreach ($element["attributes"] as $key => $val) {
$element["children"][] = array(
'name' => $key,
'text' => $val,
);
}
if (isset($element['text'])) {
$element["children"][] = array(
'name' => 'value',
'text' => $element['text'],
);
}
$this->fillRelation($element['name'], $element);
} elseif (!empty($element['text'])) {
$this->{$element['name']} = $element['text'];
} elseif (!empty($element["children"]) && is_array($element["children"])) {
$this->fillRelation($element['name'], $element);
}
}
}
/**
* #param string $property
* #param array $element
*/
private function fillRelation($property, array $element)
{
if (!class_exists($type = PPUtils::propertyType($this, $property))) {
trigger_error("Class $type not found.", E_USER_NOTICE);
return; // just ignore
}
if (isset($element['num'])) { // array of objects
$this->{$property}[$element['num']] = $item = new $type();
$item->init($element['children']);
} else {
$this->{$property} = new $type();
$this->{$property}->init($element["children"]);
}
}
}

I had this exact same error (with my live application - it worked fine in my test code). I eventually realized that, when I copied over my test code into the live environment and changed my credentials and URLs from the sandbox ones to the live ones ... I somehow deleted a line of code initializing a PaymentDetailsType object before calling SetExpressCheckout. So this error seemed pretty unrelated, but it was just a problem with setting up the objects before making the call to PayPal.

Perchance are you using json_encode/decode on your PaymentDetail record? It seems that PayPal is very particular about the component classes; stdClass (which json_decode generates) won't cut it. Serialize works, but there are warnings about using it.

Related

How to fix "user is invalid" error, when adding user to a group with redmine API REST

I would like assign a user to a group with API REST.
But it doesn't work.
I use POST /groups/:id/users.:format syntax (see Rest Groups)
User with this id exists in redmine and group too.
In redmine log I can see:
Processing by GroupsController#add_users as XML
Parameters: {"group"=>{"user_id"=>"34"}, "key"=>"81aa228c55ac5cfe4264a566ef67ac27702da8eb", "id"=>"5"}
Current user: admin (id=1)
Rendering common/error_messages.api.rsb
Rendered common/error_messages.api.rsb (0.1ms)
Completed 422 Unprocessable Entity in 4ms (Views: 0.4ms | ActiveRecord: 1.3ms)
And in API's response:
Code Error :422
Message : User is invalid
In request body : id of user
I use ActiveResouce for REST API.
$method = 'users'
$options = array('user_id' => user's id to add)
/**
* Posts to a specified custom method on the current object via:
*
* POST /collection/id/method.xml
*/
function post ($method, $options = array (), $start_tag = false) {
$req = $this->site . $this->element_name_plural;
if ($this->_data['id']) {
$req .= '/' . $this->_data['id'];
}
$req .= '/' . $method . '.xml';
return $this->_send_and_receive ($req, 'POST', $options, $start_tag);
}
And this function for send request and parse the response :
/**
* Build the request, call _fetch() and parse the results.
*/
function _send_and_receive ($url, $method, $data = array (), $start_tag = false) {
$params = '';
$el = $start_tag ? $start_tag : $this->element_name; // Singular this time
if ($this->request_format == 'url') {
foreach ($data as $k => $v) {
if ($k != 'id' && $k != 'created-at' && $k != 'updated-at') {
$params .= '&' . $el . '[' . str_replace ('-', '_', $k) . ']=' . rawurlencode ($v);
}
}
$params = substr ($params, 1);
} elseif ($this->request_format == 'xml') {
$params = '<?xml version="1.0" encoding="UTF-8"?><' . $el . ">\n";
foreach ($data as $k => $v) {
if ($k != 'id' && $k != 'created-at' && $k != 'updated-at') {
$params .= $this->_build_xml ($k, $v);
}
}
$params .= '</' . $el . '>';
}
if ($this->extra_params !== false) {
if(strpos($url, '?'))
{
$url = $url .'&'.$this->extra_params;
}
else
{
$url = $url .'?'.$this->extra_params;
}
}
$this->request_body = $params;
$this->request_uri = $url;
$this->request_method = $method;
$res = $this->_fetch ($url, $method, $params);
if ($res === false) {
return $this;
}
// Keep splitting off any top headers until we get to the (XML) body:
while (strpos($res, "HTTP/") === 0) {
list ($headers, $res) = explode ("\r\n\r\n", $res, 2);
$this->response_headers = $headers;
$this->response_body = $res;
if (preg_match ('/HTTP\/[0-9]\.[0-9] ([0-9]+)/', $headers, $regs)) {
$this->response_code = $regs[1];
} else {
$this->response_code = false;
}
if (! $res) {
return $this;
} elseif ($res == ' ') {
$this->error = 'Empty reply';
return $this;
}
}
// parse XML response
$xml = new SimpleXMLElement ($res);
// normalize xml element name in case rails ressource contains an underscore
if (str_replace ('-', '_', $xml->getName ()) == $this->element_name_plural) {
// multiple
$res = array ();
$cls = get_class ($this);
foreach ($xml->children () as $child) {
$obj = new $cls;
foreach ((array) $child as $k => $v) {
$k = str_replace ('-', '_', $k);
if (isset ($v['nil']) && $v['nil'] == 'true') {
continue;
} else {
$obj->_data[$k] = $v;
}
}
$res[] = $obj;
}
return $res;
} elseif ($xml->getName () == 'errors') {
// parse error message
$this->error = $xml->error;
$this->errno = $this->response_code;
return false;
}
foreach ((array) $xml as $k => $v) {
$k = str_replace ('-', '_', $k);
if (isset ($v['nil']) && $v['nil'] == 'true') {
continue;
} else {
$this->_data[$k] = $v;
}
}
return $this;
}
Thank you

How to get html of a cell using PHPExcel?

I've tried to import an xlsx file to array, but noticed, that it contains simple text whereas if I open it in OpenOffice, I can see some words in the cell are bold.
#items: array:2 [
"url" => "theurl"
"meta_title" => "text with one bold word"
]
Is there a way to get somehow text with <b>one</b> bold word?
PHPExcel wiki says nothing about it. Its valueBinder applies only to csv and html files.
Thanks to Mark Baker. The HTML Writer code helps to create simple converter. Usage:
$richtextService = new RichTextService();
$value = $reader->getActiveSheet()->getCell('F2')->getValue();
$html = $richtextService->getHTML($value); // html in it
And the class:
namespace App\Services;
use PHPExcel_RichText;
use PHPExcel_RichText_Run;
use PHPExcel_Style_Font;
class RichTextService{
/**
* Takes array where of CSS properties / values and converts to CSS stri$
*
* #param array
* #return string
*/
private function assembleCSS($pValue = array())
{
$pairs = array();
foreach ($pValue as $property => $value) {
$pairs[] = $property . ':' . $value;
}
$string = implode('; ', $pairs);
return $string;
}
/**
* Create CSS style (PHPExcel_Style_Font)
*
* #param PHPExcel_Style_Font $pStyle PHPExcel_Style_Font
* #return array
*/
private function createCSSStyleFont(PHPExcel_Style_Font $pStyle)
{
// Construct CSS
$css = array();
// Create CSS
if ($pStyle->getBold()) {
$css['font-weight'] = 'bold';
}
if ($pStyle->getUnderline() != PHPExcel_Style_Font::UNDERLINE_NONE && $pStyle->getStrikethrough()) {
$css['text-decoration'] = 'underline line-through';
} elseif ($pStyle->getUnderline() != PHPExcel_Style_Font::UNDERLINE_NONE) {
$css['text-decoration'] = 'underline';
} elseif ($pStyle->getStrikethrough()) {
$css['text-decoration'] = 'line-through';
}
if ($pStyle->getItalic()) {
$css['font-style'] = 'italic';
}
$css['color'] = '#' . $pStyle->getColor()->getRGB();
$css['font-family'] = '\'' . $pStyle->getName() . '\'';
$css['font-size'] = $pStyle->getSize() . 'pt';
return $css;
}
/**
*
* #param PHPExcel_RichText|string $value
* #return string
*/
public function getHTML($value) {
if(is_string($value)) {
return $value;
}
$cellData = '';
if ($value instanceof PHPExcel_RichText) {
// Loop through rich text elements
$elements = $value->getRichTextElements();
foreach ($elements as $element) {
// Rich text start?
if ($element instanceof PHPExcel_RichText_Run) {
$cellData .= '<span style="' . $this->assembleCSS($this->createCSSStyleFont($element->getFont())) . '">';
if ($element->getFont()->getSuperScript()) {
$cellData .= '<sup>';
} elseif ($element->getFont()->getSubScript()) {
$cellData .= '<sub>';
}
}
// Convert UTF8 data to PCDATA
$cellText = $element->getText();
$cellData .= htmlspecialchars($cellText);
if ($element instanceof PHPExcel_RichText_Run) {
if ($element->getFont()->getSuperScript()) {
$cellData .= '</sup>';
} elseif ($element->getFont()->getSubScript()) {
$cellData .= '</sub>';
}
$cellData .= '</span>';
}
}
}
return str_replace("\n", "<br>\n", $cellData);
}
}
With this small adaptions this solution works with the successor PhpSpreadsheet:
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\RichText;
use PhpOffice\PhpSpreadsheet\RichText\Run;
use PhpOffice\PhpSpreadsheet\Style\Font;
class RichTextService {
/**
* Takes array where of CSS properties / values and converts to CSS stri$
*
* #param array
* #return string
*/
private function assembleCSS($pValue = array())
{
$pairs = array();
foreach ($pValue as $property => $value) {
$pairs[] = $property . ':' . $value;
}
$string = implode('; ', $pairs);
return $string;
}
/**
* Create CSS style (Font)
*
* #param Font $pStyle Font
* #return array
*/
private function createCSSStyleFont(Font $pStyle)
{
// Construct CSS
$css = array();
// Create CSS
if ($pStyle->getBold()) {
$css['font-weight'] = 'bold';
}
if ($pStyle->getUnderline() != Font::UNDERLINE_NONE && $pStyle->getStrikethrough()) {
$css['text-decoration'] = 'underline line-through';
} elseif ($pStyle->getUnderline() != Font::UNDERLINE_NONE) {
$css['text-decoration'] = 'underline';
} elseif ($pStyle->getStrikethrough()) {
$css['text-decoration'] = 'line-through';
}
if ($pStyle->getItalic()) {
$css['font-style'] = 'italic';
}
$css['color'] = '#' . $pStyle->getColor()->getRGB();
$css['font-family'] = '\'' . $pStyle->getName() . '\'';
$css['font-size'] = $pStyle->getSize() . 'pt';
return $css;
}
/**
*
* #param RichText|string $value
* #return string
*/
public function getHTML($value) {
if(is_string($value)) {
return $value;
}
$cellData = '';
if ($value instanceof PhpOffice\PhpSpreadsheet\RichText\RichText) {
// Loop through rich text elements
$elements = $value->getRichTextElements();
foreach ($elements as $element) {
// Rich text start?
print_r($element);
if ($element instanceof PhpOffice\PhpSpreadsheet\RichText\Run) {
$cellData .= '<span style="' . $this->assembleCSS($this->createCSSStyleFont($element->getFont())) . '">';
if ($element->getFont()->getSuperScript()) {
$cellData .= '<sup>';
} elseif ($element->getFont()->getSubScript()) {
$cellData .= '<sub>';
}
}
// Convert UTF8 data to PCDATA
$cellText = $element->getText();
$cellData .= htmlspecialchars($cellText);
if ($element instanceof PhpOffice\PhpSpreadsheet\RichText\Run) {
if ($element->getFont()->getSuperScript()) {
$cellData .= '</sup>';
} elseif ($element->getFont()->getSubScript()) {
$cellData .= '</sub>';
}
$cellData .= '</span>';
}
}
}
return str_replace("\n", "<br>\n", $cellData);
}
}

Get array's value by list of keys in PHP

I have a simple methods to access to variables by their names in the class:
function get_value($name) {
return isset($this->vars[$name]) ? $this->vars[$name] : NULL;
}
function set_value($name, $value) {
//Some operations with value
$this->vars[$name] = $value;
}
But i want that vars would be a multidimensional array. Something like:
//$names = array('blocks', 'top', 1)
function get_value($names) {
//Try to return $this->vars['blocks']['top'][1]
}
I can access to value using loop like here https://stackoverflow.com/a/5127874/7462321 But "set_value" code will be uglier. Please, help to to come up with an elegant implementation of this.
Would a function like this be fine?
function get_value($arr, $keys) {
if (!is_array($keys)) {
$keys = [$keys];
}
foreach ($keys as $key) {
if (!isset($arr[$key])) {
return NULL;
}
$arr = $arr[$key];
}
return $arr;
}
Call it like this:
get_value($array, [1, 2, 3])
or:
get_value($array, 1)
Would this fit your needs ?
I had implemented something like this this way :
<?php
class Settings {
// [...] Some stuff
/**
* Sets a value to the corresponding key
* #throws SettingsException If $key is not well written
* #param string $key The depth slahes key
* #param mixed The value to set
*/
public function setValue($key, $value){
if(strpos($key, '/') !== false){
if(($key = trim(preg_replace('#[/]+#', '/', $key), ' /')) === ''){
throw new SettingsException('"' . $key . '" is not valid');
}
$this->_setValue($key, $this->_currentGroup, $value);
} else {
$this->_currentGroup[$key] = $value;
}
$this->_saved = false;
return $this;
}
/**
* Return a setting value
* #throws SettingsException If the key doesn't exists or is null
* #param string $key The depth slashes key
* #return mixed The value corresponding to the key
*/
public function value($key){
if(strpos($key, '/')){
if(($key = trim(preg_replace('#[/]+#', '/', $key), ' /')) === ''){
throw new SettingsException('"' . $key . '" is not valid');
}
return $this->_value($key, $this->_currentGroup, $key);
} else if(isset($this->_currentGroup[$key])) {
return $this->_currentGroup[$key];
} else {
throw new SettingsException('"' . $key . '" is not a valid key');
}
}
private function _setValue($key, &$configDepth, $value){
if(($pos = strpos($key, '/'))){
if(!isset($configDepth[($k = substr($key, 0, $pos))])) {
$configDepth[$k] = array();
}
$this->_setValue(substr($key, $pos+1), $configDepth[$k], $value);
} else {
$configDepth[$key] = $value;
}
}
private function _value($key, &$configDepth, $prefix){
if(($pos = strpos($key, '/'))){
if(!isset($configDepth[($k = substr($key, 0, $pos))])) {
throw new SettingsException('The prefix "' . $prefix . '" doesn\'t exists');
}
return $this->_value(substr($key, $pos+1), $configDepth[$k], $prefix);
} else if(!isset($configDepth[$key])) {
throw new SettingsException('The prefix "' . $prefix . '" doesn\'t exists');
} else {
return $configDepth[$key];
}
}
}
Settings and getting value works like the above :
<?php
$s = new Settings;
$s->setValue('main/foo', 'bar'); // $s::_s[main][foo] = bar;
$main = $s->value('main'); // return Setting like an array(foo => bar);
$bar = $s->value('main/foo'); // return string value bar
//$s['main'] == $s->value('main');
// $s['main']['foo'] = 'bar' == $s->setValue('main/foo', 'bar');

str_replace() in DOM

My current code
$this->data = $this->result->RetrieveDocumentResult;
$this->dom = new DOMDocument();
$this->dom->strictErrorChecking = false;
$this->dom->formatOutput = true;
$this->dom->loadHTML(base64_decode($this->data));
$exceptions = array(
'a' => array('href'),
'img' => array('src')
);
$this->stripAttributes($exceptions);
$this->stripSpanTags();
file_put_contents('Recode/' . $flname . '.html', base64_decode($this->data));
}
public function stripAttributes(array $exceptions)
{
$xpath = new DOMXPath($this->dom);
if (false === ($elements = $xpath->query("//*"))) die('Xpath error!');
/** #var $element DOMElement */
foreach ($elements as $element) {
for ($i = $element->attributes->length; --$i >= 0;) {
$this->tag = $element->nodeName;
$this->attribute = $element->attributes->item($i)->nodeName;
if ($this->checkAttrExceptions($exceptions)) continue;
$element->removeAttribute($this->attribute);
}
}
$this->data = base64_encode($this->dom->saveHTML());
}
public function checkAttrExceptions(array $exceptions)
{
foreach ($exceptions as $tag => $attributes) {
if (empty($attributes) || !is_array($attributes)) {
die('Attributes not set!');
}
foreach ($attributes as $attribute) {
if ($tag === $this->tag && $attribute === $this->attribute) {
return true;
}
}
}
return false;
}
/**
* Strip SPAN tags from current DOM document
*
* #return void
*/
/**
* Strip SPAN tags from current DOM document
*
* #return void
*/
protected function stripSpanTags ()
{
$nodes = $this->dom->getElementsByTagName('span');
while ($span = $nodes->item(0)) {
$replacement = $this->dom->createDocumentFragment();
while ($inner = $span->childNodes->item(0)) {
$replacement->appendChild($inner);
}
$span->parentNode->replaceChild($replacement, $span);
}
$this->data = base64_encode($this->dom->saveHTML());
}
}
Want to remove all in HTML did the following
$html = str_replace(' ', '', $html);
But confused how and where to add this to the first set of codes .. Help me please
Also this should not override previous tag filters in first set of codes
Found Solution ., The following code works
$this->result = $this->soap->RetrieveDocument(
array('format' => 'html')
);
$this->data = $this->result->RetrieveDocumentResult;
$this->dom = new DOMDocument();
$this->dom->strictErrorChecking = false;
$this->dom->formatOutput = true;
$this->dom->loadHTML(base64_decode($this->data));
$exceptions = array(
'a' => array('href'),
'img' => array('src')
);
$this->stripAttributes($exceptions);
$this->stripSpanTags();
$decoded = base64_decode($this->data);
$decoded = $this->stripNonBreakingSpaces($decoded);
file_put_contents('Recode/' . $flname . '.html', $decoded);
}
public function stripAttributes(array $exceptions)
{
$xpath = new DOMXPath($this->dom);
if (false === ($elements = $xpath->query("//*"))) die('Xpath error!');
/** #var $element DOMElement */
foreach ($elements as $element) {
for ($i = $element->attributes->length; --$i >= 0;) {
$this->tag = $element->nodeName;
$this->attribute = $element->attributes->item($i)->nodeName;
if ($this->checkAttrExceptions($exceptions)) continue;
$element->removeAttribute($this->attribute);
}
}
$this->data = base64_encode($this->dom->saveHTML());
}
public function checkAttrExceptions(array $exceptions)
{
foreach ($exceptions as $tag => $attributes) {
if (empty($attributes) || !is_array($attributes)) {
die('Attributes not set!');
}
foreach ($attributes as $attribute) {
if ($tag === $this->tag && $attribute === $this->attribute) {
return true;
}
}
}
return false;
}
/**
* Strip SPAN tags from current DOM document
*
* #return void
*/
/**
* Strip SPAN tags from current DOM document
*
* #return void
*/
protected function stripSpanTags ()
{
$nodes = $this->dom->getElementsByTagName('span');
while ($span = $nodes->item(0)) {
$replacement = $this->dom->createDocumentFragment();
while ($inner = $span->childNodes->item(0)) {
$replacement->appendChild($inner);
}
$span->parentNode->replaceChild($replacement, $span);
}
$this->data = base64_encode($this->dom->saveHTML());
}
/**
* Replace all entities within a string with a regular space
*
* #param string $string Input string
*
* #return string
*/
protected function stripNonBreakingSpaces ($string)
{
return str_replace(' ', ' ', $string);
}
}

Non-abstract method showing as abstract in PHP fatal error message

I am getting this PHP fatal error message: "Fatal error: Class PT_Fieldtype contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (EE_Fieldtype::display_field) in /.../pt_fieldtype.php on line 148"
However, when I open the file and do a search, the word "abstract" is nowhere to be found at all. Thanks for the help!
Edit: Here's the code in question. My bad for not showing it before.
<?php if (! defined('BASEPATH')) exit('No direct script access allowed');
if (! defined('PT_FIELD_PACK_VER'))
{
// get the version from config.php
require PATH_THIRD.'pt_field_pack/config.php';
define('PT_FIELD_PACK_VER', $config['version']);
}
/**
* P&T Fieldtype Base Class
*
* #package P&T Field Pack
* #author Brandon Kelly <brandon#pixelandtonic.com>
* #copyright Copyright (c) 2010 Pixel & Tonic, LLC
*/
class PT_Fieldtype extends EE_Fieldtype {
/**
* PT_Fieldtype Constructor
*/
function PT_Fieldtype()
{
parent::EE_Fieldtype();
}
// --------------------------------------------------------------------
/**
* Options Setting
*/
function options_setting($options=array(), $indent = '')
{
$r = '';
foreach($options as $name => $label)
{
if ($r !== '') $r .= "\n";
// is this just a blank option?
if (! $name && ! $label) $name = $label = ' ';
$r .= $indent . htmlentities($name);
// is this an optgroup?
if (is_array($label)) $r .= "\n".$this->options_setting($label, $indent.' ');
else if ($name != $label) $r .= ' : '.$label;
}
return $r;
}
// --------------------------------------------------------------------
/**
* Save Options Setting
*/
function save_options_setting($options = '', $total_levels = 1)
{
// prepare options
$options = preg_split('/[\r\n]+/', $options);
foreach($options as &$option)
{
$option_parts = preg_split('/\s:\s/', $option, 2);
$option = array();
$option['indent'] = preg_match('/^\s+/', $option_parts[0], $matches) ? strlen(str_replace("\t", ' ', $matches[0])) : 0;
$option['name'] = trim($option_parts[0]);
$option['value'] = isset($option_parts[1]) ? trim($option_parts[1]) : $option['name'];
}
return $this->_structure_options($options, $total_levels);
}
/**
* Structure Options
*/
private function _structure_options(&$options, $total_levels, $level = 1, $indent = -1)
{
$r = array();
while ($options)
{
if ($indent == -1 || $options[0]['indent'] > $indent)
{
$option = array_shift($options);
$children = (! $total_levels OR $level < $total_levels)
? $this->_structure_options($options, $total_levels, $level+1, $option['indent']+1)
: FALSE;
$r[(string)$option['name']] = $children ? $children : (string)$option['value'];
}
else if ($options[0]['indent'] <= $indent)
{
break;
}
}
return $r;
}
// --------------------------------------------------------------------
/**
* Prep Iterators
*/
function prep_iterators(&$tagdata)
{
// find {switch} tags
$this->_switches = array();
$tagdata = preg_replace_callback('/'.LD.'switch\s*=\s*([\'\"])([^\1]+)\1'.RD.'/sU', array(&$this, '_get_switch_options'), $tagdata);
$this->_count_tag = 'count';
$this->_iterator_count = 0;
}
/**
* Get Switch Options
*/
function _get_switch_options($match)
{
global $FNS;
$marker = LD.'SWITCH['.$FNS->random('alpha', 8).']SWITCH'.RD;
$this->_switches[] = array('marker' => $marker, 'options' => explode('|', $match[2]));
return $marker;
}
/**
* Parse Iterators
*/
function parse_iterators(&$tagdata)
{
// {switch} tags
foreach($this->_switches as $i => $switch)
{
$option = $this->_iterator_count % count($switch['options']);
$tagdata = str_replace($switch['marker'], $switch['options'][$option], $tagdata);
}
// update the count
$this->_iterator_count++;
// {count} tags
$tagdata = $this->EE->TMPL->swap_var_single($this->_count_tag, $this->_iterator_count, $tagdata);
}
}
// ====================================================================
/**
* P&T Multi Fieldtype Base Class
*
* #package P&T Field Pack
* #author Brandon Kelly <brandon#pixelandtonic.com>
* #copyright Copyright (c) 2010 Pixel & Tonic, LLC
*/
class PT_Multi_Fieldtype extends PT_Fieldtype {
var $default_field_settings = array(
'options' => array(
'Option 1' => 'Option 1',
'Option 2' => 'Option 2',
'Option 3' => 'Option 3'
)
);
var $default_cell_settings = array(
'options' => array(
'Opt 1' => 'Opt 1',
'Opt 2' => 'Opt 2'
)
);
var $default_tag_params = array(
'sort' => '',
'backspace' => '0'
);
var $total_option_levels = 1;
// --------------------------------------------------------------------
/**
* Display Field Settings
*/
function display_settings($data)
{
// load the language file
$this->EE->lang->loadfile($this->class);
$options = isset($data['options']) ? $data['options'] : array();
$input_name = $this->class.'_options';
$this->EE->table->add_row(
lang($this->class.'_options', $input_name) . '<br />'
. lang('field_list_instructions') . '<br /><br />'
. lang('option_setting_examples'),
'<textarea id="'.$input_name.'" name="'.$input_name.'" rows="6">'.$this->options_setting($options).'</textarea>'
);
}
/**
* Display Cell Settings
*/
function display_cell_settings($data)
{
// load the language file
$this->EE->lang->loadfile($this->class);
$options = isset($data['options']) ? $data['options'] : array();
return array(
array(
lang($this->class.'_options'),
'<textarea class="matrix-textarea" name="options" rows="4">'.$this->options_setting($options).'</textarea>'
)
);
}
// --------------------------------------------------------------------
/**
* Save Field Settings
*/
function save_settings($data)
{
$post = $this->EE->input->post($this->class.'_options');
// replace quotes
$post = str_replace('"', '"', $post);
return array(
'options' => $this->save_options_setting($post, $this->total_option_levels)
);
}
/**
* Save Cell Settings
*/
function save_cell_settings($settings)
{
// replace quotes
$settings['options'] = str_replace('"', '"', $settings['options']);
$settings['options'] = $this->save_options_setting($settings['options'], $this->total_option_levels);
return $settings;
}
// --------------------------------------------------------------------
/**
* Prep Field Data
*
* Ensures $field_data is an array.
*/
function prep_field_data(&$data)
{
if (! is_array($data))
{
$data = array_filter(preg_split("/[\r\n]+/", $data));
}
}
// --------------------------------------------------------------------
/**
* Display Field
*/
function display_field($data)
{
if (is_string($data)) $data = html_entity_decode($data);
return $this->_display_field($data, $this->field_name);
}
/**
* Display Cell
*/
function display_cell($data)
{
return $this->_display_field($data, $this->cell_name);
}
// --------------------------------------------------------------------
/**
* Save
*/
function save($data)
{
// replace quotes
return str_replace('"', '"', $data);
}
/**
* Save Cell
*/
function save_cell($data)
{
// replace quotes
return $this->save($data);
}
// --------------------------------------------------------------------
/**
* Find Options
*/
private function _find_option($needle, $haystack)
{
foreach ($haystack as $key => $value)
{
$r = $value;
if ($needle == $key OR (is_array($value) AND (($r = $this->_find_option($needle, $value)) !== FALSE)))
{
return $r;
}
}
return FALSE;
}
// --------------------------------------------------------------------
/**
* Replace Tag
*/
function replace_tag($data, $params = array(), $tagdata = FALSE)
{
if (! isset($this->settings['options']) || ! $this->settings['options'])
{
return $data;
}
if (! $tagdata)
{
return $this->replace_ul($data, $params);
}
$this->prep_field_data($data);
$r = '';
if ($this->settings['options'] && $data)
{
// optional sorting
if (isset($params['sort']) && $params['sort'])
{
$sort = strtolower($params['sort']);
if ($sort == 'asc')
{
sort($data);
}
else if ($sort == 'desc')
{
rsort($data);
}
}
// offset and limit
if (isset($params['offset']) || isset($params['limit']))
{
$offset = isset($params['offset']) ? $params['offset'] : 0;
$limit = isset($params['limit']) ? $params['limit'] : count($data);
$data = array_splice($data, $offset, $limit);
}
// prepare for {switch} and {count} tags
$this->prep_iterators($tagdata);
foreach($data as $option_name)
{
if (($option = $this->_find_option($option_name, $this->settings['options'])) !== FALSE)
{
// copy $tagdata
$option_tagdata = $tagdata;
// simple var swaps
$option_tagdata = $this->EE->TMPL->swap_var_single('option', $option, $option_tagdata);
$option_tagdata = $this->EE->TMPL->swap_var_single('option_name', $option_name, $option_tagdata);
// parse {switch} and {count} tags
$this->parse_iterators($option_tagdata);
$r .= $option_tagdata;
}
}
if (isset($params['backspace']) && $params['backspace'])
{
$r = substr($r, 0, -$params['backspace']);
}
}
return $r;
}
// --------------------------------------------------------------------
/**
* Unordered List
*/
function replace_ul($data, $params = array())
{
return "<ul>\n"
. $this->replace_tag($data, $params, " <li>{option}</li>\n")
. '</ul>';
}
/**
* Ordered List
*/
function replace_ol($data, $params = array())
{
return "<ol>\n"
. $this->replace_tag($data, $params, " <li>{option}</li>\n")
. '</ol>';
}
// --------------------------------------------------------------------
/**
* All Options
*/
function replace_all_options($data, $params = array(), $tagdata = FALSE, $options = FALSE, $iterator_count = 0)
{
if (! $tagdata)
{
return "<ul>\n"
. $this->replace_all_options($data, $params, " <li>{option}</li>\n")
. "</ul>";
}
PT_Multi_Fieldtype::prep_field_data($data);
$r = '';
if ($options === FALSE)
{
$options = $this->settings['options'];
}
if ($options)
{
// optional sorting
if (isset($params['sort']) && $params['sort'])
{
$sort = strtolower($params['sort']);
if ($sort == 'asc')
{
asort($options);
}
else if ($sort == 'desc')
{
arsort($options);
}
}
// prepare for {switch} and {count} tags
$this->prep_iterators($tagdata);
$this->_iterator_count += $iterator_count;
foreach($options as $option_name => $option)
{
if (is_array($option))
{
$sub_params = array_merge($params, array('backspace' => '0'));
$r .= $this->replace_all_options($data, $sub_params, $tagdata, $option, $this->_iterator_count);
}
else
{
// copy $tagdata
$option_tagdata = $tagdata;
// simple var swaps
$option_tagdata = $this->EE->TMPL->swap_var_single('option', $option, $option_tagdata);
$option_tagdata = $this->EE->TMPL->swap_var_single('option_name', $option_name, $option_tagdata);
$option_tagdata = $this->EE->TMPL->swap_var_single('selected', (in_array($option_name, $data) ? 1 : 0), $option_tagdata);
// parse {switch} and {count} tags
$this->parse_iterators($option_tagdata);
$r .= $option_tagdata;
}
}
if (isset($params['backspace']) && $params['backspace'])
{
$r = substr($r, 0, -$params['backspace']);
}
}
return $r;
}
// --------------------------------------------------------------------
/**
* Is Selected?
*/
function replace_selected($data, $params = array())
{
$this->prep_field_data($data);
return (isset($params['option']) AND in_array($params['option'], $data)) ? 1 : 0;
}
/**
* Total Selections
*/
function replace_total_selections($data, $params = array())
{
$this->prep_field_data($data);
return $field_data ? (string) count($data) : '0';
}
}
The answer of your question is on line 148 of this file...
Maybe you are using a class with abstract methods without inheriting them that is rejected, make sure you implement all of them and after show us some code. ;-)

Categories