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

    Based on this page http://rtfm.modx.com/display/revolution20/Adding+Custom+Fields+to+Manager+Forms i have setup a new field in my modx db. I altered the modx_site_content table to add a new field called shorturl, my goal is to store a short the url of my document.

    I have a strange issue, if i change :

        case 'OnDocFormSave':
            /* do processing logic here. */
            $resource =& $scriptProperties['resource'];
            $resource->set('description',$_POST['shorturl']);
            $resource->save();
            break;
    }
    return;

    to this
        case 'OnDocFormSave':
            /* do processing logic here. */
            $resource =& $scriptProperties['resource'];
            $resource->set('shorturl',$_POST['shorturl']);
            $resource->save();
            break;
    }
    return;


    I have nothing in my shorturl db field. Am I missing something ?
    Do I need to declare my new field somewhere ?

    Regards

    Frost
      Configuration Apache + Modx + MSSQL 2008
      ===============================
      Apache/2.4.3 (Win32) OpenSSL/1.0.1c PHP/5.4.8
      OS : Windows 2008 R2
      SGBDR : Microsoft SQL Server 2008 (express)
      • 3749
      • 24,544 Posts
      You should never, ever alter existing MODx DB tables. Make that a template variable instead.
        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
        • 34123
        • 103 Posts
        Thanks for your answer,

        Thanks for your advice, i should store my data into a tv... I need to search how to do that

        What is the best way to create a plugin, is there any best practice or any gold rules about this ?

        Frost
          Configuration Apache + Modx + MSSQL 2008
          ===============================
          Apache/2.4.3 (Win32) OpenSSL/1.0.1c PHP/5.4.8
          OS : Windows 2008 R2
          SGBDR : Microsoft SQL Server 2008 (express)
          • 3749
          • 24,544 Posts
          If you create a TV for it, I don’t think you’ll need a plugin at all.

          If you call it shortUrl, you can access it with a MODx tag in any resource or chunk:

          [[*shortUrl]] 

            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
            • 34123
            • 103 Posts
            Yeap thanks to your reply i have a better view to this task.

            I can create a TV with a binding to a snippet (here is the solution : http://modxcms.com/forums/index.php/topic,14503.0.html). Then my TV will be populate. I have to generate my shorturl TV only if my document is published. I think it will be possible.

            My first goal is to obtain the full url of a document (domain + path to my document) and store it into my tv. Then my second attempt will be to convert the full url into a shorten version.

            What do you think about this BobRay ?
              Configuration Apache + Modx + MSSQL 2008
              ===============================
              Apache/2.4.3 (Win32) OpenSSL/1.0.1c PHP/5.4.8
              OS : Windows 2008 R2
              SGBDR : Microsoft SQL Server 2008 (express)
              • 3749
              • 24,544 Posts
              I can’t really answer because I don’t know what your short URLs will look like or what you intend to do with them. I make it a rule never to mess with URLs, I just take whatever I get.

              Have you tried turning the system setting for Friendly Alias *paths* off? That will give you a short URL.
                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
                • 34123
                • 103 Posts
                There is no issue with url (at all sure !!)

                In my case, i want to build a snippet which permits me to generate automatically (through a service like bit.ly or goo.gl or any service) from a given resource to a shorten url based on the full url : now with code i am able to rebuild the whole url (even if my resource is inside a folder, which is inside another folder).

                My main issue now with my code is to retrieve the content_type : I am able to retrieve the id of the content_type now I need to have the description (like .html for id 1, .css for id 2 etc. etc.).

                $docInfo = $modx->getObject('modResource',18);
                $docname = $docInfo->get('alias');
                $doctypeid = $docInfo->get('content_type');


                Now in my variable $doctypeid I have the id, I am searching how to convert into the right Mime type.

                Frost
                  Configuration Apache + Modx + MSSQL 2008
                  ===============================
                  Apache/2.4.3 (Win32) OpenSSL/1.0.1c PHP/5.4.8
                  OS : Windows 2008 R2
                  SGBDR : Microsoft SQL Server 2008 (express)
                  • 3749
                  • 24,544 Posts
                  Try this:

                  <?php
                  $resource = $modx->getObject('modResource',18);
                  
                  $contentType = $resource->getOne('ContentType');
                  
                  $description = $contentType->get('description');
                  $mimeType = $contentType->get('mime-type');
                  


                  One of those should be what you want.
                    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
                    • 34123
                    • 103 Posts
                    Argh,

                    I have no result with the $contentType and $mimeType.

                    Hard to retrieve file extension based on the mime type or content type !

                    I will continue to search

                    Edit 1 : Good News, I am now able to generate short URL : http://bit.ly/eR7Y3G (it’s a demo url). But it’s incomplete, I need the document extension.

                    Here is my code, I think it needs some tweaking and security checks

                    <?php
                    /* Retrieve properties from DB */
                    
                    $Bitly_KEY = $modx->getOption('Bitly_KEY', $scriptProperties, '');
                    $Bitly_LOGIN = $modx->getOption('Bitly_LOGIN', $scriptProperties, '');
                    $siteURL = $modx->getOption('site_url');
                    $docInfo = $modx->getObject('modResource',18);
                    $docname = $docInfo->get('alias');
                    
                    //damn I am unable to retrieve file extension
                    
                    /* Check if it is in folder */
                    $parent = $docInfo->get('parent');
                    $path='';
                    
                    /* Checks if everything is correct */
                    
                    if (empty($Bitly_KEY)){
                    	$output="-No Bit.ly key, please create an account @ bit.ly website";
                    	return $output;
                    	die();
                    	}
                    	else
                    	{
                    		$output=$Bitly_KEY;
                    		}
                    
                    if (empty($Bitly_LOGIN)){
                    	$output="-No Bit.ly key, please create an account @ bit.ly website";
                    	return $output;
                    	die();
                    	}
                    	else
                    	{
                    		$output=$Bitly_LOGIN;
                    		}
                    
                    //loop through parents
                    while ($parent != 0) {
                    	$docInfo = $modx->getObject('modResource',$parent);
                    	$alias = $docInfo->get('alias');
                    	$path = $alias . '/' . $path;
                    	$parent = $docInfo->get('parent');
                    	}
                    
                    $longurl = $siteURL . $path . $docname . $extension;
                    $bitly = 'http://api.bit.ly/shorten?version=2.0.1&longUrl='.urlencode($longurl).'&login='.$Bitly_LOGIN.'&apiKey='.$Bitly_KEY.'&format=xml';
                    $response = file_get_contents($bitly);
                    //credits to davidwalsh.name
                    //parse depending on desired format
                    if(strtolower('xml') == 'json'){
                    	$json = @json_decode($response,true);
                    	return $json['results'][$longurl]['shortUrl'];
                    	}
                    	else //xml
                    	{
                    		$xml = simplexml_load_string($response);
                    		return 'http://bit.ly/'.$xml->results->nodeKeyVal->hash;
                    	}
                      Configuration Apache + Modx + MSSQL 2008
                      ===============================
                      Apache/2.4.3 (Win32) OpenSSL/1.0.1c PHP/5.4.8
                      OS : Windows 2008 R2
                      SGBDR : Microsoft SQL Server 2008 (express)
                      • 3749
                      • 24,544 Posts
                      Sorry, I should have looked in the DB:

                      <?php
                      $resource = $modx->getObject('modResource',18);
                      
                      $contentType = $resource->getOne('ContentType');
                      
                      $extension = $contentType->get('file_extensions');

                        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