We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 1634
    • 51 Posts
    Hi All,

    I’m having a problem with makeUrl.

    I’m using this class (docmanager):
    http://modxcms.com/Docmanager-881.html

    ... to create a document then later in my code calling makeUrl().  makeUrl just returns the id that I pass to it.

    Before I call makeUrl I call $modx->getDocument to make sure the document exists so it doesn’t appear to have a problem with a non existent document.

    So hear is some pseudo code:

    
    $doc = new Document();
    $doc->Set('parent',10);
    $doc->Set('template','MyTemplate');
    $doc->Set('pagetitle','My Title');
    $doc->Set('alias','my-title');
    $doc->Set('published',1);
    $doc->Set('cacheable',0);
    $doc->Save();
    $id = $doc->fields['id'];
    
    $mDoc = $modx->getDocument($id, '*', 1);
    
    if (empty($mDoc)) {
      return 0;
    } else {
      return $modx->makeUrl($id);
    }
    


    So the above code returns the id of the created doc rather that the friendly url (i have friendly urls turned on).  I feel like something about the environment needs resetting after i create the new doc but I have no idea what that might be.

    I’m on 0.9.6.2

    Any ideas anyone? Thanks in advance.
      Hmmmmm.

      It's all a delusion
      • 1634
      • 51 Posts
      ... actually, it doesn’t just return the id, it returns the base_url + id
        Hmmmmm.

        It's all a delusion
        • 10449
        • 956 Posts
        How are you doing this exactly? And from where? If you place this in a .php file in assets/snippets/ chances are this path gets added to the SEO-friendly URL. But otherwise it should work fine. If you activated mod_rewrite, you’ll get SEO-friendly URLs too.

        Try this:

        
        include_once('modxapi.php'); // adjust path if necessary
        $modx = new MODxAPI();
        $modx->connect();
        $modx->getSettings();
        
        $url1 = $modx->makeUrl(1, '', '', 'full');
        $url2 = $modx->makeUrl(1);
        echo "$url2 <br /> $url2";
        exit();
        
        
          • 1634
          • 51 Posts
          Hi Ganesh,

          Thanks for your reply.

          Sorry, I should have said, I’m doing this inside a snippet, not in a php file. Its actually the server side of some Ajax functionality that eventually returns JSON. One of the things that it does is return a link to a just created document (like in my example).

          I’m actually building a ’scrapbook system’. There are loads of images on a site and users can add them to a scrapbook. It all works with AJAX. A scrapbook is a document in the modx tree. The items in the scrapbook are stored as a comma separated list of doc ids in a tv.

          The first time a user clicks ’Add to scrapbook’ the id is sent to the server, the server checks if a scrapbook is already live (as in a doc has been created). If there is a live one it just modifies the list stored in the tv. If there isn’t a live one a doc is created and some info about whats just happened (including the link) is returned. I then use JS to update the UI including a ’view my scrapbook’ link, thats the bit that I need the link for. There’s loads of other functionality in this system and it’s all working sweetly, am loving ’ajaz’ coding and the JSON format with jQuery. This is the ONLY issue I’m having.

          So another strange thing about this is (I’m finding it hard to make this seem understandable)....

          I get the link when I create the doc... and it doesn’t work as per my first post. When a page loads it checks, through the same ajax system, to see if there is a scrapbook already created (I store the doc id in a session variable/cookie). If there is it gets the link, executing the same code in the same way. Sometimes this works, and sometimes it doesn’t, in the same way I describe in my first post but without the doc creation bit. So here’s an example of what I mean:

          1. Click add to scrapbook.
          -Ajax passes the id up to the server, server finds that there IS NO scrapbook, creates one and returns (including link in error)

          2. Refresh page
          -Ajax passes the id upto the server, server finds that there IS a scrapbook, gathers some info returns (including link in error)

          3. Go into Manager, find correct doc by link, open it and save it

          4. Refresh page again
          -Same as step 2 but this time when it returns the link is correct

          So what this seems to say is that if I create a doc in code, something goes with the modx object where despite being able to find a doc by ID with $modx->getDocument it cant make a url with $modx->makeUrl..... unless I save the doc in the manager. I’ve been looking at the source files for the manager and I can see that is missing is event invocation.

          So, looking at the source for makeUrl:

                      $alias= $id;
                      if ($this->config['friendly_alias_urls'] == 1) {
                          $al= $this->aliasListing[$id];
                          $alPath= !empty ($al['path']) ? $al['path'] . '/' : '';
                          if ($al && $al['alias'])
                              $alias= $al['alias'];
                      }
          
          


          Line 1549 is this:
          $alias= $id;

          So it looks to me that $alias never gets modified. I think this test: !empty ($al[’path’]) ... is failing, but I have no idea why.

          Sorry about the long post, wanted to try to explain everything.
            Hmmmmm.

            It&#39;s all a delusion
            • 22303 MODX Staff
            • 10,725 Posts
            The cache has to be rebuilt when a new document is added. See the NewsPublisher snippet for an example of how this is done.
              • 1634
              • 51 Posts
              Thanks opengeek, I’ll take a look. I have been trying a few things. I added this function to the docmanager class and now call it after save:

              function clearCache() {
                  global $modx;  
                  include_once $modx->config['base_path']."manager/processors/cache_sync.class.processor.php";
                  $sync = new synccache();
                  $sync->setCachepath($modx->config['base_path'].'assets/cache/');
                  $sync->setReport(false);
                  $sync->emptyCache();
              }
              


              Do you think this should work? I’ll be looking into the NewsPublisher snippet.
                Hmmmmm.

                It&#39;s all a delusion
                • 10449
                • 956 Posts
                Apparently, there’s an API function for this: http://wiki.modxcms.com/index.php/API:clearCache
                  • 1634
                  • 51 Posts
                  Hi,

                  Ok, so, now it works if I refresh the page so I’m guessing that rebuilding the cache doesn’t do anything for the modx object for this request but its ready for the next one. I think I’ve got a work around I can go with now so thanks. It would be nice to know if there is a way to rebuild the cache and have newly created docs available to the modx object in the same code block.

                  @ganeshXl: Yea, i tried that, didn’t do anything.

                  Cheers,
                  Andrew
                    Hmmmmm.

                    It&#39;s all a delusion
                    • 22303 MODX Staff
                    • 10,725 Posts
                    Quote from: ganeshXL at Dec 03, 2008, 02:11 PM

                    Apparently, there’s an API function for this: http://wiki.modxcms.com/index.php/API:clearCache
                    That only clears a specific existing pages cache file. It is also gone in Revolution, i.e. deprecated.

                    Quote from: andyflan at Dec 03, 2008, 02:16 PM

                    Ok, so, now it works if I refresh the page so I’m guessing that rebuilding the cache doesn’t do anything for the modx object for this request but its ready for the next one. I think I’ve got a work around I can go with now so thanks. It would be nice to know if there is a way to rebuild the cache and have newly created docs available to the modx object in the same code block.
                    Correct, you would need to reload the entire siteCache file again in that request, and there currently is no way to do this easily. Best bet is to redirect to an intermediate location after rebuilding the cache.
                      • 1634
                      • 51 Posts
                      Yea, that’s kinda where I am, cheers opengeek wink
                        Hmmmmm.

                        It&#39;s all a delusion