We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 39535
    • 39 Posts
    I've created a CMP following this official tutorial: http://rtfm.modx.com/revolution/2.x/developing-in-modx/advanced-development/custom-manager-pages

    On this CMP I have a button that I want to perform AJAX requests (on click).

    What is the proper way to create php-file that will process this AJAX requests?

    Either by creating php-file in /assets/components/my_component_name/ and populating it with this code at the top:
    require_once '/absolute/path/to/modx/config.core.php';
    require_once MODX_CORE_PATH.'model/modx/modx.class.php';
    $modx = new modX();
    $modx->initialize('web');
    // My code is here

    or by creating another one action in System->Actions menu in manager and doing something there
    or in another way (by, maybe, using connectors, processors, controllers...)?
      • 3749
      • 24,544 Posts
      You might find this helpful: http://bobsguides.com/blog.html/2014/03/28/modx-2.2-cmps-an-anatomy-lesson/.

      You might also take a look at MyComponent. It comes with an example project that creates a CMP (among other things). You can copy it or just look at it's code to follow along with the article in the link above.

      I think there is also a MIGx way of creating CMPs, though I haven't used it.
        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
        • 39535
        • 39 Posts
        Quote from: BobRay at Nov 20, 2014, 03:18 AM
        You might find this helpful: http://bobsguides.com/blog.html/2014/03/28/modx-2.2-cmps-an-anatomy-lesson/.

        You might also take a look at MyComponent. It comes with an example project that creates a CMP (among other things). You can copy it or just look at it's code to follow along with the article in the link above.

        I think there is also a MIGx way of creating CMPs, though I haven't used it.

        Thank you for article, I think I solver my problem in a proper way:
        So, I created my custom connector and processor.
        From my CMP, when user presses "Save" button, it sends AJAX POST request to connector.php, that is placed in /assets/components/my_component_name/ with 'action' parameter and other parameters that I need.
        Here is the code of my connector.php:

        <?php
        require_once dirname(dirname(dirname(dirname(__FILE__)))).'/config.core.php';
        require_once MODX_CORE_PATH.'config/'.MODX_CONFIG_KEY.'.inc.php';
        require_once MODX_CONNECTORS_PATH.'index.php';
        
        $corePath = $modx->getOption('my_component_name.core_path',null,$modx->getOption('core_path').'components/my_component_name/');
        require_once $corePath.'model/my_component_name.class.php';
        $modx->my_component_className= new my_component_className($modx);
        
        /* handle request */
        $path = $modx->getOption('processorsPath',$modx->my_component_className->config,$corePath.'processors/');
        $modx->request->handleRequest(array(
            'processors_path' => $path,
            'location' => ''
        ));


        The action parameter is a path to processor relative to path is set in 'processors_path' in $modx->request->handleRequest.
        I set action parameter to 'mgr/saveorder' as my processor file is there /core/components/my_component_name/processors/mgr/saveorder.class.php

        By the way, as I discovered name of processor MUST be lower-case.

        Here is the code of saveorder.class.php:
        <?php
        class SortArticlesSaveOrderProcessor extends modProcessor {
            public function initialize() {
        	return parent::initialize();
            }
            public function process() {
            	$sortOrder = json_decode($this->getProperty('order'));
        		
        		foreach($sortOrder as $order => $id) {			
        			$page = $this->modx->getObject('modResource', $id);
        			if (!$page->setTVValue('sort_id', $order)) {
        			    $this->modx->log(xPDO::LOG_LEVEL_ERROR, 'There was a problem saving sort_id in saveorder.class.php');
        				return 'Ошибка.';
        			}			 
        		}
        		
        		return 'Изменения успешно сохранены.';
            }
        }
        return 'SortArticlesSaveOrderProcessor';
          • 36467
          • 73 Posts
          Have a look at this simple and best tutorial, i managed to make my way through this tut http://ridcully.dunnock.modxcloud.com/records/2014/07/08/using-custom-processors-in-modx-revolution-over-ajax-request/