MoneyField isChanged always returning true - silverstripe

I have a DataObject Confirmation that looks like this:
<?php
class Confirmation extends DataObject
{
private static $db = array(
'Reimbursement' => 'Money',
'SomeText' => 'Varchar(255)'
);
private static $has_one = array(
'Page' => 'Page'
);
public function getCMSFields()
{
$fields = parent::getCMSFields();
$reimbursementField = MoneyField::create('Reimbursement');
$someTextField = TextField::create('SomeText');
$reimbursementField->setAllowedCurrencies(array('SEK'));
$fields->addFieldsToTab(
'Root.Main',
array(
$reimbursementField,
$someTextField
)
);
return $fields;
}
public function onBeforeWrite()
{
parent::onBeforeWrite();
if($this->isChanged('Reimbursement')) SS_Log::log( print_r ( 'changed', true ), SS_Log::WARN );
}
}
if($this->isChanged('Reimbursement')) in the onBeforeWrite() function will always evaluate to true. Regardless of what is being changed. Even when saving without making any changes - it will fire the log function.
Edit: Checking the stacktrace with SS_Backtrace::backtrace() in Money.php's setValue() function I find that every time I save the Confirmation DataObject, setValue() is called without passing the $markChanged parameter set to false, from DataObject.php line 1281, resulting in the $isChanged flag being set to true, and the field always behaving as if it has changed.
When logging DataObject's getChangedFields() function I get the output below (when saving without making any changes). Notice the Reimbursement field being present, and before being empty. What am I missing here?
[22-Mar-2016 16:28:07] Warning at framework/model/DataObject.php line 2597: Array
(
[Reimbursement] => Array
(
[before] =>
[after] => Money Object
(
[currency:protected] => SEK
[amount:protected] => 25000
[isChanged:protected] => 1
[locale:protected] =>
[currencyLib:protected] => Zend_Currency Object
(
[_options:protected] => Array
(
[position] => 8
[script] =>
[format] =>
[display] => 2
[precision] => 2
[name] => svensk krona
[currency] => SEK
[symbol] => kr
[locale] => sv_SE
[value] => 0
[service] =>
[tag] => Zend_Locale
)
)
[allowedCurrencies:protected] =>
[value:protected] =>
[tableName:protected] =>
[name:protected] => Reimbursement
[arrayValue:protected] =>
[defaultVal:protected] =>
[failover:protected] =>
[customisedObject:protected] =>
[objCache:ViewableData:private] => Array
(
)
[class] => Money
[extension_instances:protected] => Array
(
)
[beforeExtendCallbacks:protected] => Array
(
)
[afterExtendCallbacks:protected] => Array
(
)
)
[level] => 2
)
)

Related

What data is POSTed to a shippo webhook?

I would like to implement a shippo webhook in order to know the delivery status of my shipments, their documentation is a little unclear... I don't know what information will be passed to my script
I have setup a test URL and a live one and have added those to my account, in API -> Webhooks.
Whenever my script is requested either via the live or test URLs I get empty arrays, no data. Please help me figure this out. Anyone from Shippo??
Here is what I have so far:
<?php
namespace MW\PublicBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class ShippoController extends Controller
{
/**
* #Route("/shippo/", name="shippo_web_hook")
* #Method("GET|POST")
*/
public function webHookAction(Request $request)
{
if ($request->getMethod() == 'POST'){
$post = $request->request->all();
} elseif ($request->getMethod() == 'GET'){
$post = $request->query->all();
}
file_put_contents(__DIR__ . '/shippo.txt', print_r($post,true));
$mailer = $this->get('swiftmailer.mailer.transactional');
$messageObject = \Swift_Message::newInstance()
->setSubject('Shippo Webhook Posted DATA')
->setFrom('emai#example.com')
->setTo('email#example.com')
->setBody(print_r($post,true) . "\n" . print_r($_REQUEST,true) . "\n" . print_r($_POST,true));
try {
$mailer->send($messageObject);
} catch (\Exception $e){
}
return new Response('OK');
}
}
As you can see I should be able to catch some incoming data but I get nothing but empty arrays..
Indeed my script is receiving straight up JSON, thank you to mootrichard for sharing the requestb.in tool, with it I was able to see all the headers and data sent, just for future reference this is what I got.
namespace MW\PublicBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class ShippoController extends Controller
{
/**
* #Route("/shippo/", name="shippo_web_hook")
* #Method("GET|POST")
*/
public function webHookAction(Request $request)
{
$headers = $request->headers->all();
$content = $request->getContent();
if (!empty($content))
{
$post = json_decode($content, true);
}
if (isset($headers['x-shippo-event'][0]) && $headers['x-shippo-event'][0] == 'track_updated' &&
(isset($headers['content-type'][0]) && $headers['content-type'][0] == 'application/json')){
if (count($post) > 0) {
file_put_contents(__DIR__ . '/shippo.txt', print_r($headers, true) . "\n\n\n" . print_r($post, true));
}
}
return new Response('OK');
}
}
And the contents of shippo.txt is:
Array
(
[host] => Array
(
[0] => ******
)
[user-agent] => Array
(
[0] => python-requests/2.9.1
)
[content-length] => Array
(
[0] => 1021
)
[accept] => Array
(
[0] => */*
)
[accept-encoding] => Array
(
[0] => gzip, deflate
)
[content-type] => Array
(
[0] => application/json
)
[shippo-api-version] => Array
(
[0] => 2014-02-11
)
[x-forwarded-for] => Array
(
[0] => **.**.***.**
)
[x-original-host] => Array
(
[0] => *****
)
[x-shippo-event] => Array
(
[0] => track_updated
)
[x-php-ob-level] => Array
(
[0] => 0
)
)
Array
(
[messages] => Array
(
)
[carrier] => usps
[tracking_number] => 123
[address_from] => Array
(
[city] => Las Vegas
[state] => NV
[zip] => 89101
[country] => US
)
[address_to] => Array
(
[city] => Spotsylvania
[state] => VA
[zip] => 22551
[country] => US
)
[eta] => 2017-09-05T01:35:10.231
[original_eta] => 2017-09-05T01:35:10.231
[servicelevel] => Array
(
[token] => usps_priority
[name] => Priority Mail
)
[metadata] => Shippo test webhook
[tracking_status] => Array
(
[status] => UNKNOWN
[object_created] => 2017-08-31T01:35:10.240
[status_date] => 2017-08-31T01:35:10.240
[object_id] => ac0e0c060d6e43b295c460414ebc831f
[location] => Array
(
[city] => Las Vegas
[state] => NV
[zip] => 89101
[country] => US
)
[status_details] => testing
)
[tracking_history] => Array
(
[0] => Array
(
[status] => UNKNOWN
[object_created] => 2017-08-31T01:35:10.240
[status_date] => 2017-08-31T01:35:10.240
[object_id] => ac0e0c060d6e43b295c460414ebc831f
[location] => Array
(
[city] => Las Vegas
[state] => NV
[zip] => 89101
[country] => US
)
[status_details] => testing
)
)
[transaction] =>
)
According to their documentation they're just sending you a straight JSON response, not a key/value pair of data that you can get from the request parameters. You would want to do something like this instead:
$data = json_decode($request->getContent(), true);
This documentation is from Silex but it's using the same components as Symfony to receive a accept a JSON request body.

Laravel ErrorException Undefined offset: 0

I am facing this error in laravel 5.
Here is the controller function in which I am facing the error (in the line where I do ($vendor->client[0]->id) :
public function show($username) {
Log::info('Vendors Controller : show function with '.$username);
$vendor = VendorProfile::where('username', $username)->first();
$output = print_r($vendor,1);
Log::info($output);
if($vendor) {
Log::info('client '. $vendor->client);
$client = Client::find($vendor->client[0]->id);
$title = $client->profile->company_name;
$output is printed as:
[2015-06-15 21:34:43] local.INFO: App\models\VendorProfile Object
(
[table:protected] => vendor_profile
[guarded:protected] => Array
(
[0] => id
)
[morphClass:protected] => MorphVendorProfile
[connection:protected] =>
[primaryKey:protected] => id
[perPage:protected] => 15
[incrementing] => 1
[timestamps] => 1
[attributes:protected] => Array
(
[id] => 16
[first_name] => some name
[last_name] =>
[company_name] => some name
[contact_number] => 1234567890
[username] => username
[profile_photo] =>
[photo_mime_type] =>
[cover_photo] =>
[cover_photo_mime_type] =>
[address] =>
[city_id] => 1
[zip_code] =>
[story] =>
[establishment_date] =>
[pricing] =>
[education] =>
[services_offered] =>
[assignments_undertook] =>
[advanced_fees] =>
[equipments] =>
[about_service] =>
[coins] => 500
[created_at] => 2015-06-15 20:21:45
[updated_at] => 2015-06-15 20:21:45
)
[original:protected] => Array
(
[id] => 16
[first_name] => some name
[last_name] =>
[company_name] => some name
[contact_number] => 1234567890
[username] => username
[profile_photo] =>
[photo_mime_type] =>
[cover_photo] =>
[cover_photo_mime_type] =>
[address] =>
[city_id] => 1
[zip_code] =>
[story] =>
[establishment_date] =>
[pricing] =>
[education] =>
[services_offered] =>
[assignments_undertook] =>
[advanced_fees] =>
[equipments] =>
[about_service] =>
[coins] => 500
[created_at] => 2015-06-15 20:21:45
[updated_at] => 2015-06-15 20:21:45
)
[relations:protected] => Array
(
)
[hidden:protected] => Array
(
)
[visible:protected] => Array
(
)
[appends:protected] => Array
(
)
[fillable:protected] => Array
(
)
[dates:protected] => Array
(
)
[casts:protected] => Array
(
)
[touches:protected] => Array
(
)
[observables:protected] => Array
(
)
[with:protected] => Array
(
)
[exists] => 1
)
The models VendorProfile and Client are connected as:
in VendorProfile model:
protected $morphClass = 'MorphVendorProfile';
// Defining 'Polymorphic' Relationship with Client Model
public function client() {
return $this->morphMany('App\models\Client', 'profile');
}
and I have an alias in my config/app.php:
'MorphVendorProfile'=> 'App\models\VendorProfile'
in Client model :
public function profile() {
return $this->morphTo();
}
Update:
This error has occurred while migrating the code from laravel 4.2 to laravel 5. So right now, when I run the previous code that was based on 4.2 version with the SAME database, and it didn't throw me any error, so I think problem is with the code, not database. I am certain that there is a problem with 'morph' relationships, I had to modify a bit in the process of migrating to make it work on other pages.
here is the morphTo function in my Eloquent/Model.php:
/**
* Define a polymorphic, inverse one-to-one or many relationship.
*
* #param string $name
* #param string $type
* #param string $id
* #return \Illuminate\Database\Eloquent\Relations\MorphTo
*/
public function morphTo($name = null, $type = null, $id = null)
{
// If no name is provided, we will use the backtrace to get the function name
// since that is most likely the name of the polymorphic interface. We can
// use that to get both the class and foreign key that will be utilized.
if (is_null($name))
{
list(, $caller) = debug_backtrace(false, 2);
$name = snake_case($caller['function']);
}
list($type, $id) = $this->getMorphs($name, $type, $id);
// If the type value is null it is probably safe to assume we're eager loading
// the relationship. When that is the case we will pass in a dummy query as
// there are multiple types in the morph and we can't use single queries.
if (is_null($class = $this->$type))
{
Log::info('eagerly loading');
return new MorphTo(
$this->newQuery(), $this, $id, null, $type, $name
);
}
// If we are not eager loading the relationship we will essentially treat this
// as a belongs-to style relationship since morph-to extends that class and
// we will pass in the appropriate values so that it behaves as expected.
else
{
Log::info('not eagerly loading');
Log::info($class);
$instance = \App::make('\App\models\\'.$class);
$output = print_r($instance,1);
Log::info('*'.$output.'*');
return new MorphTo(
$instance->newQuery(), $this, $id, $instance->getKeyName(), $type, $name
);
}
}
Check your namespaces. Make sure that the polymorphic relationship that you have setup references the full namespace in both the Vendor and Client models

Do a custom query search in wordpress by post meta

I'm doing a custom search in wordpress, and I would like to find my custom type articles by searching them from custom meta keys.
This code filters posts by custom types, but filters all of them by custom metas, showing no results!
function filtri_di_ricerca( $query ) {
if ( is_admin() || ! $query->is_main_query() )
return;
if (is_home())
{
$query->set('post_type', 'post_annunci');
$query->set('posts_per_page', 1000);
$meta_query = array();
// Only check these form fields
$fields = array( 'annuncio_contratto', 'annuncio_categorie', 'annuncio_tipologia', 'annuncio_citta', 'annuncio_prezzo');
foreach( $fields as $field ) {
if( $_GET[$field] != '' ) {
// We have something to match, otherwise ignore the field...
$meta_query[] = array(
'key' => $field,
'value' => $_GET[$field],
'compare' => '='
);
}
}
$meta_query['relation'] = 'AND';
$query->set('meta_query',$meta_query);
}
return;
}
add_action( 'pre_get_posts', 'filtri_di_ricerca');
The search address is:
http://example.com/?annuncio_contratto=Vendita&annuncio_categorie=Residenziale&annuncio_tipologia=Appartamento&annuncio_citta=NewYork
DEBUG with:
echo "<pre>"; print_r($wp_query->query_vars); echo "</pre>";
result:
Array
(
[error] =>
[m] => 0
[p] => 0
[post_parent] =>
[subpost] =>
[subpost_id] =>
[attachment] =>
[attachment_id] => 0
[name] =>
[static] =>
[pagename] =>
[page_id] => 0
[second] =>
[minute] =>
[hour] =>
[day] => 0
[monthnum] => 0
[year] => 0
[w] => 0
[category_name] =>
[tag] =>
[cat] =>
[tag_id] =>
[author_name] =>
[feed] =>
[tb] =>
[paged] => 0
[comments_popup] =>
[meta_key] =>
[meta_value] =>
[preview] =>
[s] =>
[sentence] =>
[fields] =>
[menu_order] =>
[category__in] => Array
(
)
[category__not_in] => Array
(
)
[category__and] => Array
(
)
[post__in] => Array
(
)
[post__not_in] => Array
(
)
[tag__in] => Array
(
)
[tag__not_in] => Array
(
)
[tag__and] => Array
(
)
[tag_slug__in] => Array
(
)
[tag_slug__and] => Array
(
)
[post_type] => post_annunci
[posts_per_page] => 1000
[meta_query] => Array
(
[0] => Array
(
[key] => annuncio_contratto
[value] => Vendita
[compare] => =
)
[1] => Array
(
[key] => annuncio_categorie
[value] => Residenziale
[compare] => =
)
[2] => Array
(
[key] => annuncio_tipologia
[value] => Appartamento
[compare] => =
)
[3] => Array
(
[key] => annuncio_citta
[value] => Pesaro
[compare] => =
)
[relation] => AND
)
[ignore_sticky_posts] =>
[suppress_filters] =>
[cache_results] => 1
[update_post_term_cache] => 1
[update_post_meta_cache] => 1
[nopaging] =>
[comments_per_page] => 50
[no_found_rows] =>
[order] => DESC
)
function filtri_di_ricerca( $query ) {
if ( is_admin() || ! $query->is_main_query() )
return;
if (is_home())
{
$query->set('post_type', 'post_annunci'); // added
$query->set('posts_per_page', 1000); //added
$meta_query = array(); //edited
// Only check these form fields
$fields = array( 'annuncio_contratto', 'annuncio_categorie', 'annuncio_tipologia', 'annuncio_citta', 'annuncio_prezzo');
foreach( $fields as $field ) {
if( isset($_GET[$field]) and $_GET[$field] != '' ) { // ADDED isset()
// We have something to match, otherwise ignore the field...
$meta_query[] = array(
'key' => $field,
'value' => $_GET[$field],
'compare' => '=', // CHANGED
);
}
}
$meta_query['relation'] = 'AND'; // ADDED
$query->set('meta_query',$meta_query);
}
return;
}
add_action( 'pre_get_posts', 'filtri_di_ricerca');
EDIT
You went wrong when you put main query data and meta query data in the same basket.

How To Create New Content Type Programmatically in Drupal 7?

I'm building a module (my_module) in Drupal 7.
It has some functionality and also will create new content type.
In my_module.install I implemented the hook_install (my_module_install).
Can I use more one implementation of hook_install to create new content type (my_cck_install) in this module?
If (yes), how should I do this?
Else: have I do this in another module? :-)
You can't use more than one implementation of hook_install in the same module; in PHP you can't have 2 function with the same name which rules this out.
You would just need to add your new content type in the same hook_install anyway (have a look at how the standard installation profile does it at /profiles/standard/standard.install). This is how I always add new content types from the install file (using the example of a testimonials module):
function testimonial_install() {
// Make sure a testimonial content type doesn't already exist
if (!in_array('testimonial', node_type_get_names())) {
$type = array(
'type' => 'testimonial',
'name' => st('Testimonial'),
'base' => 'node_content',
'custom' => 1,
'modified' => 1,
'locked' => 0,
'title_label' => 'Customer / Client Name'
);
$type = node_type_set_defaults($type);
node_type_save($type);
node_add_body_field($type);
}
}
The following code will create a content type called "Event" with a machine name of 'event' and a title field -
//CREATE NEW CONTENT TYPE
function orderform_node_info() {
return array(
'event' => array(
'name' => t('Event'),
'base' => 'event',
'description' => t('A event content type'),
'has_title' => TRUE
),
);
}
function event_form($node,$form_state) {
$form['title'] = array(
'#type' => 'textfield',
'#title' => t('event Title'),
'#default_value' => !empty($node->title) ? $node->title : '',
'#required' => TRUE,
'#weight' => -5
);
return $form;
}
//END CONTENT TYPE
you should place it in your .module file... if you want do add additional fields to it, let me know and I'll patch you up with the code... good luck!
/**
* Implements hook_node_info()
*/
function mymodule_node_info() {
return array(
'news' => array(
'name' => t('News'),
'base' => 'news',
'description' => t('You can add News here'),
'has_title' => TRUE,
'title_label' => t('News title')
)
);
}
/**
* Implement hook_form()
*/
function mymodule_form($node, $form_state) {
return node_content_form($node, $form_state);
}
Add the implementation to mymodule.install is as follows:
/**
* Implements hook_install().
*/
function mymodule_install() {
node_types_rebuild();
$types = node_type_get_types();|
node_add_body_field($types['news']);
}
You can get a detailed description with code from here
/*
* Implementation in hook node info in .Module file
*/
function test_node_info() {
return array(
'product' => array(
'name' => t('Product'),
'base' => 'product',
'description' => t('Product Title'),
)
);
}
/**
* Implement hook_form()
*/
function product_form($node, $form_state) {
return node_content_form($node, $form_state);
}
/**
* Implements hook_install() in .install file.
*/
function test_install() {
node_types_rebuild();
$types = node_type_get_types();
node_add_body_field($types['product']);
//New way to implement to add fields in your content type
foreach (_test_installed_fields() as $field) {
field_create_field($field);
}
foreach (_test_installed_instances() as $fieldinstance) {
$fieldinstance['entity_type'] = 'node';
$fieldinstance['bundle'] = 'product';
field_create_instance($fieldinstance);
}
}
/*
* Define your fields
*/
function _test_installed_fields() {
$t = get_t();
return array(
'product_title123' => array(
'field_name' => 'product_title123',
'label' => $t('Product Title'),
'type' => 'text'
),
'description123' => array(
'field_name' => 'description123',
'label' => $t('Description'),
'type' => 'text'
),
);
}
/*
* Define your instance of fields
*/
function _test_installed_instances() {
$t = get_t();
return array(
'product_title123' => array(
'field_name' => 'product_title123',
'type' => 'text',
'label' => $t('Product Title'),
'widget' => array(
'type' => 'text_textfield'
),
'display' => array(
'example_node_list' => array(
'label' => $t('Product Title'),
'type' => 'text'
)
)
),
'description123' => array(
'field_name' => 'description123',
'type' => 'text',
'label' => $t('Description'),
'widget' => array(
'type' => 'text_textarea_with_summary'
),
'display' => array(
'example_node_list' => array(
'label' => $t('Description'),
'type' => 'text'
)
)
),
);
}
/**
* Implements hook_uninstall().
*/
function test_uninstall() {
$ournewtype = 'product';
$sql = 'SELECT nid FROM {node} n WHERE n.type = :type';
$result = db_query($sql, array(':type' => $ournewtype));
$nodeids = array();
foreach ($result as $row) {
$nodeids[] = $row->nid;
}
node_delete_multiple($nodeids);
node_type_delete($ournewtype);
}
That's it.

trouble with update_option() in WordPress import settings function

I am having a bit of trouble with an import/export option I'm building into my theme. Hopefully I explain this well, let me start with the function.
function ifeature_import_options() {
$options = get_option('ifeature');
$import = $options['if_import_code'];
if ($import != '');
$options_array = (unserialize($import));
foreach( $options_array as $option_name => $option_value ) {
update_option( $option_name, $option_value );
}
}
if_import_code returns the serialized output of $options (which the user enters into a textarea in my options panel), and I've tested that $options_array is properly unserializing the import code as it prints the following:
Array ( [if_menu_color] => Pink [if_font] => Arial [if_css_options] =>
[if_favicon] => [if_ga_code] => [if_import_code] => [if_export_code]
=> b:0; [if_logo] => [if_menuicon] => [if_header_contact] =>
[if_facebook] => [if_twitter] => [if_linkedin] => [if_youtube] =>
[if_googlemaps] => [if_email] => [if_rsslink] => [if_home_description]
=> [if_home_keywords] => [if_home_title] => [if_callout_title] =>
[if_callout_text] => [if_callout_img] => [if_callout_image_link] =>
[if_slider_type] => posts [if_slider_placement] => feature
[if_slider_category] => [if_slider_posts_number] => [if_slider_height]
=> [if_slider_delay] => [if_slider_animation] => random
[if_footer_text] => [if_hide_callout] => 0 [if_show_fb_like] => 0
[if_hide_slider] => 0 [if_hide_boxes] => 0 [if_hide_link] => 0
[if_slider_navigation] => 0 )
So I have the new array that needs to be written to my options, but the update_options code I've used isn't working. What am I missing?
$options_array = unserialize($importOptions);
foreach ($options_array as $option_name=>$value) {
if ($value != '') update_option( $key, $value );
}

Resources