Laravel Blade directive adding extra quotes - laravel

Im using Laravel 7 so I didnt think this was going to be an issue.
Just a note on my installation, it was an upgrade from 5.3.
My issue is with a custom Blade directive I created.
It is aded additional quotes around the input
I added a dd() to see why my Helper didnt work.
My ServiceProvider
Blade::directive('setting', function ($expression) {
dd($expression);
return SettingHelper::value($expression);
});
View file
#setting('theme_public')
Output of dd()
"'theme_public'"
Expected output
'theme_public'
I dont know why the extra quotes are being added.

You are right, I get the same behavior with a fresh laravel 7 installation.
I created some little tests, to see what's going on. I think the behavior is best explained like this. Imagine you are calling a view with one variable:
return view('welcome', ['var' => "Hallo"]);
You have a custom blade directive like this:
Blade::directive('dirtest', function ($expression) {
dd($expression);
});
If you use that in your template the output is this:
#dirtest($var)
// output of dd in the browser:
"$var"
So it seems like the blade directives are just meant to replace some shorthand directive with more verbose php code. The actual code get's executed later in the blade templating engine. That makes sense, since blade templates are also cached for faster execution. In the cached version that custom directive is already embedded and your custom function doesn't get fired anymore. I hope that explanation makes sense to you.
What that means for you
It really depends on your use case. If you have a custom directive, that only gets passed constant strings, you could probably get away with just writing:
#setting(theme_public)
But if there's just a slight chance, that you might pass in a variable from time to time, like
#setting($theme)
You really have to return code, that utilizes that variable and can be evaluated later.

Related

Laravel 5.7+, How to use helper functions within Blade templates smartly

Since Laravel 5.7, the majority of global Helper functions (specifically the ones related with "Arrays & Objects" and "Strings") are now based on Facades (using Illuminate\Support\Str and Illuminate\Support\Arr classes) instead of being defined as "normal" helper functions, as they were before 5.7 (see difference with previous Laravel 5.6 docs).
Does it really mean that we are not allowed to use them anymore directly within our Blade views? If we do, they have to be obviously prefixed with its full path in any case, resulting in a dirtier Blade views...
Is not this change counter-productive?
EDIT:
Made some googling and found this article that confirms the situation.
Also, I have seen that in 5.8.17 it is planned to include Arr and Str aliases by default within config/app.php (link).
In the meanwhile, I proceed to register Arr and Str aliases in my config/app.php config file to avoid the full path issue.
Thanks

prevent duplicate value using ajax in sugar crm

i have create module using module builder , now i am having a field called as book Name
now if i give same book name 2 time t is accepting .
i don't want to use and plug in for checking duplicate value because i want to learn the customization through code .
so i can call ajax and check in data base weather the same book name is exist in db or not but i don't know how controller works in sugar crm . and how to call ajax in sugar crm .
can any one guide me , your help is much appreciated .
If you really want to accomplish this using ajax then I'd recommend an entryPoint as the way to go. This customization will require a couple of simple things. First you'll write a little bit of javascript to perform the actual ajax call. That ajax call will post to the entryPoint you write. The entryPoint will run the query for you and return a response to you in the edit view. So lets get started by writing the entryPoint first.
First, open the file custom/include/MVC/Controller/entry_point_registry.php. If the folder structure and file do not exist yet, go ahead and create them.
Add the following code to the entry_point_registry.php file:
$entry_point_registry['test'] = array('file' => 'custom/test.php', 'auth' => true);
Some quick explanation about that line:
The index value of test can be changed to whatever you like. Perhaps 'unique_book_value' makes more sense in your case. You'll see how this value is used in a minute.
The file value in the array points to where you're gonna put your actual code. You should also give this a more meaningful name. It does NOT need to match the array key mentioned above.
The 'auth' => true part determines whether or not the browser needs to have an active logged in session with SugarCRM or not. In this case (and almost all) I'd suggest keeping this to true.
Now lets look at the code that will go in custom/test.php (or in your case unique_book_name.php):
/* disclaimer: we are not gonna get all crazy with using PDO and parameterized queries at this point,
but be aware that there is potential for sql injection here. The auth => true will help
mitigate that somewhat, but you're never supposed to trust any input, blah blah blah. */
global $db; // load the global sugarcrm database object for your query
$book_name = urldecode($_REQUEST['book_name']); // we are gonna start with $_REQUEST to make this easier to test, but consider changing to $_POST when confirmed working as expected
$book_id = urldecode($_REQUEST['book_id']); // need to make sure this still works as expected when editing an existing record
// the $db->quote is an alias for mysql_real_escape_string() It still does not protect you completely from sql injection, but is better than not using it...
$sql = "SELECT id FROM book_module_table_name WHERE deleted = 0 AND name = '".$db->quote($book_name)."' AND id <> '".$db->quote($book_id)."'";
$res = $db->query($sql);
if ($db->getRowCount($res) > 0) {
echo 'exists';
}
else {
echo 'unique';
}
A note about using direct database queries: There are api methods you can use to accomplish this. (hint: $bean->retrieve_by_string_fields() - check out this article if you wanna go that route: http://developer.sugarcrm.com/2012/03/23/howto-using-the-bean-instead-of-sql-all-the-time/) However, I find the api to be rather slow and ajax should be as fast as possible. If a client asked me to provide this functionality there's a 99% chance I'd use a direct db query. Might use PDO and parameterized query if I'm feeling fancy that day, but it's your call.
Using the above code you should be able to navigate to https://crm.yourdomain.com/index.php?entryPoint=test and run the code we just wrote.
However at this point all you're gonna get is a white screen. If you modify the url to include the entryPoint part and it loads your home page or does NOT go to a white screen there are 3 potential causes:
You put something different for $entry_point_registry['test']. If so change the url to read index.php?entryPoint=whatever_you_put_as_the_array_key
You have sugar in a folder or something on your domain so instead of crm.yourdomain.com it is located somewhere ugly and stupid like yourdomain.com/sugarcrm/ if this is the case just make sure that your are modifying the url such that the actual domain portion is preserved. Okay I'll spell it out for you... https://yourdomain.com/sugarcrm/index.php?entryPoint=test
This is more rare, but for some reason that I cannot figure out apache sometimes needs to be reloaded when adding a new entrypoint. If you have shell access a quick /etc/init.d/apache2 reload should do the trick. If you don't have shell access you may need to open a ticket with your hosting provider (or get a fricking vps where you have some control!!!, c'mon man!)
Still not working? Did you notice the "s" in https? Try http instead and buy a fricking $9 ssl cert, geez man!
Okay moving on. Let's test out the entryPoint a bit. Add a record to the book module. Let's add the book "War of Art" (no, not Art of War, although you should give that a read too).
Now in the url add this: index.php?entryPoint=test&book_name=Art%20of%20War
Oh gawd that url encoding is hideous right! Don't worry about it.
You should hopefully get an ugly white screen with the text "exists". If you do let's make sure it also works the other way. Add a 2 to the book name in the url and hopefully it will now say "unique".
Quick note: if you're using Sugar you're probably also using mysql which is case insensitive when searching on strings. If you really need case sensitivity check out this SO article:
How can I make SQL case sensitive string comparison on MySQL?
Okay so now we have our entryPoint working and we can move on to the fun part of making everything all ajaxical. There are a couple ways to go about this, but rather than going the most basic route I'm gonna show you what I've found to be the most reliable route.
You probably will need to create the following file: custom/modules/CUSTOM_BOOK_MODULE/views/view.edit.php (I hope by now I don't need to point out changing that path to use your module name...
Assuming this file did not exist and we are starting from scratch here is what it will need to look like:
if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
class CUSTOM_BOOK_MODULEViewEdit extends ViewEdit
{
public function display()
{
// make sure it works in the subpanel too
$this->useForSubpanel = true;
// make the name value available in the tpl file
$this->ss->assign('name_value', $this->bean->name);
// load the parsed contents of the tpl into this var
$name_input_code = $this->ss->fetch('custom/modules/CUSTOM_BOOK_MODULE/tpls/unique_book_checker.tpl.js');
// pass the parsed contents down into the editviewdefs
$this->ss->assign('custom_name_code', $name_input_code);
// definitely need to call the parent method
parent::display();
}
}
Things are looking good. Now we gotta write the code in this file: custom/modules/CUSTOM_BOOK_MODULE/tpls/unique_book_checker.tpl.js
First a couple of assumptions:
We're going to expect that this is Sugar 6.5+ and jquery is already available. If you're on an earlier version you'll need to manually include jquery.
We're going to put the event listener on the name field. If the book name value that you want to check is actually a different field name then simply adjust that in the javascript below.
Here is the code for custom/modules/CUSTOM_BOOK_MODULE/unique_book_checker.tpl.js:
<input type="text" name="name" id="name" maxlength="255" value="{$name_value}" />
<span id="book_unique_result"></span>
{literal}
<script type="text/javascript">
$(document).ready(function() {
$('#name').blur(function(){
$('#book_unique_result').html('<strong> checking name...</strong>');
$.post('index.php?entryPoint=test', {book_name: $('#name').val(), book_id: $('[name="record"]').val()}, function(data){
if (data == 'exists') {
removeFromValidate('EditView', 'name');
addToValidate('EditView', 'name', 'float', true, 'Book Name Must be Unique.');
$('#book_unique_result').html('<strong style="color:red;"> ✗</strong>');
}
else if (data == 'unique') {
removeFromValidate('EditView', 'name');
addToValidate('EditView', 'name', '', true, 'Name Required');
$('#book_unique_result').html('<strong style="color:green;"> ✓</strong>');
}
else {
// uh oh! maybe you have php display errors on?
}
});
});
});
</script>
{/literal}
Another Note: When the code detects that the name already exists we get a little hacky and use Sugar's built in validation stuff to prevent the record from saving. Basically, we are saying that if the name already exists then the name value MUST be a float. I figured this is pretty unlikely and will do the trick. However if you have a book named 3.14 or something like that and you try to create a duplicate this code will NOT prevent the save. It will tell you that a duplicate was found, but it will not prevent the save.
Phew! Okay last two steps and they are easy.
First, open the file: custom/modules/CUSTOM_BOOK_MODULE/metadata/editviewdefs.php.
Next, find the section that provides the metadata for the name field and add this customCode attribute so that it looks like this:
array (
'name' => 'name',
'customCode' => '{$custom_name_code}',
),
Finally, you'll need to do a quick repair and rebuild for the metadata changes to take effect. Go to Admin > Repair > Quick Repair & Rebuild.
Boom! You should be good to go!

CodeIgniter model not right with where condition

I have this code in my model:
$this->db->where('transaction_income_barcode.barcode_number', $barcode_number);
The value of $barcode_number is 000500007301000005304778
So I compare using PHP it is the correct value:
if($_POST['barcode_number'] == '000500007301000005304778')
but somehow via the model it is not working correctly.
Any ideas?
Forget all the chatter of var_dump() or complicated last_query(), simply follow the CI process of profiling your code and add:
$this->output->enable_profiler(TRUE);
In your controller after the active call. This will show you your FULL SQL QUERY (actually, all queries on that page).
Cleanest way to go about it, my suspicion is that your value is being converted to a int and it STRIPS out the leading 000's
Take care of that by doing a cast:
$this->db->where('transaction_income_barcode.barcode_number', (string)$barcode_number );
As a side note:
Don't use $_POST['barcode_number'] instead be safe with CI's $this->input->post('barcode_number');

Codeigniter extend helper but want to call old helper

I am developing an Application in Bonfire.
They have extended the form helper.
Is there a way to call the original form helper from Codigniter without removing the extended one from Bonfire?
"Helpers" are just files with PHP functions in them. They aren't actually "extended", Codeigniter loads it's default helpers after loading yours, and checks if you "overwrote" a function like so:
if ( ! function_exists('form_open'))
{
function form_open() {/* default code */}
}
So unfortunately, no - there's no way to call the original function if you already declared your own.
HOWEVER: It appears that Bonfire does the exact same thing, checking with function_exists, so if you want to - you should be able to load your own form helper before it, but you still cannot simply load the original one without hacking Bonfire and removing the functions (which could have terrible side effects).
Faced the same prob, user742736's comment is the only answer that solved the prob.
Explained in detail, may be this can help some one
You can create your own helper function with out the divs surrounding the drop down here
bonfire/application/helpers/MY_form_helper.php
make a copy of the function form_dropdown, name it like form_dropdown_plain
modify the last few lines of the function to output with out divs
call form_dropdown_plain instead of form_dropdown

Serialization not allowed in Magento?

When I turn on cache in Magento, I get the following exception:
Serialization of 'Mage_Core_Model_Layout_Element' is not allowed
Exception occurs in app/code/core/Mage/Page/Block/Template/Links.php, on line:
return parent::getCacheKeyInfo() + array(
'links' => base64_encode(serialize($links)),
'name' => $this->getNameInLayout()
)
I am using Magento Enterprise 1.10 and PHP 5.3.
Can anyone tell me what the problem is?
You shouldn't have an empty after_text or before_text tags in your layout file. If you don't need it, just delete the tag at all.
If it will not help, dump the $links variable before the 150th line in the app/code/core/Mage/Page/Block/Template/Links.php file, and you will see an array with arrays inside it. All of keys and values should be strings or integers, not objects. The key of array value which is an object will tell you which tag to delete from the layout file.
Awesome #vsushkov.
I used:
try{
serialize($links);
} catch(Exception $e){
Mage::log($links);
die;
}
to find out exact layout where we had those empty tags and after removing those empty tags, it fixed the issue and then removed above code :-)
Saw this issue on a clients site. None of the solutions above worked for me. After much googling of the error, it seems to be related to JM or JoomlArt themes/extensions.
The code is extremely poorly written. For example some of the things you will find in these themes include:
Declaring php classes inside templates,
Setting global variables inside templates,
Setting data into superglobals from templates,
Providing a translation file, yet not wrapping most text strings in template in the translate function
I found 1 response from their support staff essentially suggesting to turn off error reporting to fix the issue.
I found my problem in app/design/frontend/default/jm_adamite/template/catalog/navigation/tops.phtml
There was a line setting $this into $_SESSION. I commented it out and the error went away. Nothing else appeared broken. A grep for that variable being used anywhere else had 0 results. If you have one of these JM extensions installed or use one of their themes, I would suspect that first
Good luck
This problem happened to me when trying to serialize categories after calling the getCategoryUrl function after digging i found out that that set _urlModel object which cannot be serialized as it contains Mage_Core_Model_Layout_Element so before serializing the object check if it has that _urlModel property

Resources