We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 506
    • 20 Posts
    I have created a script, using PHP, to allow me to add new sales leads to a database table. My script also allows me to view/display all rows of data contained in my "leads" database table. My script is working properly outside of MODx, but I have a few questions as to the best way to intigrate it with my MODx install.

    1. Should I add the script to the site using a new document with assigned access permission granting access only to site administrators or company staff? Seems a bit redundant If I do this, becuase I am thinking the sales department will have to register and manage a new account in addition to their current admin (MODx Manager) account.

    2. Should I create a custom module for the script and advice the sales depart to login to the MODx Manager and then have them Run the module from within the Manage Modules area?

    3. Is this better fit as a plugin or snippet?

    4. I just keep the entire script out of MODx and move on to the next project. (Lazy way out)

    Due to the extreme lack of easy to follow documentation and examples for adding custom scripts to MODx I am a bit confused as to how to go about it. I would prefer that an employee from the sales department could just use their MODx Manager login info to login and view sales leads stored in the database.

    Any pointers you all have will be greatly appreciated.


    Thanks,

    Admix
      • 22303 MODX Staff
      • 10,725 Posts
      Put it on a document and create a snippet to wrap your script and do something like this...

      if ($modxUserId = $modx->getLoginUserID('mgr')) {
          return include(MODX_BASE_PATH . 'path/to/myscript.php');
      } else {
          $modx->sendUnauthorizedPage();
      }


      Note: If you are echo’ing content in your script, you’ll want to wrap it in an output buffer. Not a lot of rules other than understanding that a snippet or plugin is eval()’d.
        • 506
        • 20 Posts
        Thank you for your reply,

        So are you saying it is better for me to put my script into a snippet, then call the snippet from within a web document, then for security reasons I need to create a second login account for the sales staff (aside from the Manager login they already have) so they can login to the site as a web user (not a manager) and then navigate to the page to view their sales leads from my database script?

        If that is in fact your suggestion, forgive me but it seems kind of redundant to force them to maintain two separate login accounts for managing their site (1 for MODx Manager and another account for managing sales leads). In my opinion adding, deleting, editing, and/or viewing sales leads is in fact a Managerial sort of task and would constitute being able to do it from within the Manager area of MODx. Allowing them the convenience of having to remember only a single user/pass to perform site maintenance. The people (in-house staff) who will be maintaining the sales leads database are also in charge of updating their own document content.

        Sorry I just want to make sure you are clear on what I am trying to accomplish here. Please let me know if I am misunderstanding things here.
          • 22303 MODX Staff
          • 10,725 Posts
          No, I was suggesting you let them use their existing manager user account with that code. Notice the ’mgr’ which checks that the user has a valid manager session or it will not return a user ID.
            • 506
            • 20 Posts
            Ok gotcha, so let me make sure I am understanding correctly now.

            I create a new snippet, put that code in it, create a new web document and title it say "Sales Leads", then insert a snippet call into that web document calling the new snippet I created.

            How do you suggest I instruct them to where to go to access the page to manage the sales leads? Since it is for mangers only and not public web users I obviously won’t make it a public URL. I could just give them the URL, ask them to write it down or remember it. Or is it possible to add a link to the sales leads management page from within MODx Manager. That is why I was initially trying to add my script as a module, hoping they could easily navigate through the manager, then click on Manage Modules, find my "Show Sales Leads" module and finally, simply click "Run Module".
              • 22303 MODX Staff
              • 10,725 Posts
              Well, if you want them to access it through the manager as module, then do it that way.

              Otherwise, the current access system and most of the pre-existing add-ons in the repository are made to hide/show links and automatically control access to documents on the front-end using web-users. This is an unfortunate redundancy, but this separation of user systems into web and manager is going away in the future in favor of a single user system that allows users to login and out of specific "contexts" defined in MODx.
                • 506
                • 20 Posts
                OK, so I figured out how to get my script to be accessible to only Manager Users this takes care of my issue of wanting site admins to only have to manage 1 set of login info in order to manage the sales leads. What I did was created a new document for my show_leads script then removed the check mark on "Published" so that the general public won’t be able to view it, I also removed the check mark on that documents Access Permissions (All Document Groups (Public)). The "Show Leads" page/document is now only viewable by a site manager only after they login to the Manager now.

                Next I created a new snippet and dropped in my "Show Leads" php script, then I placed a snippet call to the script in my Web Document. However then I went to test view the page and see if the results would display as anticipated but I am getting eval()’d ERRORS and no db rows are displaying.

                Can someone please explain how I should eval out my display script and/or get it working?

                Thanks
                  • 4310
                  • 2,310 Posts
                  Could you post your snippet?
                    • 506
                    • 20 Posts
                    sure but never posted code in the forums before. Is it ok to just copy paste the PHP code in full or do I have to wrap in special MODx forum tags or something?
                      • 506
                      • 20 Posts
                      Quote from: bunk58 at Feb 25, 2008, 02:51 AM

                      Could you post your snippet?

                      <?php
                      // Call database connection info
                      require("/leads/config.php");
                      
                      $result = mysql_query("SELECT * FROM csb_leads order by firstname");
                      echo "<br /><br /><table border='1'>
                      <tr>
                      <th>First Name</th>
                      <th>Last Name</th>
                      </tr>";
                      
                      while($row = mysql_fetch_array($result))
                        {
                        echo "<tr>";
                        echo "<td>" . $row['firstname'] . "</td>";
                        echo "<td>" . $row['lastname'] . "</td>";
                        echo "</tr>";
                        }
                      echo "</table>";
                      mysql_close($con)
                      ?>


                      This script is connecting to another mysql database on the same localhost and not the DB I have MODx installed on. Also you may notice for now I am just testing the script with only a couple form values right now (firstname & lastname) once it is working I will come back and add the remaining 22 table columns I have in the leads data I am gathering.

                      Thanks Bunk!