O
O
Oleg2018-11-20 21:24:22
MODX
Oleg, 2018-11-20 21:24:22

Why does changing the system settings not from under the admin work?

I have a system setting (number). I want this number to increase by 1 on the site when the button is clicked. I created a snippet with the following content:

<?php
$settingName = 'quiz_counter';
$currentCount = $modx->getOption($settingName);
$currentCount++;
  
$response = $modx->runProcessor(
  'system/settings/update',
  array(
    'key' => $settingName,
    'value' => $currentCount,
    'namespace' => 'core'
  )
);
  
$modx->reloadConfig();


I created a page with an empty template and called a snippet on it:



And I hung a handler on a button click. On click, an AJAX request is made to this page:

$('.btn').on('click', function() {

  $.ajax({
    url: '/system/quiz-counter-increment/',
    data: ''
  })
});


Everything works for me, but only from under the administrator. Doesn't work in incognito mode or just logged out. Why? I completely deleted the contents of the core / cache folder, pressed Ctrl + F5 - it does not help.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Lunegov, 2018-11-21
@r45her

Good afternoon!
Try my snippet. We get and change the system setting as a MODX object.

$settingName = 'quiz_counter';
$currentCountArray = $modx->getObject('modSystemSetting', $settingName);
$currentCount = $currentCountArray->get('value');
$currentCount++;
$currentCountArray->set('value', $currentCount);
$currentCountArray->set('editedon', date("Y-m-d H:i:s"));  // Не обязательно, но коли есть такое поле, почему не использовать?
$currentCountArray->save();
$cacheRefreshOptions =  array( 'system_settings' => array() );
$modx->cacheManager-> refresh($cacheRefreshOptions);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question