Trying to create rule based static blocks in Magento - dynamic

So one of my clients has a request to do rule based static blocks on their home page. The page will basically swap out several static blocks for others based on the perceived gender of the person viewing the site. It will get this data from the session that the user is currently in, or the data associated with the users account. Basically, if a user searches in a specific set of categories (Men's or Women's categories) it should swap the static blocks out on the home page, so when that user visits the site again they will have a more personalized experience. There will be a default set of blocks if the user is new to the site.
Something like this (and excuse my shabby php):
if($categories = $user->getViewedCategories()){
foreach($categories as $category){
switch($category){
case 14: //insert womens category id here
echo $staticBlockWomen
break;
case 16: //insert mens category id here
echo $staticBlockMen
break;
}
}
} else {
echo $staticBlockDefault
}
I know Magento tracks a users path through a site, and I know that other elements in Magento can have rules based on this data (the dynamic banners and checkout rules), but I am really lost on where to get started.
If someone could point me in the right direction, any help would be appreciated!
Cheers,
Matthew

I'm assuming you know the basics of Magento (at least how to create new blocks and how to manage the layout using xml).
If you need more info about that,
You can accomplish what you need in a few steps:
1 - create the blocks you need (create a new module and block classes inside it within the corresponding .phtml files)
2 - from the admin panel, select the category for which you want to add a block and navigate to "Custom Design" tab, then add in the "Custom Layout Update" textarea something like this:
<reference name="content" >
<block type="mymodule/myblock" name="myblock" />
</reference>
This way, every time the selected category is viewed by the custmer, a block of type "mymodule/myblock" will be added in the content area.

Related

Access Roles in DNN Hotcakes Checkout Views

I'd like to be able to show/hide certain payment methods based on user roles.
In Hotcakes under the "_DisplayPaymentMethods" view set I see a switch statement with "payMethod.MethodId" as the expression. Inside each case I'd like to set an if statement that checks if the current user has "x" role.
How can I access the user roles from this view set?
You'll find the answer in the Hotcakes Commerce documentation area. I've included it here for posterity on SO as well.
Security roles are used for a large number of purposes in both the e-commerce and CMS parts of your website. On occasion, you might want to re-purpose these roles to do something dynamic with your views. One example might be to only show the Add to Cart button to a specific role. We'll use that use case for this example.
First, you'll need a plan. In this plan, we're going to make the Add to Cart button available to all people that are logged in and part of the "VIP-Customer" security role. (This isn't a built-in role. It's made-up for this code sample. You can create and use whatever roles that you want.)
Add the code below to the header area of the view that you want to edit, such as the _ProductDetails.cshtml view.
#functions
{
private bool IsVipCustomer()
{
var customer = DotNetNuke.Entities.Users.UserController.Instance.GetCurrentUserInfo();
if (customer != null && customer.UserID > 0)
{
return customer.IsInRole("VIP-Customer") || customer.IsInRole("Administrators");
}
return false;
}
}
In this code sample below, we're checking to see if the role matches what we expect. If it does, we show the Add to Cart button. You can add and use this code anywhere you want, as long as it has the function we created above in the same view file.
#if (IsVipCustomer())
{
<input type="submit" id="addtocartbutton" value="#Localization.GetString("AddToCart")" class="dnnPrimaryAction largeButton fullCartButton" />
}
We hope this helps you as a baseline example for doing anything dynamic with views that involves the security roles in the CMS.

#auth.requires_permission not working ver 2

Good day to all web2py experts!
I can't find a way on how to use the web2py Decorators
#auth.requires_permission('read','person')
def f(): ....
in the pdf manual it says that:
prevents visitors from accessing the function f unless the visitor is a member
of a group whose members have permissions to "read" records of table
"person". If the visitor is not logged in, the visitor gets directed to a login
page (provided by default by web2py). web2py also supports components,
i.e. actions which can be loaded in a view and interact with the visitor via
Ajax without re-loading the entire page. This is done via a LOAD helper which
allows very modular design of applications; it is discussed in chapter 3 in the
context of the wiki and, in some detail, in the last chapter of this book.
This 5th edition of the book describes web2py 2.4.1 and later versions
In my case:
I have list of groups: Admin_Tier_1, Admin_Tier_2, Admin_Tier_3
Admin_Tier_1 - has the highest authority to access all features like adding a school year, set a school year etc.
Admin_Tier_2 - has the authority to add students etc
Admin_Tier_3 - its the lowest level of authority that can only add fines to the students (Organization Officers)
now I use the Decorator code like this:
#auth.requires_permission('Admin_Tier_1','student_list')
def add(): ....
now I login the account of the Chairman which registered in the auth_membership as Admin_Tier_1. Then I click the link "List of Students" which redirect to add(): function but the system returned a message:
Not Authorized
Insufficient privileges
The auth.requires() method can take a callable rather than a boolean value as the condition, and this is preferable when it is expensive to generate the boolean value (otherwise, the boolean value is generated whenever the controller is accessed, even if the particular decorated function is not the one being called). So, to avoid calling auth.has_membership unnecessarily, you can do:
#auth.requires(lambda: auth.has_membership('Admin_Tier_1') or
auth.has_membership('Admin_Tier_2'))
Now the two auth.has_membership calls will only be made when the actual function being decorated is called.
Also, if you need to check a larger number of roles, you can do something like:
#auth.requires(lambda: any([auth.has_membership(r) for r in ['list', 'of', 'roles']))
Problem solved:
#auth.requires(auth.has_membership('Admin_Tier_1') or auth.has_membership('Admin_Tier_2'))
source here.
Whenever I access the page if the user belong to the group of Admin_Tier_3 the system block the acess and redirect it to "/default/user/not_authorized" page :)

How to implement backoffice controller

after lot of google searches and going through prestashop's official documentation over and over again, I still couldn't find an example of backoffice controller. I even looked into the modules folder of prestashop's installation, but couldn't find any.
I need to implement 3 different back-office pages, each served by its own controller and view.
Can anyone provide me any hint, or even one working example...Just hello world is more than enough.
Thanks in advance...
notice:i write this article for prestashop1.5 and i don't check it for prestashop 1.6.perhaps it is works for 1.6 too.
You should know every thing in this way have special structure.
step 1: Create a folder in your module folder call that 'controllers' (notice:this name is static)
step 2: Create a folder in controller folder call that 'admin' (notice:this name is static too)
step 3: Create a php class file call that AdminMyclassnameController (notice:in this name Admin at first and controller at last is the key word and thay are static but Myclassname is dynamic .attention to first words all of should be uppercase A for Admin, M for Mclass,... )
step 4: Then you should write your class in AdminMyclassnameController and this class should extent with AdminController or AdminControllerCore.
for know how does it class work you can search about helper forums in internet.
step 5: When you create the class you want a tab to show that controller notice:when act to step 1,2,3,4 this controller take an automatic URL for access to this URL you should create a tab.in yourmodule.php in install() you should add this codes
$tab = new Tab();
$tab->class_name = 'AdminTest';
$tab->module = 'test';
$tab->id_parent = 9;
$tab->position = 11;
then you can see the tab in admin office that redirect to your controller.
i am tired to continue ... but if you want more send a message and i continue this article
this is use full site really clear:
http://doc.prestashop.com/display/PS15/Using+helpers+to+overload+a+back-office+template
http://presthemes.com/prestashop-news/modules-classes-and-controller-override-by-julien-breux-4.html
http://doc.prestashop.com/display/PS15/Diving+into+PrestaShop+Core+development
http://www.prestashop.com/forums/topic/270177-solved-making-own-objectmodel-class-doesnt-work/
http://doc.prestashop.com/display/PS15/New+Developers+Features+In+PrestaShop+1.5
http://blog.belvg.com/how-to-implement-a-controller.html
best regards

MVC user's full name in Url, how to handle duplicates

I want to setup the following url in my MVC4 website, using the user's full name in the url:
http://www.myapp.com/profile/steve-jones
I have setup the following route in Global.asax:
routeCollection.MapRoute(
"profile", "profile/{userName}",
new { controller = "myController", action = "profile", userName = string.Empty
});
And I can take the parameter 'steve-jones' and match it to a user with matching name. My only problem though is, what if there is more than one 'Steve Jones', how can I handle this?
Does anyone know of a workaround/solution to this so that I can use a user's full name as part of the url and still be able to retrieve the correct user in the controller method?
Am I forced into including the user's id with the url (something that I do not want to appear)?
The usual way of handling this is by appending a number when creating the profiles. So if "steve-jones" is already a name in the database, then make the user's display name "steve-jones2". You basically have to insist that all profile urls are unique, which includes updating any existing database and account creation code.
Alternatively (and/or additionally), if two same names are found then have the script reroute to a disambiguation page where the user is presented with links and snippet of profile info of the many existing Steve Joneseses so they can go to the full correct profile.
Another way of handling it is by giving all user profiles an additional numeric code on the end. At my university all logins are based on name, so they give everyone pseudo-random 3-digit extensions so that they are safe as long as they don't get 1000 people with the exact same names :)
Some people might be happier being steve-jones-342 if there is no steve-jones or steve-jones1, if you're concerned.

how different URL's are working

I would like to ask a theoritical question about how some web sites work. As an example.Let's say that I'm in the A market on line store. I placed a case of wine in the shopping trolley, The page appeared with this URL:
www.A.co.uk/webstore/basket.asp?calledby=normal&ProductCode=6379044
I continued shopping and then placed a different wine in the trolley and again the page appeared with this URL
www.A.co.uk/webstore/basket.asp?calledby=normal&ProductCode=6323456
I then Clicked the Back Button on the browser three times and the trolley page appeared again. This time contained ONLY the first item and NOT the second.
In another website I showed the following:
I selected a case of wine. As a result the form containing the wine was posted to this ASP page basket.asp?Item=3605681, where Item is the ID of the particular case of wine. However the page appeared in the browser had a different URL:
www.B.com/extra/basket.aspx?acstore=10&ba=0
I then added another case of different wine to basket. The address that appeared was exactly same as previous one.
When I clicked the Back Button The Shopping basket always showed that I have two items in the basket. How do you think that these Online stores have programmed the site so that shopping basket always shows its current state even if the user presses the Back Button several times? Also, what's the difference of these two situations?
The basket is likely stored in the session. More than often the session is in turn backed by cookies. In JSP/Servlet it's the JSESSIONID cookie. To test it yourself, locate the cookie in browser's cookie store and delete it. You'll see that a page refresh will result in an empty basket. For more detailed background information, please read How do servlets work? Instantiation, sessions, shared variables and multithreading.
In JSP/Servlet terms, the basket could be retrieved/precreated as follows:
Basket basket = (Basket) session.getAttribute("basket");
if (basket == null) {
basket = new Basket();
session.setAttribute("basket", basket);
}
// ...
This lives then as long as the user is interacting with the same webpage within the same session. Any products could be added to the basket as follows:
String productCode = request.getParameter("productCode");
Product product = someProductService.find(productCode);
if (product != null) {
basket.addProduct(product);
}
// ...
In JSP you could then display it as follows:
<table>
<c:forEach items="${basket.products}" var="product">
<tr>
<td>${product.code}</td>
<td>${product.description}</td>
<td>${product.quantity}</td>
<td>${product.price}</td>
</tr>
</c:forEach>
</table>