Archive for November, 2011

Using NCache to store image thumbnails

Wednesday, November 30th, 2011

There are many ways to handle image thumbnails.  Sites with heavy traffic generate resized copies right after the original image is uploaded. Light traffic sites on the other hand, prefer to wait until the thumbnail is actually requested. Nette offers a few creative ways to handle this:

  1. You can use .htaccess to catch requests for non-existent thumbnails and redirect them to your script.
  2. Following the same theme, you can also use your ErrorPresenter to generate the thumbnail and override the response code (so that it’s not 404).
  3. An unusual solution is to use a helper and generate the thumbnail when rendering your template.
  4. Or simply use a presenter action.

In any of these cases, you need to decide where you’re going to store the thumbnail and how you’re going to delete/update it when the main image changes. This is where you can use the excellent built-in caching aparatus.

(more…)

n:src – the missing twin brother of n:href

Wednesday, November 23rd, 2011

As you have probably guessed by now, I love Nette. The more it hurts when I see the documentation lag behind the quality of the framework. Although I don’t understand why Latte built-in macros don’t include n:src when the oh-so-useful n:href is there my main concern however, is that nowhere in the documentation you’ll find detailed information on how to add it yourself. You’re condemned to endlessly search through forum again and crawl through various outdated subdomains of nette.org.

Here’s my solution to the n:src alias based on a post in the cookbook and the built-in n:href macro. You can also take a peek an another post of mine on how to tweak your forms using macros. As always, hopefully it’ll save you some time.

(more…)

Repeated forms on the same page

Tuesday, November 22nd, 2011

Often you want the user to have a form not just for one entity but for all of them on the same page. A form for each product in the same category, for example. I’m not talking about multiple different forms but multiple instances of the same form.

This topic has been discussed in the Nette forum so many times that it’s really a shame that it’s still missing in the documentation. Frustrated after an hour spent reading outdated forum posts, I offer here a complete solution based on one of the posts.

(more…)

Refreshing current user’s identity

Wednesday, November 9th, 2011

Nette stores current user’s identity in session. There are a few situations when you want to reload the identity or change it completely. For example, the user updates his profile and you want the change reflected in the page header. Or a super administrator is allowed to re-authenticate as a different user without having to know the other user’s password.

NUser has a convenient method called setIdentity() but unfortunately, it’s declared as private so you can’t use it. The solution is either to loop through the current identity and update its fields one by one, or do it nice and clean with a custom authenticator. Let me show you.

(more…)

Testing for unique value during form validation

Monday, November 7th, 2011

Let’s talk about what happens when a user signs up for a new account. He fills in his nickname, e-mail address and password and clicks Register. When validating the e-mail address you need to check for at least three things:

  1. the user provided one
  2. the syntax is all right – the e-mail is formally valid and can in fact exist (which doesn’t mean it really exists)
  3. the address hasn’t been used yet for some other account

So far not very difficult.

The tricky part comes when you allow the user to edit his profile. He can change any of the three values. When he submits the edit form the situation is one step more difficult than during registration. The e-mail address has in fact already been used – for the very user that is submitting the form. That is not the tricky part. The fun is how to get the user id when validating his e-mail address.

(more…)