public static function executeScript(SiteFile $_SCRIPT_NODE, $_SCRIPT_EXIT = true) { // create session if (empty($GLOBALS['Session']) && static::$autoCreateSession && !in_array(implode('/', static::$resolvedPath), static::$skipSessionPaths)) { $GLOBALS['Session'] = UserSession::getFromRequest(); } if (extension_loaded('newrelic')) { if (!empty($GLOBALS['Session'])) { newrelic_add_custom_parameter('session_id', $GLOBALS['Session']->Handle); newrelic_add_custom_parameter('person_id', $GLOBALS['Session']->PersonID); } newrelic_add_custom_parameter('script_path', $_SCRIPT_NODE->FullPath); } if (is_callable(static::$onBeforeScriptExecute)) { call_user_func(static::$onBeforeScriptExecute, $_SCRIPT_NODE); } if (class_exists('Emergence\\EventBus')) { Emergence\EventBus::fireEvent('beforeScriptExecute', 'Site', array('node' => $_SCRIPT_NODE, 'exit' => $_SCRIPT_EXIT)); } require $_SCRIPT_NODE->RealPath; if ($_SCRIPT_EXIT) { exit; } }
public function save($deep = true) { // fire event Emergence\EventBus::fireEvent('beforeRecordSave', $this->getRootClass(), array('Record' => $this, 'deep' => $deep)); // set creator if (static::_fieldExists('CreatorID') && !$this->CreatorID) { $Creator = $this->getUserFromEnvironment(); $this->CreatorID = $Creator ? $Creator->ID : null; } // set created if (static::_fieldExists('Created') && (!$this->Created || $this->Created == 'CURRENT_TIMESTAMP')) { $this->Created = time(); } // validate if (!$this->validate($deep)) { throw new RecordValidationException($this, 'Cannot save invalid record'); } // clear caches foreach ($this->getClassFields() as $field => $options) { if (!empty($options['unique']) || !empty($options['primary'])) { $key = sprintf('%s/%s:%s', static::$tableName, $field, $this->getValue($field)); DB::clearCachedRecord($key); } } // traverse relationships if ($deep) { $this->_saveRelationships(); } if ($this->isDirty) { if (!$this->_isPhantom && static::$trackModified) { $this->Modified = time(); $Modifier = $this->getUserFromEnvironment(); $this->ModifierID = $Modifier ? $Modifier->ID : null; } // prepare record values $recordValues = $this->_prepareRecordValues(); // transform record to set array $set = static::_mapValuesToSet($recordValues); // create new or update existing if ($this->_isPhantom) { $insertQuery = DB::prepareQuery('INSERT INTO `%s` SET %s', array(static::$tableName, join(',', $set))); try { try { DB::nonQuery($insertQuery); } catch (TableNotFoundException $e) { // auto-create table and try insert again DB::multiQuery(SQL::getCreateTable(get_called_class())); DB::nonQuery($insertQuery); } $this->_record['ID'] = DB::insertID(); $this->_isPhantom = false; $this->_isNew = true; } catch (DuplicateKeyException $e) { if (static::$updateOnDuplicateKey && preg_match('/Duplicate entry \'.*?\' for key \'([^\']+)\'/', $e->getMessage(), $errorMatches) && ($duplicateKeyName = $errorMatches[1]) && ($duplicateKeyName == 'PRIMARY' || ($duplicateKeyConfig = static::getStackedConfig('indexes', $duplicateKeyName)))) { if (!empty($duplicateKeyConfig)) { $keyFields = $duplicateKeyConfig['fields']; } else { $keyFields = array(); foreach (static::getClassFields() as $fieldName => $fieldConfig) { if (!empty($fieldConfig['primary'])) { $keyFields[] = $fieldName; } } } $keyValues = array_intersect_key($recordValues, array_flip($keyFields)); $deltaValues = array_diff_key($recordValues, array_flip(array('Created', 'CreatorID'))); DB::nonQuery('UPDATE `%s` SET %s WHERE %s', array(static::$tableName, join(',', static::_mapValuesToSet($deltaValues)), join(' AND ', static::_mapConditions($keyValues)))); $this->_record = static::getRecordByWhere($keyValues); $this->_isPhantom = false; $this->_isUpdated = true; } else { throw $e; } } } elseif (count($set)) { DB::nonQuery('UPDATE `%s` SET %s WHERE `%s` = %u', array(static::$tableName, join(',', $set), static::_cn('ID'), $this->ID)); $this->_isUpdated = true; } // clear cache static::_invalidateRecordCaches($this->ID); // update state $this->_isDirty = false; } // traverse relationships again if ($deep) { $this->_postSaveRelationships(); } // fire event Emergence\EventBus::fireEvent('afterRecordSave', $this->getRootClass(), array('Record' => $this, 'deep' => $deep)); }
public static function executeScript(SiteFile $_SCRIPT_NODE, $_SCRIPT_EXIT = true) { // create session if (empty($GLOBALS['Session']) && static::$autoCreateSession && !in_array(implode('/', static::$resolvedPath), static::$skipSessionPaths)) { $GLOBALS['Session'] = UserSession::getFromRequest(); } if (is_callable(static::$onBeforeScriptExecute)) { call_user_func(static::$onBeforeScriptExecute, $_SCRIPT_NODE); } if (class_exists('Emergence\\EventBus')) { Emergence\EventBus::fireEvent('beforeScriptExecute', 'Site', array('node' => $_SCRIPT_NODE, 'exit' => $_SCRIPT_EXIT)); } require $_SCRIPT_NODE->RealPath; if ($_SCRIPT_EXIT) { exit; } }