We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 38783
    • 571 Posts
    Thanks Bob. It does not seem to work though.

    My page where a new editor initially creates their page has this code
    [[!onePageOnly]]
    [[!NewsPublisher@rc?
    &show=`pagetitle`
    &initrte=`0`
    &initdatepicker=`1`
    &parentid=`35`
    &published=`1`
    &hidemenu=`1`
    &templateid=`10`
    &ErrorTpl=`rcErrorTpl`
    &ownPagesOnly=`1`
    ]]


    The [[!onePageOnly]] snippet contains your code

    <?php
    /* Keep non-admins from creating more than one Doc */
       
    $sorryChunk = 'NpSorryOverLimit';    // Name of chunk telling them they're out of luck.
    $pageLimit = 1;
     
      
       
    if (! $modx->user->isMember('Administrator')) {
        /* Don't act if user is editing an existing page */
        if (!isset($_SESSION['np_doc_to_edit'])) {
            /* Check the user's page count */
            $userId = $modx->user->get('id');
            $count = (int) $modx->getCount('modResource', array('createdby' => $userId));
            if ($count >= $pageLimit) {  /* They've already created at least one page */
                 $msg = $modx->getChunk($sorryChunk);
                 die ($msg);
            }
        }
    }
    


    And the NpSorryOverLimit also contains your code

    <h3>Sorry, you're limited to creating a single page, and you've already done that<h3>


    However, a user who access this page to create their first page seems to be able to revisit the page and create as many pages as they like.


    Could it be that &ownPagesOnly=`1` only works when used with the NpEditThisButton snippet and not the NewsPublisher snippet?


      If I help you out on these forums I would be very grateful if you would consider rating me on Trustpilot: https://uk.trustpilot.com/review/andytough.com

      email: [email protected] | website: https://andytough.com
      • 3749
      • 24,544 Posts
      It's on the NP page so it should definitely work there. I can't see any reason why it won't work, but I confess that I didn't have time to test it. Did you try it with the redirect to another page:

      Put this as the top
      $sorryPageId = 12; // change to the ID of your "sorry" resource which should contain a chunk tag for the "sorry" chunk.


      Replace the "die()" line with this:

      $url = $modx->makeUrl($sorryPageId, "", "", "full");
      $modx->sendRedirect($url);


      [Update] I had a thought on what could be happening. The session variable may not be being cleared reliably, so if the user has ever used the "Edit" button, they may get a pass to create another doc.
        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 42562
        • 1,145 Posts
        I wrote a tutorial on how I did this, but I can't find it, it was dead simple at the time. I used it to allow users create/delete their public profiles, and of course one profile per user.

        I had a tiny getresources or own snippet to go check if a certain parent resource had any children whose createdby/publishedby value matched the username of current attempting user.
        If there was a match, the NP create/edit page would redirect to the existing profile. And the user had to go to their account to edit the public profile, as I removed the edit button o the profile page.
        And if no match, the NP page proceeded to present itself.

        In a more evolved and developed mental unsteadiness, I made TinyMagicPublisher that has all of this under perfect control.
        Here is an excerpt from the TinymceWrapper Suite
        <?php
        //You simply have to force your resource url to have parameters
        //www.about.com/profiles?create=1
        //www.about.com/profiles?edit=1
        //And make sure that the [[!NewsPublisher]] never is fired without a valid parameter
        
        function redirectTo($desti,$param){
          global $modx;
          $url = $modx->makeUrl($desti,'',$param,'full');
          $modx->sendRedirect($url);
        }
        // $npCreateResource = the id of ya newspublisher resource
        //let's set up some restrictions
        if (isset($_GET['edit']) && !isset($_GET['create']) && $modx->resource->get('id') !== $npCreateResource ) {
          $legalEDITparam = 1;
        }
        if (isset($_GET['create']) && !isset($_GET['edit']) && $modx->resource->get('id') == $npCreateResource ) {
          $legalCREATEparam = 1;
        }
        if (!$modx->hasPermission('new_document') || $_GET['create'] !== 'true') {
        // at this point you may add more logic to find if this user has created a resource before. Best to check against a resource parent to see if any child belongs to said user
          $legalHASpermission = 1;
        }
        if ($modx->resource->get('createdby') == $modx->user->get('id') || $modx->user->isMember(listArray($editorAdminGroups))) {
          $legalOWNER = 1;
        }
        //reset these session variables so that NewsPublisher can have clean slate for Editing existing resource or creating a new one
        if(isset($_SESSION['np_doc_id'], $_SESSION['np_doc_to_edit'])) {
          unset($_SESSION['np_doc_id'], $_SESSION['np_doc_to_edit']);
        }
        //grab url parameters and init NewsPublisher accordingly
        if ($legalEDITparam == 1) {
          //return to resource url withou the edit parameter
          if ($_GET['edit'] !== 'true' || !$legalOWNER == 1) {
            redirectTo($modx->resource->get('id'),'');
          }
          //restrict editing to owner of resource or Admin editors
          if ($legalOWNER == 1) {
            //give NewsPublisher snippet some food
            $_SESSION['np_doc_to_edit'] = $modx->resource->get('id');
            $_SESSION['np_doc_id'] = $modx->resource->get('id');
            echo $buttonsANDnewspublisher; //e.g [[!NewsPublisher]]
          }
        }
        elseif ($legalCREATEparam == 1) {
          if ($legalHASpermission) {
            redirectTo($npCancelCreateId,'');
          }
          echo $buttonsANDnewspublisher; //e.g [[!NewsPublisher]]
        }
        else{
          if($createNewButton){
            $createNewButton = '<a href="[[~'.$npCreateResource.' ? &create=`true` &scheme=`full`]]">'.$createNewButton.'</a>';
          }
          else{
            $createNewButton = '';
          }
          if($editThisButton){
            $editThisButton = '<a href="[[~[[*id]]? &edit=`true` &scheme=`full`]]">'.$editThisButton.'</a>';
          }
          else{
            $editThisButton = '';
          }
          if (!$modx->hasPermission('new_document')) {
            $createNewButton = '';
          }
          if (!$legalOWNER || !$modx->hasPermission('edit_document') ) {
            $editThisButton = '';
          }
          if (!$legalOWNER || !$modx->hasPermission('edit_document') && !$modx->hasPermission('new_document') ) {
            return;
          }
          $editCreateButtons = '<div class="publishButtons">'.$editThisButton.$createNewButton.'</div>';
          echo $editCreateButtons;
        }
        

          TinymceWrapper: Complete back/frontend content solution.
          Harden your MODX site by passwording your three main folders: core, manager, connectors and renaming your assets (thank me later!)
          5 ways to sniff / hack your own sites; even with renamed/hidden folders, burst them all up, to see how secure you are not.
          • 3749
          • 24,544 Posts
          It would be easy enough to do inside NewsPublisher, but I was trying to come up with a separate snippet to add to the top of the page. It may not be possible because of the security feature that prevents users from editing disallowed docs via a $_SESSION variable.
            Did I help you? Buy me a beer
            Get my Book: MODX:The Official Guide
            MODX info for everyone: http://bobsguides.com/modx.html
            My MODX Extras
            Bob's Guides is now hosted at A2 MODX Hosting
            • 38783
            • 571 Posts
            Hello Bob, sorry for my absence from the conversation. Visiting parents with no internet connection followed by a backlog of work has kept me away.

            I am delighted to say that the snippet now works with one hurdle to overcome. If a user has no pages, logs in and makes a page they can then carry on making as many pages as they like until the log out. When they log back in the snippet kicks in and prevents them from adding additional pages. I am hopeful that this is a minor hurdle?

              If I help you out on these forums I would be very grateful if you would consider rating me on Trustpilot: https://uk.trustpilot.com/review/andytough.com

              email: [email protected] | website: https://andytough.com
              • 3749
              • 24,544 Posts
              Is your NewPublisher tag called uncached?

              [[!NewsPublisher ... ]]
                Did I help you? Buy me a beer
                Get my Book: MODX:The Official Guide
                MODX info for everyone: http://bobsguides.com/modx.html
                My MODX Extras
                Bob's Guides is now hosted at A2 MODX Hosting
                • 38783
                • 571 Posts
                Hello Bob, yes the NewsPublisher tag is called uncached. I have also disabled caching for the whole page but it still allows multiple pages to be created until the user logs out and back in again. Then the snippet works.
                  If I help you out on these forums I would be very grateful if you would consider rating me on Trustpilot: https://uk.trustpilot.com/review/andytough.com

                  email: [email protected] | website: https://andytough.com
                  • 3749
                  • 24,544 Posts
                  Here are a couple things to try:

                  First,

                  In the NewsPublisher class file (core\components\newspublisher\model\newspublisher\newspublisher.class.php), around line 2175,

                  change this:

                  /* clean post array */
                  $_POST = array();


                  with this:

                  /* clean post array */
                  $_POST = array();
                  unset($_SESSION['np_doc_to_edit']);


                  If that doesn't work: add this third line at the end:

                  $modx->reloadContext($this->resource->get('context_key'));
                    Did I help you? Buy me a beer
                    Get my Book: MODX:The Official Guide
                    MODX info for everyone: http://bobsguides.com/modx.html
                    My MODX Extras
                    Bob's Guides is now hosted at A2 MODX Hosting
                    • 38783
                    • 571 Posts
                    I think I know what is causing the issue. Because the page that is used to create new pages is 'the page being edited' the Don't act if user is editing an existing page bit of the code prevents the snippet from running when a second visit is made to the page in one login session.

                    If I comment out if (!isset($_SESSION['np_doc_to_edit'])) it works as expected.

                    I think this will work OK for me, as I am only calling the OnePageOnly on this page. Other pages where they are editing their content simply do not have the ability to create new resources visible.

                    Let me know though if I am creating a different, unforeseen problem for myself by doing this!


                    <?php
                    /* Keep non-admins from creating more than one Doc */
                    $sorryPageId = 160; // change to the ID of your "sorry" resource which should contain a chunk tag for the "sorry" chunk.   
                    $sorryChunk = 'NpSorryOverLimit';    // Name of chunk telling them they're out of luck.
                    $pageLimit = 1;
                     
                      
                       
                    if (! $modx->user->isMember('Administrator')) {
                        /* Don't act if user is editing an existing page */
                        /*if (!isset($_SESSION['np_doc_to_edit'])) {*/
                            /* Check the user's page count */
                            $userId = $modx->user->get('id');
                            $count = (int) $modx->getCount('modResource', array('createdby' => $userId));
                            if ($count >= $pageLimit) {  /* They've already created at least one page */
                                 $msg = $modx->getChunk($sorryChunk);
                                 $url = $modx->makeUrl($sorryPageId, "", "", "full");
                                 $modx->sendRedirect($url);
                            /*}*/
                        }
                    }



                      If I help you out on these forums I would be very grateful if you would consider rating me on Trustpilot: https://uk.trustpilot.com/review/andytough.com

                      email: [email protected] | website: https://andytough.com
                      • 38783
                      • 571 Posts
                      Oh, we have both posted at the same time! I will look to see what you have said.
                        If I help you out on these forums I would be very grateful if you would consider rating me on Trustpilot: https://uk.trustpilot.com/review/andytough.com

                        email: [email protected] | website: https://andytough.com