Problems, need help? Have a tip or advice? Post it here.
6 posts Page 1 of 1
I have a product search form that logs the submitted keywords to 'couch/snippets/product-search.log' as follows:
Code: Select all
=======================[2012-02-06 21:20:30]=======================
Keyword Test 1

=======================[2012-02-06 21:20:45]=======================
Keyword Test 2

To display the results in the couch admin panel, I created a template:
Code: Select all
<?php require_once( 'couch/cms.php' ); ?>
<cms:template title='Product Search Log' executable='0' >
<cms:editable name='search_log' type='message'>
<cms:nl2br><cms:embed 'product-search.log' /></cms:nl2br>
<?php COUCH::invoke(); ?>

Is there a way I could reverse the list ordering so the most recent log entries are output at the top. I'm not sure what other applications this could be used for, but I think this reverse functionality would be a good 'log' tag parameter addition.

***** Update: Found one way to do this with php - found it on a manual note.
Code: Select all
<cms:nl2br><cms:php>$myfile = 'couch/snippets/product-search.log';$lines = file($myfile);for($i=count($lines);$i>0;$i--){echo $lines[$i];}</cms:php></cms:nl2br>

I'm not sure if this is the best way to accomplish this, let me know if you have any suggestions.
That is an innovative way of using both the 'message' editable region as well as the log tag :)

There are a couple of points that bother me though -
1. The log will eventually grow large enough to cause performance issues.
2. I am not sure whether you noticed this but the output of the 'message' type editable region is not dynamic - i.e. even if the log changes the output will not reflect this until you access the template via the browser as super-admin (at which point the region re-reads the log to get the modified data).

To handle the first problem, I think we should save the keywords in the proper database within a table and then query it from within the admin-panel to show the values.

The second problem can be rectified by a little hack.
We can use the undocumented 'dynamic' parameter of the editable region (We both have discussed this in a separate thread before - viewtopic.php?f=2&t=3682&p=4839. Also discussed at viewtopic.php?f=4&t=1671&p=2483).

Please make the following changes to your editable region-
Code: Select all
<cms:editable name='search_log' type='message' dynamic='default_data'>

In the snippet above we are declaring the 'default_data' parameter as dynamic which means that this field, instead of containing static data, will contain the name of a snippet. Couch will execute this snippet to dynamically get the data and then put the data within 'default_data' evertime the admin-panel is accessed.

If you are wondering what this 'default_data' parameter is, it actually is any value that is enclosed within the opening and the closing tags of the editable region.
You can see that we are enclosing the name of a snippet 'product-search.htm' within the tag pair.

This is the snippet that Couch will execute every time it has to output the value of the region.
Next we create this snippet named 'product-search.htm' and place it within 'couch/snippets' folder (the usual place for storing snippets).

This is what we can place in the snippet:
Code: Select all
$myfile = '<cms:show k_site_path />my_snippets/product-search.log';
$lines = file($myfile);
   echo $lines[$i] . '<br/>';

This a slightly modified version of your code.
But this time it is completely dynamic. We can place any Couch code here.
We can even place PHP code that accesses the database and gets the values.

I know this hackish. But until the time custom editable regions make their debut, this approach can help quite a bit.

Hope this helps.
When I posted this I was actually unaware that it wouldn't show new data until I accessed the template. I ended up stumbling upon that fact a few hours later, so I'm glad you spotted that issue before I mentioned it. With regard to the log file size, I added a few lines of php to remove the time-stamp and empty lines, then save to the log file. This should slow down the file size growth a bit. I can always just clear the log file if the need arises. For now this method is perfectly adequate, and I will investigate the database route if this changes. Your suggestions worked perfectly, thank you KK.
Hi! KK i was trying the solution you gave to make a region dynamic using dynamic='default_data' i´m trying to create an option with popup edit that allow users to select from a dropdown list a folder of blog.php template to display posts from, it works well if i´m logged in as super-admin but when logged in as administrator the newly created folders are not displayed, is there any way to meke it work for administrator users?
@ar2ro, could you please post the code used to define the 'dynamic' list?
Sure KK sorry this is what i used:

I have a hidden template (sidebar.php) which contains several widgets to display in a sidebar, one of them is widgets posts and this is that code:
Code: Select all
<!-- this option allows to select which widgets to display -->
<cms:repeatable name="sidebar_widgets" label="Selecciona los elementos que deseas mostrar en esta barra lateral" order="1">
  <cms:editable name="widget" label="Elementos" type="dropdown"
             opt_values="latest posts=posts|
             latest tweets=tweets

<!-- this is the option allowed if "posts" was selected -->
<cms:editable name='get_posts' type='message' dynamic='default_data' order="6">
<cms:editable name="posts_count"
                   validator='integer'" />

the file " get-dynamic/posts-dinamically-get-categories.html" contains:
Code: Select all
<cms:editable name="posts_category" 
          label="Articulos del Blog"
          desc="selecciona la categoria de la cual mostrar los articulos del blog"
                 <cms:folders masterpage="blog.php"><cms:show k_folder_title/>=<cms:show k_folder_name/>|</cms:folders>"

If the option "posts" is selected the following snippet is called:
Code: Select all
<div class="widget-body">
    <cms:pages masterpage="blog.php"
                limit="<cms:show posts_count/>"
           folder="<cms:show posts_category/>"
           id="NOT <cms:show k_page_id />">
        <div class="media">
            <a class="pull-left" href="<cms:show k_page_link/>">
                <img alt="<cms:show k_page_title/>" src="<cms:thumbnail imagen />">
       <div class="media-body">
            <h4 class="media-heading">
              <a href="<cms:show k_page_link/>">
                  <cms:excerpt count="20" truncate_chars="1">
                      <cms:show k_page_title/>
    <cms:popup_edit "get_posts|posts_category" class="sidebar-widget" link_text="<i class='fa fa-pencil-square-o'></i> Editar" prompt_text="No has guardado los cambios de tu última edición, por favor guárdalos para continuar."/>

This way when i edit the widget it display a list of folders from blog.php:
Untitled.png (115.43 KiB) Viewed 2330 times

But newly created folders are only shown when logged in as super-admin, the administrator can´t see them
6 posts Page 1 of 1

Who is online

In total there are 8 users online :: 1 registered, 0 hidden and 7 guests
(based on users active over the past 5 minutes)

Users browsing this forum: Google [Bot] and 7 guests