Problems, need help? Have a tip or advice? Post it here.
6 posts Page 1 of 1
Hello,

I have a PHP script that stores data with arrays, and contains "send-email" functions. (for a form).

I wanted to add the possibility to my client to add his own data using CouchCMS, but it's not working. My pages are loading forever and then it gives an error saying the page loaded for too long so it stopped. Some sort of infinite loop I guess?

Here's my code :

Code: Select all
<?php require_once( 'edit/cms.php' ); ?>
<cms:template title='Magasins' order="0" clonable="1">

<cms:repeatable name='manager_pass' label="Mots de passe pour Directeur" >
<cms:editable type='text' name='manager_pass_text' label='Mot de passe du Directeur' />
</cms:repeatable>

<cms:repeatable name='inspector_pass' label="Mots de passe pour Superviseur" >
<cms:editable type='text' name='inspector_pass_text' label='Mot de passe du Superviseur' />
</cms:repeatable>

<cms:repeatable name='submissions_pass' label="Mots de passe pour Magasin" >
<cms:editable type='text' name='submissions_pass_text' label='Mot de passe du Magasin' />
</cms:repeatable>

<cms:repeatable name='email_to' label="Envoyer aux courriels..." >
<cms:editable type='text' name='email_to_address' label='Courriel' />
</cms:repeatable>

</cms:template>
<cms:php>

$data = array();
$data[] = array(
      "store_title" => "<cms:show k_page_title />",
      "manager_pass" => array("12345"),
      "inspector_pass" => array("12345"),
      "submissions_pass" => array("qwerty"),
      "email_to" => array("email1@gmail.com","email2@gmail.com")
   );



$email_data = array(
   "reply_to" => "",                                        // Set your email here if you wanna resive copies of all emails
   "email_from" => "email@email.com",            // Email from
   "name_from" => "Client name",                             // Name from
   "subject" => "Rapport Contrôle des travaux - [STORENAME] - [DATE]", // Email subject (you can use [STORENAME] and [DATE] placeholders)
   "filename" => "rapport-travaux-promaintenance.pdf",                               // pdf file name, for example "filename.pdf"
   "body" => "Bonjour,<br/>
Vous trouverez en pièce jointe le rapport de contrôle des travaux pour le magasin [STORENAME] pour [DATE].<br/>
Merci de votre confiance<br/>
Client name<br/>",          // plain text (you can use [STORENAME] and [DATE] placeholders and <br/> for line break)
);
</cms:php>
<?php COUCH::invoke(); ?>


I haven't put all the "cms:show" elements yet, but still, I tried just using the "cms:show k_page_title" and nothing shows up. I mean, the page don't even load. Any idea what I am doing wrong here?

By the way, I am embedding this file (that is named "data.php") on a index.php file, using :

Code: Select all
include_once("data.php");


Along other PHP stuff.

Thanks!
Anyone? Thanks!
Your template is clonable. Apparently your script uses "<cms:show k_page_title />" which makes sense only in page-view. Without this knowledge you will find it very difficult to debug your code. Therefore I recommend to review again the "views" - home-view and page-view. http://docs.couchcms.com/concepts/views.html

The source of main trouble -
the page don't even load
- you should review again the sense of "embedding" in CouchCMS Vs "including" in php. (http://docs.couchcms.com/tags-reference/embed.html is about CouchCMS, but you seem trying to apply this to the way php works. No good.
In simple words, after you include couch-managed template data.php into non-couch php file index.php, Couch starts to think that index.php is a couch-managed template.

Php code runs before the CMS code and Couch sees <cms:template> block in a united file index.php, which messes the whole thing in my opinion.
Hi Anton,

Ok it's been a while since you answered this question, but let me go deeper into it with different code.

So, I have an index.php page, which is getting data from a "data.php" page.
It's sort of a single page form web app. So there's multiple scripts and stuff making it work.
Right now, I edit the "data.php" file manually. There's multidimensional arrays in there.
It looks like this (it changed a bit since first post) :

Code: Select all
      $data[] = array(
      "store_title" => "Title here",
      "submissions_pass" => array("Passwordhere"),
      "manager_names" => array(
         "Name here"),
      "manager_emails" => array(
         "email1@here.ca",
      ),
      "email_to" => array(
         "email2@here.com",
         "email3@here.com",
         "email4@here.com"
      ),
      "inspector_emails" => array(
         "email5@here.com",
      ),
   );


There's other stuff in the page, but that's the only section I duplicate if I want to add a "store" to the front-end.
So what I need, is just to duplicate that part. Nothing needs to change on the index.php page, or anywhere else.

Here's what I thought I would do.
I have created a new file. "my-data.php".
This is the clonable template file. Here's the code for "my-data.php" :

Code: Select all
<?php require_once( 'edit/cms.php' ); ?>
<cms:template title='Data ok' order="" clonable='1'>
    <cms:editable name='subpass' label="Submission Password" type='text' order="1" />
    <cms:editable name='managername' label="Manager name" type='text' order="1" />
    <cms:editable name='manageremail' label="Manager email" type='text' order="1" />
    <cms:editable name='emailto' label="Email to" type='text' order="1" />
    <cms:editable name='inspectoremail' label="Inspector email" type='text' order="1" />
</cms:template>
<?php COUCH::invoke(); ?>


That way I can create multiple cloned pages there with different data.

After that, that's how I thought I could edit the main file (data.php) to make this work (that's the full code) :

Code: Select all
<?php require_once( 'edit/cms.php' ); ?>
<cms:php>
   $label_data = array();
   $label_data['pdf-label'] = "Envoyer le rapport PDF";
   $label_data['link-label'] = "Envoyer le lien pour compléter la procédure";

   / managers emails /
   $data = array();
   
   <cms:pages masterpage='my-data.php'>
   $data[] = array(
      
      "store_title" => "<cms:show k_page_title />",
      "submissions_pass" => array("<cms:show subpass />"),
      "manager_names" => array(
         "<cms:show managername />"),
      "manager_emails" => array(
         "<cms:show manageremail />",
      ),
      "email_to" => array(
         "<cms:show emailto />"
      ),
      "inspector_emails" => array(
         "<cms:show inspectoremail />",
      ),
      
   );
</cms:pages>
</cms:php>
<?php COUCH::invoke(); ?>


That way, creating a new cloned item using the "my-data.php" template would add a new block of array.
But it's not working...
I thought this would be easy but I'm having hard time with this.

Again, I want to edit the content of the "data.php" file only.
Is that possible with Couch?

Thanks a lot, hope it's clearer now.
Fixed
@larin555

The file you are working with are:
    data.php
    index,php

Lets proceed:

Step #1
We will create a new file, named, data-edit.php (ar any name you want). This file shall contain the editable regions that are required by you. Also, this will be a clonable couch template. The data-edit.php will contain:
Code: Select all
<?php require_once( 'edit/cms.php' ); ?>
<cms:template title='Data - Edit' order="" clonable='1'>
    <cms:editable name='subpass' label="Submission Password" type='text' order="1" />
    <cms:editable name='managername' label="Manager name" type='text' order="1" />
    <cms:editable name='manageremail' label="Manager email" type='text' order="1" />
    <cms:editable name='emailto' label="Email to" type='text' order="1" />
    <cms:editable name='inspectoremail' label="Inspector email" type='text' order="1" />
</cms:template>
<?php COUCH::invoke(); ?>


Step #2

In the data.php do not include the boilerplates of Couch. This is inportant because, in the index.php file you have included the data.php file and since the index.php file contains the bilerplates, it will not allow the data to be fetched and brought into the dropdown or any other place, where required.

Make the following changes to the data.php file:

original code:
Code: Select all
<?php
   $label_data = array();
   $label_data['pdf-label'] = "Envoyer le rapport PDF";
   $label_data['link-label'] = "Envoyer le lien pour compléter la procédure";

   /* managers emails */
   $data = array();

   $data[] = array(
      "store_title" => "Super C - P-A-T - #5963",
      "submissions_pass" => array("qwerty"),
      "manager_names" => array(
         "Nathalie Couture"),
      "manager_emails" => array(
         "info@mediodesign.ca",
      ),
      "email_to" => array(
         "info@mediodesign.ca"
      ),
      "inspector_emails" => array(
         "info@mediodesign.ca",
      ),
   );

        // -------------------------------------------------------------
   // Here goes the pre-generation part of some data
   // WARNING: Next data is used in script. Edit on your own risk

   // Generate lowercase names for manager name-check
   for ($i = 0; $i < count($data); $i++) {
      $data[$i]['manager_names_lc'] = array();
      if(isset($data[$i]['manager_names']) && is_array($data[$i]['manager_names'])){
         foreach ($data[$i]['manager_names'] as $manager_name) {
            $data[$i]['manager_names_lc'][] = strtolower($manager_name);
         }
      }
   }
?>


Couchified Equivalent Code:
Code: Select all
<cms:template title='Data Array' order="2" />
<cms:php>
   $label_data = array();
   $label_data['pdf-label'] = "Envoyer le rapport PDF";
   $label_data['link-label'] = "Envoyer le lien pour compléter la procédure";

   /* managers emails */
   $data = array();
   
   <cms:pages masterpage='my-data-edit.php'>
   $data[] = array(
      
      "store_title" => "<cms:show k_page_title />",
      "submissions_pass" => array("<cms:show subpass />"),
      "manager_names" => array(
         "<cms:show managername />"),
      "manager_emails" => array(
         "<cms:show manageremail />",
      ),
      "email_to" => array(
         "<cms:show emailto />"
      ),
      "inspector_emails" => array(
         "<cms:show inspectoremail />",
      ),
      
   );
   </cms:pages>
   
   
// -------------------------------------------------------------
   // Here goes the pre-generation part of some data
   // WARNING: Next data is used in script. Edit on your own risk

   // Generate lowercase names for manager name-check
   for ($i = 0; $i < count($data); $i++) {
      $data[$i]['manager_names_lc'] = array();
      if(isset($data[$i]['manager_names']) && is_array($data[$i]['manager_names'])){
         foreach ($data[$i]['manager_names'] as $manager_name) {
            $data[$i]['manager_names_lc'][] = strtolower($manager_name);
         }
      }
   }
</cms:php>


After completing these two steps make sure to visit the data-edit.php and the data.php files and register them in the Couch Admin. You are now set to add the details through the couch panel for the store, the manager, etc.

Step #3

We can now edit the index.php file. Add the boilerplates to the file to make it a Couch editable template. Now as an example, we will fetch the details from the data-edit.php and put them in the dropdown named Magasin. You will have to follow similar steps to be able to fetch data on the index.php page for the rest of the places.

Original Code:
Code: Select all
<?php 
    foreach($data as $key=> $store){
        echo '<option value="'.$key.'">'.$store['store_title'].'</option>';
    }
?>


Couchified Equivalent Code:
Code: Select all
<cms:pages masterpage="my-data-edit.php">
    <option value="<cms:show k_page_id />"><cms:show k_page_title /></option>
</cms:pages>


You can do the same for the modal invoked by the button named Générer un lien
Original Code:
Code: Select all
<?php 
    foreach($data as $key => $value) {
        foreach($value['manager_emails'] as $emails) {
?>
            <li class="email-address store-<?php echo $key; ?>"><?php echo $emails; ?></li>
<?php
         }
     }
?>


Couchified Equivalent Code:
Code: Select all
<cms:pages masterpage="my-data-edit.php">
    <li class="email-address store-<cms:show k_page_id />"><cms:show manageremail /></li>
</cms:pages>


Step #4
To confirm if the data is being fetched or not, please do the following:
Save and refresh the index.php file in the browser and the visit the Backend of Couch and refresh it too for allowing couch to pick up the changes. Then refresh the index.php page in the browser and view its source. On the source page, scroll down to the page bottom. You will find a code line:
Code: Select all
<script src="js/webflow.js" type="text/javascript"></script>

Right below this line you will find the json_encoded data of the details fetched from the data.php file.

Please post in the forum for anything required further.

Regards,
Aashish
Image
where innovation meets technology
6 posts Page 1 of 1