1

I'm a novice in ExpressionEngine, but with plenty of background in PHP. Still, I'm struggling with the EE parsing order.

Here is my situation and I would be very, very thankful if anyone can give me some/any guidelines how to approach it or what I'm doing wrong.

I'm having a page that renders some content and there is a link to reveal more. The trick is that revealing that more content is password gated, but with no membership complexity.

I've got a static password (yeah, I know) that is saved as a snippet. When the user clicks to reveal more content a dialog is shown, asking for the password and then the form posts to the same page.

When the page loads it checks to see if a session variable is set and if not, sets it, then re-directs to itself (to clean up the posted values).

The page re-loads and checks for the session variable. If it is set it displays the more content, otherwise displays the link to ask for password.

Here is the snippet. I've renamed the variables, so they make more sense.

{exp:switchee variable="{exp:session_variables:get name='session_variable'}" parse="inward"}
  {case value=""}
    {if '{post:posted_variable}' != '' AND '{post:posted_variable}' == '{snippet_variable}'}
      {exp:session_variables:set name='session_variable' value='1'}
    {/if}
    {exp:redirectee:redirect status='302' url='{current_url}'}
  {/case}
{/exp:switchee}

... Content ...

{if '{exp:session_variables:get name='session_variable'}' == '1'}
  <p>... MORE CONTENT OF SOME KIND ... an embed/snippet even</p>
{if:else}
  <a href="#password-modal">Click here to view More Content</a>
  <div id="password-modal">
    <form action="{current_url}" method="post"><input name="posted_variable" type="password" /><input type="submit" value="Go" /></form>
  </div>
{/if}

... JS to handle the modal ...

My understanding is that EETemplate parses first the {exp} tags, then the advanced {if}, which made me use switchee with the parse="inward" attribute. I used to have it in an {if} conditional. Still the switchee case is not firing, and the previous setup always fired the session set and redirects.

Thanks in advance.

Third party stuff:

EDIT:

In addition to the above. I've tried extracting the top code to separate page, and the form action sends the user there, which should redirect back. It's all using switchee, which looks very complicated for such a simple thing! It still doesn't work.

{exp:switchee variable="{exp:session_variables:get name='session_variable'}" parse="inward"}
  {case value="1"}
    {exp:redirectee:redirect status='302' url='{last_page_visited}?status=allok'}
  {/case}
  {case default="no"}
    {exp:switchee variable="{post:posted_variable}" parse="inward"}
      {case value="{snippet_variable}"}
        {exp:session_variables:set name='session_variable' value='1'}
        {exp:redirectee:redirect status='302' url='{last_page_visited}?status=good'}
      {/case}
      {case default="yes"}
        {exp:redirectee:redirect status='302' url='{last_page_visited}?status=bad'}
      {/case}
    {/exp:switchee}
  {/case}
{/exp:switchee}

For some reason {case value="{snippet_variable}"} is never considered.

{case default="yes"} is the only case that is considered. Simply setting it to {case default="no"} breaks the page.

Any suggestions?

0

1 Answer 1

1

Well... since noone could answer me, in time, I've resolved it with pure PHP and OO ExpressionEngine.

I'll share my code, just if anyone has similar issue or need.

As per my edit in the question area I've moved the logic in a separate page - check_password.html with PHP ON at the Output.

To use the following code, create a snippet variable with a given name (snippet_variable in my sample case), add an html form (shown further down) that posts to this page a variable with a given name (post_variable in my case). Then when the last_page_visited gets re-loaded from the redirect at the end of the code, check for the session variable (session_variable in my case) and show whatever you want.

<?php
$post_field_key = 'post_variable';
$snippet_field_key = 'snippet_variable';
$session_field_key = 'session_variable';
$session_field_value = '1'; # The value you want the session_variable set to
$bad_request_message = 'Bad request!';
$bad_data_message = 'Wrong password!';
$flashdata_key = 'error_message';

### This does what Mo Variables' last_page_visited does
if ( isset( $this->EE->session->tracker[1] ) ) {
  if ( $this->EE->session->tracker[1] === 'index' )
    $last_page_visited = $this->EE->functions->fetch_site_index( true );
  else
    $last_page_visited = $this->EE->functions->create_url( $this->EE->session->tracker[1] );
} else
  $last_page_visited = $this->EE->config->item( 'site_url' );

### This compares the posted_variable to the snippet_variable
if ( ! empty( $_POST ) && $_POST[$post_field_key] == $this->EE->config->_global_vars[$snippet_field_key] ) {
  $sid = session_id();

  ### First check if there is a session and if we can start one
  if ( empty( $sid ) ) {
    if ( ! headers_sent() )
      session_start();
    else {
      ### This is executed if a session couldn't be started
      $this->EE->session->set_flashdata( $flashdata_key, $bad_request_message );
      $this->EE->functions->redirect( $last_page_visited );
      die();
    }
  }

  $_SESSION[$session_field_key] = $session_field_value;
} else {
  ### This is executed if the page is visited without proper POST data
  $this->EE->session->set_flashdata( $flashdata_key, $bad_data_message );
}

### Always redirect back to the last_page_visited (or the index)
$this->EE->functions->redirect( $last_page_visited );
die();
?>

This is my partial, that renders the link (all code below goes in one page, ofc):

{if '{exp:session_variables:get name='session_variable'}' == '1'}
  <p>GOT THE SESSION SET CORRECTLY</p>
{if:else}
  <div class="error-message">
    {flashdata:error_message}
  </div>
  <a href="#show-modal" class="btn" data-toggle="modal">Click here to view HIDDEN CONTENT</a>
{/if}

And this is the modal code, but you could easily have it in-line above:

<div class="modal hide" id="show-modal">

  <div class="modal-body">
    <button class="close" data-dismiss="modal"><span class="visuallyhidden">Close</span></button>

    <form action="{path='path-to/check_password}" method="post">
      <div>
        <input class="span2" type="password" name="post_variable" value="" autocomplete="off" />
        <input class="btn" type="submit" value="Go" />
      </div>
    </form>

  </div>
</div>

Show/hide of the modal is handled by Twitter's Bootstrap.

At the end I'm still using Session Variables - http://putyourlightson.net/session-variables-plugin, but dropped the need of Mo Variables and Redirectee.

In all honesty if I'm not using EE template "code" I'm fine, but otherwise I hate EE with its rendering and variable injection/substitution.

That's it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.