How can I override the store function in backpack-for-laravel v5? - laravel-backpack

I've updated my app from backpack-for-laravel v4.1 to v5, but now I'm having issues with overriding the store function. I followed the upgrade guide and step 15: https://backpackforlaravel.com/docs/5.x/upgrade-guide#step-15
but unfortunately, my preferred option A is not working for me. this is how my function looked in v4.1:
public function store()
{
// do something before validation, before save, before everything
$this->crud->setOperationSetting('saveAllInputsExcept', ['save_action']); //somehow required...
$end = strtotime('+1 hour', strtotime($this->crud->getRequest()->request->get('start'))); //set end automatically after 1h
$this->crud->getRequest()->request->add(['end'=> $end]);
$response = $this->traitStore();
// do something after save
return $response;
}
I've converted it now to match the v5 requirements as below:
public function store()
{
$this->crud->set('strippedRequest', function($request) {
$end = '2023-02-01 14:00:00';
$request->add([ 'end' => $end ]);
return $request->except(['_save_action', '_token', '_http_referrer']);
});
return $this->traitStore();
}
I even tried it with a static value, but I keep getting the error from SQL that the field 'end' doesn't have a default value...meaning no value for end has been added to the request.
Appreciate any kind of support. Thanks

Thanks for the question. Indeed there is an issue in the upgrade guide example.
I've just submitted a PR to docs to fix it: https://github.com/Laravel-Backpack/docs/pull/404
Should be merged soon!
You can see the actual working solution in the PR diff.
Cheers

Related

Sort Childobjects by specific value

im currently trying to sort all Childobjects i can get by their validity time.
my current code is not sorting anything:
$children = $this->productRepository->findByParent($product->getNumber());
foreach ($children as $child) {
//debug::debug($child->getvalidityPeriod());
$product->addChild($child);
}
As allready to see in the debug, $child->getvalidityPeriod() is there to get the needed info as integer. I thought, there must be a way to sort this object array by their validityPeriod, but how do i manage that? Do i need to create a new method and filter it there or can i use something BEFORE the ForEach Loop?
Pseudo like:
$children = $this->productRepository->findByParent($product->getNumber());
ObjectSortFunction($children, 'getvalidityPeriod');
...and then for each...
Would be awesome if somebody would help me!
Okay after several tries i have a workaround, that can be used as answer, as well ;)
I wrote this method in my repo:
public function findByParentSorted($parentId)
{
$query = $this->createQuery();
$query->matching($query->equals('parent', $parentId));
$query->setOrderings(
[
'validity_period' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_ASCENDING
]
);
return $query->execute();
}

Tinytext issue on Upgrade magento to 2.3.0

In Magento my website 's current version is magento 2.2.5 . Now i have updated it to latest version magento 2.3.0 .
But there i am getting error when i run
php bin/magento setup:upgrade
I got this error
Cannot process definition to array for type tinytext
Please suggest me solution.
Thank You
You are getting this error because "data type" of any third party extension's table's column is tinytext.
So you need to find out column name using debug in following file.
Open this file /vendor/magento/framework/Setup/Declaration/Schema/Db/DefinitionAggregator.php and check this fromDefinition() method and then add debug code to find column name.
public function fromDefinition(array $data)
{
$type = $data['type'];
if (!isset($this->definitionProcessors[$type])) {
/* Add Code for Debug */
echo "<pre>";
print_r($data); exit();
/* Code End */
throw new \InvalidArgumentException(
sprintf("Cannot process definition to array for type %s", $type)
);
}
$definitionProcessor = $this->definitionProcessors[$type];
return $definitionProcessor->fromDefinition($data);
}
After that please run setup:upgrade command and you will get array of column data in console. so from this array you will get name of column from your third party extension table.
Now from that table please change column's data type "tinytext" to "text" and issue will be fixed.
Note : You might also get issues from ENUM and MEDIUMINT data type as well, so do the same steps if get any other data type issue.
Open file
/vendor/magento/framework/Setup/Declaration/Schema/Db/DefinitionAggregator.php
Replace function fromDefinition:
With:
public function fromDefinition(array $data)
{
$type = $data['type'];
if(in_array($type, ["tinytext", "enum"])){
$data['type'] = 'text';
$type = 'text';
}
if(in_array($type, ['time', 'mediumint'])){
$data['type'] = 'datetime';
$type = 'datetime';
}
if(in_array($type, ['mediumint'])){
$data['type'] = 'int';
$type = 'int';
}
if (!isset($this->definitionProcessors[$type])) {
throw new \InvalidArgumentException(
sprintf("Cannot process definition to array for type %s", $type)
);
}
$definitionProcessor = $this->definitionProcessors[$type];
return $definitionProcessor->fromDefinition($data);
}
Yes it is because of some extensions.I just exported the database and search for keywords tinytext , found a table which use this format, I changed it to TEXT and the problem solved.
You might want to check your extensions. I debugged this error for myself and it originated from an extension which was included with a theme purchased, but not updated.

MailChimp Campaign Content Update

MailChimp campaign content docs - https://developer.mailchimp.com/documentation/mailchimp/reference/campaigns/content
I'm trying to replace some placeholders in a campaign content with actual values via the API. At first, I thought there might be some syntax errors or internal logic errors like non-unique mc:edits into a mc:repeatable that would get the HTML refused/declined by MailChimp, hence the update not taking place, however, that was not the case.
Tried replacing html with a simple <p>test</p> and it was still not working.
Here are a couple of local logs, I'll use xyz as my campaign id:
2018-02-26 16:26:13 [::1][9804][-][warning][application] calling GET /campaigns/xyz/content []
2018-02-26 16:26:13 [::1][9804][-][warning][application] got both plain_text and html versions of content
2018-02-26 16:26:13 [::1][9804][-][warning][application] calling PUT /campaigns/xyz/content {"html":"<p>test</p>"}
2018-02-26 16:26:14 [::1][9804][-][warning][application] got response [
'plain_text' => 'test' + other MailChimp stuff such as footer, that were appended automatically by MailChimp,
'html' => '<p>test</p>'
]
// calling GET immediately after PUT in order to see if any update occurred
2018-02-26 16:26:14 [::1][9804][-][warning][application] calling GET /campaigns/xyz/content []
2018-02-26 16:26:14 [::1][9804][-][warning][application] got updated html (my "test" paragraph + auto footer from MailChimp) and proper plain_text
Everything looks fine according to these, that means both versions updated as they were supposed to. However, on the next API/MailChimp dashboard request, it displays the old HTML content, preserving the update I've just made in the plain text version only.
No errors, nothing to look into. It could be any internal MailChimp behaviour.
PS: I know about Setting Mailchimp campaign content html not working or MailChimp API v3 campaign content template sections, but none of the answers provided to those are helpful.
PS2: I know I should contact MailChimp, but according to
Our MailChimp Support Team isn't trained at in-depth API troubleshooting. If you need a developer to help you configure something using the API, check out our great Experts Directory, which lists third-party MailChimp experts who can be hired to help out.
they don't provide support for API troubleshooting.
MailChimp doesn't allow updating the campaign's HTML content because the campaign type is based on a template.
In order to update the HTML content, the campaign has to be set to custom HTML instead of a template. You can check the type by sending a GET API request to /campaigns or /campaigns/{campaign_id} and finding the content_type attribute in the response (documentation).
Alternatively, in the dashboard, the type can be determined by editing the design of the email. Anything under 'Code your own' is HTML and templates are of course templates.
I'm not entirely sure why the first response to a PUT request on a template campaign shows the updated content, but changing the content type should let you update as you want to.
Hope this helps!
If anyone's still looking for an answer to this.
I managed to solve the issue several weeks ago without creating the campaign via API, but actually updating it.
I used placeholders like [product_card id=123], 3 cards per block/row, all repeatable, which are wrapped in a class that I named product-card. In the MailChimp dashboard, you may still see the placeholders, but on preview and any form of preview like thumbnail, it will display correctly.
On the server, I crawl through the campaign's content, "detect" section names based on how they seemed to me in MailChimp and update each section with the content that I want.
PHP snippet below, some Yii2 stuff, but mostly plain PHP. I use $preview to display a preview of how the template would look, I know it's not visible in the function.
/**
* #param $id - Id of the campaign
* #param $s - Whether to save or just preview
*
* #return bool
*/
function changeContent($id, $s = false)
{
$mcCampaign = new McCampaign();
$mcCampaign::$itemId = $id;
$content = $this->api->get("/campaigns/{$id}/content");
if (!isset($content['html'])) return false;
$template = $content['html'];
$forgedDom = new \DOMDocument();
$forgedDom->loadHTML($template);
$mcSections = [];
$finder = new \DOMXPath($forgedDom);
$nodes = $finder->query('//td[contains(#class, "product-card")]');
// disabling this shit in order to avoid strict errors
libxml_use_internal_errors(true);
$mcEditId = 1;
$mcEditIndex = 0;
foreach ($nodes as $key => $node) {
/** #var \DOMElement $node */
$textContent = $node->textContent;
if (!preg_match("#\[product_card id=\d+\]#", $textContent)) continue;
$productId = filter_var($textContent, FILTER_SANITIZE_NUMBER_INT);
$node->textContent = false;
$product = Product::findOne($productId);
$productDetails = $product ? $this->renderPartial('/partials/_mc-product', [
'product' => $product
]) : 'Product not found.';
if ($key != 0) {
if ($key % 3 == 0) {
$mcEditId = 1;
$mcEditIndex++;
} else {
$mcEditId++;
}
}
$mcSections["repeat_1:{$mcEditIndex}:product_card_{$mcEditId}"] = $productDetails;
$fragment = $forgedDom->createDocumentFragment();
$fragment->appendXML($productDetails);
$node->appendChild($fragment);
}
libxml_use_internal_errors(false);
$preview = $forgedDom->saveHTML();
// just in case
/* $preview = str_replace(["\n", "\t", "\r"], "", $preview); */
if ($s) {
if (!empty($mcSections)) {
$res = $this->api->put("/campaigns/{$id}/content", [
'template' => [
'id' => *template_id_here*,
'sections' => $mcSections
],
]);
// forcing Mc to rebuild cache
$this->api->get("/campaigns/{$id}/content");
Yii::$app->session->setFlash('success', 'Done.');
return $this->redirect(['campaign/index']);
} else {
Yii::$app->session->setFlash('error', 'Something went wrong.');
}
}
}

Codeigniter form validation callback rule issue

I am using Codeigniter 3.x form validation callback method in combination trim and required to validate a field.
The problem is, when I pipe them: trim|required|callback_some_method, the callback method seems to take precedence over trim and required and shows its error message.
Any ideas on this?
EDIT:
This is the rule:
$this->form_validation->set_rules('new_password', 'New Password', 'trim|required|min_length[8]|callback_password_check');
And this is the password_check method:
function password_check($pwd) {
$containsLetterUC = preg_match('/[A-Z]/', $pwd);
$containsLetterLC = preg_match('/[a-z]/', $pwd);
$containsDigit = preg_match('/\d/', $pwd);
$containsSpecial = preg_match('/[^a-zA-Z\d]/', $pwd);
if ( !($containsLetterUC && $containsLetterLC && $containsDigit && $containsSpecial) ) {
$this->form_validation->set_message('password_check', '{field} must contain UPPERCASE and lowercase letters, digits, and special characters.');
return FALSE;
}
return TRUE;
}
The method should return FALSE, but as long as required is before my custom rule and the field is empty, it should stop there with Required field message, NOT the custom method message.
Okay guys, I've managed to solve it by extending the Form_validation library, putting my callback method there and piping as the other rules (without callback_ prefix).
Unfortunately, as described in the code from CI, callbacks validation rules are always verified first, prior to ‘required’ for instance.
There is an official issue opened at CI : https://github.com/bcit-ci/CodeIgniter/issues/5077

Symfony2 - Setting up a blog archive

I've been working on trying to setup a blog archive for a blog site where the use clicks on a date and the corresponding posts appear. (see image) I understand I need to retrieve all my blog posts and sort by date, but the steps after that are foggy to me. Taking that data then sorting it by month/year and passing it to a template is the part I am having trouble with.
Can someone shed some light on what I am doing wrong or provide a simple working example?
What I have thus far:
public function archiveAction()
{
$em = $this->getDoctrine()->getManager();
// $query = $em->getRepository('AcmeProjectBundle:Blog')
// ->findAll();
$blogs = $em->getRepository('AcmeProjectBundle:Blog')
->getLatestBlogs();
if (!$blogs) {
throw $this->createNotFoundException('Unable to find blog posts');
}
foreach ($blogs as $post) {
$year = $post->getCreated()->format('Y');
$month = $post->getCreated()->format('F');
$blogPosts[$year][$month][] = $post;
}
// exit(\Doctrine\Common\Util\Debug::dump($month));
return $this->render('AcmeProjectBundle:Default:archive.html.twig', array(
'blogPosts' => $blogPosts,
));
}
You want to tell your archiveAction which month was actually clicked, so you need to one or more parameters to it: http://symfony.com/doc/current/book/controller.html#route-parameters-as-controller-arguments (I would do something like /archive/{year}/{month}/ for my parameters, but it's up to you.) Then when someone goes you myblog.com/archive/2014/04, they would see those posts.
Next, you want to show the posts for that month. For this you'll need to use the Doctrine Query builder. Here's one SO answer on it, but you can search around for some more that pertain to querying for dates. Select entries between dates in doctrine 2