/** Add value to counter. If counter not exists set counter to value. Save to database. * @param Date $day - counter date * @param string $name - counter name * @param int|float $value - number to add * @throws ArgumentTypeException * @throws SystemException */ public function addCounter(Date $day, $name, $value) { if (!is_string($name)) { throw new ArgumentTypeException('name', 'string'); } if (!is_numeric($value)) { throw new ArgumentTypeException('value', 'numeric'); } if (($id = $this->id) === null) { throw new SystemException('Cannot add counter without context!'); } static $types; if (!$types) { $types = CounterManager::getTypes(); } if (!($type = $types[$name])) { throw new SystemException('Undefined counter type!'); } if (!$type['ACTIVE']) { return; } // save to database $primary = array('DAY' => $day, 'CONTEXT_ID' => $id, 'NAME' => $name); $data = array('VALUE' => new DB\SqlExpression('?# + ?f', 'VALUE', $value)); $result = ContextCounterDayTable::update($primary, $data); if ($result->getAffectedRowsCount() === 0) { try { $result = ContextCounterDayTable::add($primary + array('VALUE' => $value)); } catch (DB\SqlQueryException $e) { $result = ContextCounterDayTable::update($primary, $data); } } $result->isSuccess(); // TODO isSuccess }
public static function generateInitialData(Date $from) { if (($to = Option::get('conversion', 'START_DATE_TIME', 'undefined')) != 'undefined' && DateTime::isCorrect($to, 'Y-m-d H:i:s') && ($to = new DateTime($to, 'Y-m-d H:i:s')) && $to->format('Y-m-d H:i:s') > $from->format('Y-m-d H:i:s') && Option::get('conversion', 'GENERATE_INITIAL_DATA', 'undefined') == 'undefined') { Option::set('conversion', 'GENERATE_INITIAL_DATA', 'generated'); $context = new self(); // generate data $data = array(); foreach (EventManager::getInstance()->findEventHandlers('conversion', 'OnGenerateInitialData') as $handler) { $result = ExecuteModuleEventEx($handler, array($from, $to)); // TODO validate foreach ($result as $row) { $context->id = null; $context->attributes = array(); $context->setAttributes($row['ATTRIBUTES']); $context->save(); if ($dayCounters =& $data[$context->id]) { self::appendDayCounters($dayCounters, $row['DAY_COUNTERS']); } else { $dayCounters = $row['DAY_COUNTERS']; } } } unset($dayCounters); // save data to database $numerators = CounterManager::getTypes(array('GROUP' => 'day')); unset($numerators['conversion_visit_day']); foreach ($data as $id => $dayCounters) { $context->id = $id; foreach ($dayCounters as $day => $counters) { $day = new Date($day, 'Y-m-d'); $visitSum = 0; $visitQuantity = 0; unset($counters['conversion_visit_day']); foreach ($counters as $name => $value) { $context->addCounter($day, $name, $value); if ($numerators[$name]) { $visitSum += $value; $visitQuantity += 1; } } $context->addCounter($day, 'conversion_visit_day', $visitQuantity ? round($visitSum / $visitQuantity * 100) + 1 : 1); } } } }
public static function getModules() { if (!($modules =& self::$modules)) { $default = array('ACTIVE' => !ModuleManager::isModuleInstalled('sale')); foreach (array(AttributeManager::getTypesInternal(), CounterManager::getTypesInternal(), RateManager::getTypesInternal()) as $types) { foreach ($types as $type) { $modules[$type['MODULE']] = $default; } } if ($modules['sale']) { $modules['sale']['ACTIVE'] = true; } $modules = unserialize(Option::get('conversion', 'MODULES', 'a:0:{}')) + $modules; // TODO all modules with attributes must be active $modules['conversion'] = $modules['abtest'] = $modules['sender'] = $modules['seo'] = array('ACTIVE' => true); ksort($modules); } return $modules; }