Welcome Guest, Not a member yet? Register   Sign In
Config updates via jQuery
#6

[eluser]Narkboy[/eluser]
Ok. Well - I have a sort-of solution.

If I create a new function:
Code:
function set_status(status) {
            $.ajax( { url: '/admin/cp_ajax/status_change_mode/' + status , dataType: 'html' , cache: false } );
            setTimeout("check_status()" , 1000);
        };

It works. I've checked the changes made by the PHP function and they work immediatly - I can't be certain they take less than a second or two, but it does work for the next page call.

WTF?

The php function that gets called is:
Code:
function update_site_mode($to) {

        // Get the current mode:
        $mode = $this->config->item('site_mode');

        // Make sure that the $to is acceptable:
        if ( $to != 'online' && $to != 'maintenance' && $to != 'testing' ) {
            // Requested mode is not acceptable:
            log_message('error' , 'CT_Local::update_site_mode - bad $to recieved ('.$to.').');

            // Add to the audit trail:
            $this->log_mode_update('Update', $mode, $to, 'Fail - bad mode recieved');

            return FALSE;
        }

        // Hardcode the file:
        $filename = '../htapp/application/config/tgt_config.php';

        if( ! is_really_writable($filename)) {
            // Log an error:
            log_message('error' , 'CT_Local::update_config could not write to the config file.');

            // Add to the audit trail:
            $this->log_mode_update('Update', $mode, $to, 'Fail - config file not writable');
            return FALSE;
        }

        $fh = fopen( $filename , 'r+' );

        if ( ! $fh ) {
            // Log an error:
            log_message('error' , 'CT_Local::update_config could not read the config file.');

            // Add to the audit trail:
            $this->log_mode_update('Update', $mode, $to, 'Fail - could not open the config file');
            return FALSE;
        }

        $contents = fread($fh, filesize($filename));

        // Determine the needle to find from the config:
        $needle = "['site_mode']='$mode';";

        // Set the new version required:
        $new_version ="['site_mode']='$to';";

        $updated = str_replace( $needle , $new_version , $contents);

        // Clear the file:
        ftruncate($fh , 0);
        rewind($fh);

        // Write the new data in:
        fwrite( $fh , trim($updated) );

        // Can we check the new config? Not via $this->config...
        // @todo Way to check the changes were made..

        $this->log_mode_update('Update', $mode, $to, 'Ok');

        // Return:
        return TRUE;
    }

I'm giving up for the night. It's either:

1) a time thing - it takes longer to edit / close the file than the ajax request takes to complete. Weird.
2) a jQuery thing - success requests are sent before they should be. Not really possible.
3) Something else.

Bah.


Messages In This Thread
Config updates via jQuery - by El Forum - 10-29-2010, 10:14 AM
Config updates via jQuery - by El Forum - 10-29-2010, 12:26 PM
Config updates via jQuery - by El Forum - 11-09-2010, 02:31 AM
Config updates via jQuery - by El Forum - 11-23-2010, 11:39 AM
Config updates via jQuery - by El Forum - 11-23-2010, 11:50 AM
Config updates via jQuery - by El Forum - 11-23-2010, 12:18 PM
Config updates via jQuery - by El Forum - 11-23-2010, 03:05 PM
Config updates via jQuery - by El Forum - 11-24-2010, 03:10 AM



Theme © iAndrew 2016 - Forum software by © MyBB