Making improvements to code - magento

I have been working on a magento module for sometime now (not because it is a large module, its my first and I really struggled with it). I now have it working, which I was really pleased with at first, but now I would like to improve it by increasing the reusability of the code.
While making my module I have looked at other modules to learn from them and have seen that many actions are only a line or two and I have tried keeping in with the skinny controller approach but failed, my example is as follows:
I have a function that get a users details that they have inputted into a custom form
protected function _setPostData()
{
$this->_salutation = $this->getRequest()->getPost('salutation');
$this->_f_name = $this->getRequest()->getPost('f_name');
$this->_l_name = $this->getRequest()->getPost('l_name');
$this->_email = $this->getRequest()->getPost('email');
$this->_emailAddressCheck = $this->getRequest()->getPost('emailAddressCheck');
$this->_gender = $this->getRequest()->getPost('gender');
$this->_country = $this->getRequest()->getPost('country');
$this->_pref_lang = $this->getRequest()->getPost('pref_lang');
}
I feel that this is not the correct way to do it and that there is a better way of achieving the same goal, as you can see this function gets the posts data and assigns it to attributes that i've set at the start of the class. I have several other examples that are very similar to the above and if someone could please offer some guidance on this one I am sure I will be able to work out the others
This example is held within the index action, should I put it in a helper as once it created correctly i am sure there will be a few occasions that I will be able to use it again?

you should put all the post data on an array and use it from there
$dataArray=$this->getRequest()->getPost();
$this->_salutation = $dataArray['salutation'];
$this->_f_name = $dataArray['f_name'];
$this->_l_name = $dataArray['l_name'];

Related

I have a problem with the method removeChild

I'm new here on Stackoverflow, started programming in January, small own applications could already be implemented and now I have a problem with JavaScript with the removeChild method, because I don't know how to use this method with other methods and functions can combine or must write.
Thanks in advance
Not really sure what you are trying to do, your question should be a bit more specific.
Just as an example, say you want to remove all items from a list, here's an example of what could be used.
var element = document.getElementById("top");
while (element.firstChild) {
element.removeChild(element.firstChild);
}
If you simply want to delete one child you would need to know the parent's Id, it is done like so...
var d = document.getElementById("top");
var d_nested = document.getElementById("nested");
var throwawayNode = d.removeChild(d_nested);
you can try the W3School documentation if you'd like
https://www.w3schools.com/jsref/met_node_removechild.asp

Changing values of numbers in CodeMirror by using scrubbing addon

I'm having trouble with CodeMirror. I'm trying to add in live number scrubbing, similar to Brett Victor's example, and Khan Academy's capability, but I am not having too much luck.
I can't post links, but I found this library which kind of gets the job done (made by user FWeinb on GitHub) which can kind of accomplish what I'm looking for, but I've noticed that although the numbers appear to have been changed, as soon as I do something like press enter in CodeMirror, then the value of the variable resets to what it was originally.
I'm using ReactJS, and I am not too sure how to fix this. I'm trying something a little ludicrous by calling this.replaceRange every time the contents are being changed, but there must be a better way. Here is a snippet of my code. It's not what I want it to be ideally, but just for testing purposes:
this.cm.on('dblclick', this.handleDblClick.bind(this))
...
handleDblClick() {
let matches = document.querySelectorAll(".cm-number");
let scrub = new Scrubbing (matches[0],
{ driver :
[
Scrubbing.driver.Mouse,
Scrubbing.driver.MouseWheel,
Scrubbing.driver.Touch
]
})
matches[0].addEventListener('DOMSubtreeModified', () => {
//console.log('change detected')
})
}
So I know that currently the scrubber edits this: <span class="cm-number"></span>, but that the actual underlying data isn't updating with the scrubber, nor is it persisting. Can anyone shed some light on what I should be doing here so that the value of the variable within the editor is updating live with the scrubber, and so that the value persists upon a new action?

Should I use singletableview?

I'm learning about Django tables. I first wrote a basic example, here my view:
def people1(request):
table = PersonTable(Person.objects.filter(id=2))
RequestConfig(request).configure(table)
return render(request, 'people.html', {'table': table})
This way I've been able to easily display a table with a filter condition "filter(id=2))".
After that I found SingleTableView which is supposed to be an easier way to display database tables, as an example I wrote this view, which worked fine:
from django_tables2 import SingleTableView
class PersonList(SingleTableView):
template_name = 'ta07/comun.html'
model = Person
table_class = PersonTable
Questions are: how should I do to apply filters like in the first example? And is SingleTableView better than the basic way?
I'd say for now, you should only use it for the very basic use case. As soon as you need customizations from that, use your own.
Since filtering is a very common use case, I might consider adding that to the features of SingleTableView at some point. If you need it before that, feel free to open a pull request.

Adding an extension to the blog post pages on liferay

My problem is i want to add '.html' extension to my every blog post. For example right now i have a blog post url 'http://www.indu.com/blog/-/blogs/creating-a-custom-portlet-in-liferay' and what i want is 'http://www.indu.com/blog/-/blogs/creating-a-custom-portlet-in-liferay.html'
I would be grateful if any one can help?
I wonder why would you want this.
Anyways, I think you can change the urlTitle field of BlogsEntry and append .html to the string. May be use a service-wrapper-hook to append .html to the urlTitle whenever a blog is created or updated.
Or you can even use a ModelListener if it exists for BlogsEntry hook to update the blog's urlTitle by using onCreate & onUpdate methods.
Edit
The urlTitle field is present in the BlogsEntry table.
You can access this in java with the following methods:
BlogsEntry blog = BlogsEntryLocalServiceUtil.getBlogsEntry(90989); // retrieves the blog-entry
String blogUrlTitle = blog.getUrlTitle();
blog.setUrlTitle(blogUrlTitle + ".html"); // this would set the string
You can have a check for the blogUrlTitle so that you don't have repeated .html appended to the string:
if (!blogUrlTitle.contains(".html")) { // append only if the title does not contain `.html`
blog.setUrlTitle(blogUrlTitle + ".html");
}
You can refine your code as you like, the above is just a guide-line.
As a side-note, I would always try to reason with the client why they want something, this helps not only to fend-off bad-change-requests but also helps in giving an alternative to the client (which is less taxing on the developers like us ;-) ). In most cases this helps to understand the client's business better & provide better returns.

loadByRequestPath() is Overriding Parameter With Current URL Path

I am trying to load a rewrite rule based on a product's URL path.
I am using the loadByRequestPath() method in Mage_Core_Model_Url_Rewrite to accomplish this. However, no matter what I supply this method I get the following result (Check comment in code):
public function loadByRequestPath($path)
{
Zend_Debug::dump($path); // returns the path to my module
$this->setId(null);
$this->_getResource()->loadByRequestPath($this, $path);
$this->_afterLoad();
$this->setOrigData();
$this->_hasDataChanges = false;
return $this;
}
Here is my module code:
$productRewrite = Mage::getModel('core/url_rewrite') ->loadByRequestPath($product->getUrlPath());
Oddly, I get this back:
Array ( [0] => rewrites/getProductRewrites
[1] => rewrites/getProductRewrites/ )
Array ( [0] => 01003-product-name )
So loadByRequestPath() is getting called twice for whatever reason. $productRewrite still returns an empty object.
I have verified that $product->getUrlPath() returns the correct path. (As seen in the second array)
I am on Magento 1.6.1.
Your question is still a little unclear, so this answer might not address the specific problem you're seeing.
Magento's core team hasn't done a great job of communicating these sorts of things over the years, but loadByRequestPath is one of those methods that's best thought of as a "private api". Not in the OOP sense, but in the "this is a method used to implement core system functionality, and probably won't work like you think it should work, so use at your own risk".
The PHP code you're trying to use
$productRewrite = Mage::getModel('core/url_rewrite') ->loadByRequestPath($product->getUrlPath());
won't work with a default installation of Magento because the rewrite object doesn't have a store ID set. Trying something like this should work. (assuming the sample data, with an installed store object that has an ID of "1" and that the product in question exists in that store)
$productRewrite = Mage::getModel('core/url_rewrite');
$productRewrite->setStoreId(1);
$productRewrite->loadByRequestPath($product->getUrlPath());
The loadByRequestPath method assumes that a rewrite already has a store ID set, as it's part of Magento's larger dispatching process. (self-link to article describing the role of rewrites in Magento's routing system)
All that said, the problem you're describing is somewhat confusing. You say that
Zend_Debug::dump($path);
returns
an array that contains the path to my module
While I'm sure you know what the phrase "path to my module" means, it's a meaningless term in the larger magento universe. Being more specific about the literal value will help people understand what you mean.
Additionally, you also say
I have verified that $product->getUrlPath() returns the correct path.
but you're not clear on the value of "the correct path".
My guess would be the path you're seeing in Zend_Debug::dump is the call that's coming through as a part of the standard dispatch and not your later call using $product->getUrlPath(). However, the lack of clarity in your question makes that hard to tell.
If setting the store ID doesn't get you what you want, update your question with a full explanation of how you're running your code, and what you see displayed. With that information more people will be able to help you.

Resources