We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 37357
    • 6 Posts
    I using this snippet for creat new post, when i login as admin it work perfect! But when i am login as user i have error: "Fatal error: Call to a member function getOption() on a non-object in /web/jcar/public_html/www/core/model/modx/modresource.class.php on line 681"

    <?php
    error_reporting(E_ALL | E_STRICT);
    ini_set('display_errors', 1);
    
    if($_POST['model']){
    
    
    $holder = $modx->getObject('modResource',array('pagetitle' =>$_POST['model']));
    $id = $holder->get('id');
    $name = $_POST['name'].' "'.$_POST['model'].'" '.'('.date('h-i-s').')';
    $resource = $modx->newObject('modDocument', 
    array('pagetitle'=>$name,
    	  'introtext'=>$_POST['text'],
    	  'content'=>$_POST['text'],
    	  'template'=>1,
              'parent'=>$id,
              'published'=>1,
              'alias'=>'index'
    
    ));
    
    
    $resource->save();
    
    $holder = $modx->getObject('modResource',array('pagetitle' =>$name));
    $id = $holder->get('id');
    
    $tv = $modx->getObject('modTemplateVar',array('name'=>'otzivc'));
     
    $tv->setValue($id,'otziv');
    $tv->save();
    
    $modx->cacheManager->clearCache();
    
    
    }
    
    


    Any help or ideas is greatly appreciated, and I thank you in advance. [ed. note: eap1985 last edited this post 14 years, 11 months ago.]
      • 22303 MODX Staff
      • 10,725 Posts
      Is this in 2.1.3?
        • 37357
        • 6 Posts
        Yes - MODX Revolution 2.1.3-pl (traditional)
          • 22303 MODX Staff
          • 10,725 Posts
          First, make sure the $resource is getting save()'d. If not, perhaps you are not setting some important piece of data.
            • 37357
            • 6 Posts
            This simply code don`t save new post too, when i loggin, but when i logout it save new post! I don`t know what i do wrong? And what is "some important piece of data" or where can i find right piece of data?

            <?php
            /*
             * MODx Revolution
             *
             * Copyright 2006-2010 by the MODx Team.
             * All rights reserved.
             *
             * This program is free software; you can redistribute it and/or modify it under
             * the terms of the GNU General Public License as published by the Free Software
             * Foundation; either version 2 of the License, or (at your option) any later
             * version.
             *
             * This program is distributed in the hope that it will be useful, but WITHOUT
             * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
             * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
             * details.
             *
             * You should have received a copy of the GNU General Public License along with
             * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
             * Place, Suite 330, Boston, MA 02111-1307 USA
             *
             */
            $mtime= microtime();
            $mtime= explode(" ", $mtime);
            $mtime= $mtime[1] + $mtime[0];
            $tstart= $mtime;
            
            error_reporting(E_ALL & ~E_NOTICE);
            
            /**
             * @deprecated 2.0.0
             * For backward compatibility with MODx 0.9.x
             */
            define("IN_PARSER_MODE", "true");
            /**
             * @deprecated 2.0.0
             * For backward compatibility with MODx 0.9.x
             */
            define("IN_MANAGER_MODE", false);
            
            /* define this as true in another entry file, then include this file to simply access the API
             * without executing the MODx request handler */
            if (!defined('MODX_API_MODE')) {
                define('MODX_API_MODE', false);
            }
            
            /* this can be used to disable caching in MODx absolutely */
            $modx_cache_disabled= false;
            
            /* include custom core config and define core path */
            @include('config.core.php');
            if (!defined('MODX_CORE_PATH')) define('MODX_CORE_PATH', $_SEVER['DOCUMENT_ROOT'].'/core/');
            
            /* include the modX class */
            if (!@include_once (MODX_CORE_PATH . "model/modx/modx.class.php")) {
                $errorMessage = 'Site temporarily unavailable';
                @include(MODX_CORE_PATH . 'error/unavailable.include.php');
                header('HTTP/1.1 503 Service Unavailable');
                echo "<html><title>Error 503: Site temporarily unavailable</title><body><h1>Error 503</h1><p>{$errorMessage}</p></body></html>";
                exit();
            }
            
            /* start output buffering */
            ob_start();
            
            /* Create an instance of the modX class */
            if (empty($options) || !is_array($options)) $options = array();
            $modx= new modX('', $options);
            if (!is_object($modx) || !($modx instanceof modX)) {
                @ob_end_flush();
                $errorMessage = '<li><a href="setup/">MODx not installed. Install now?</a>';
                @include(MODX_CORE_PATH . 'error/unavailable.include.php');
                header('HTTP/1.1 503 Service Unavailable');
                echo "<html><title>Error 503: Site temporarily unavailable</title><body><h1>Error 503</h1><p>{$errorMessage}</p></body></html>";
                exit();
            }
            header('Content-Type: text/html; charset=utf-8');
            /* Set the actual start time */
            $modx->startTime= $tstart;
            
            /* Set additional logging options including level and tarGET: */
            $modx->setLogLevel(modX::LOG_LEVEL_ERROR);
            $modx->setLogTarGET('FILE');
            
            /* Set debugging mode (i.e. error_reporting): */
            $modx->setDebug(E_ALL & ~E_NOTICE);
            
            /* Initialize the default 'web' context */
            $modx->initialize('web');
                                $resource= $modx->newObject('modDocument');
                                $resource->set('context_key', 'web');
                                $resource->set('content_type', 1);
                                $resource->set('pagetitle', 'New doc');
                                $resource->set('parent', 1);
                                $resource->set('isfolder', false);
             
            
             
                                $resource->set('alias', 'newalias');
                                $resource->set('published', true);
                                $resource->set('template', $modx->getOption('default_template'));
                                $resource->set('menuindex', $modx->getCount('modResource', array ('parent' => 1)));
                                $resource->set('searchable', $modx->getOption('search_default'));
                                $resource->set('cacheable', $modx->getOption('cache_default'));
                                $resource->set('content', 'New doc');
             
                                if (!$resource->save()) {
                                    $results .= $modx->lexicon('import_site_failed') . "Could not import resource from {$filepath}/{$filename}: <br />" . nl2br(print_r($modx->errorInfo(), true));
                                } else {
                                    $results .= $modx->lexicon('import_site_success') . "<br />";
                                }
            
            
            
            
            
            
            ?>
            
            
              • 37357
              • 6 Posts
              Please can someone tell me if I need to call some file or what is the problem? Maybe it's not my fault, and this is error modx system? Thanks!
                • 22303 MODX Staff
                • 10,725 Posts
                Sounds like you have setup security incorrectly if it works when you logout. Make sure you have not given anonymous users an Administrator policy for the web Context.
                  • 37357
                  • 6 Posts
                  Thank you Jason! It work after i added user to new "user group" with role "Member" and had set context this user group as "web". Now in Login.Register snippet i use "usergroups" property to user registration.