Beispiel #1
0
 public function getRefEntity()
 {
     if ($this->refEntity === null) {
         $this->refEntity = Base::getInstance($this->refEntityName);
     }
     return $this->refEntity;
 }
Beispiel #2
0
 /**
  * @static
  * @return Base
  */
 public static function getEntity()
 {
     $class = get_called_class();
     if (!isset(static::$entity[$class])) {
         static::$entity[$class] = Base::getInstance(get_called_class());
     }
     return static::$entity[$class];
 }
Beispiel #3
0
 /**
  * @param Base|Query|string $source
  * @throws \Exception
  */
 public function __construct($source)
 {
     if ($source instanceof $this) {
         $this->init_entity = Base::getInstanceByQuery($source);
     } elseif ($source instanceof Base) {
         $this->init_entity = $source;
     } elseif (is_string($source)) {
         $this->init_entity = Base::getInstance($source);
     } else {
         throw new \Exception(sprintf('Unknown source type "%s" for new %s', gettype($source), __CLASS__));
     }
 }
Beispiel #4
0
 /**
  * 初始化
  */
 public function __construct($configfile = null)
 {
     global $config;
     if (empty($config) && file_exists($configfile)) {
         require $configfile;
     }
     $this->config = $config;
     $this->Base = Base::getInstance();
     $this->Base->init($this->config);
     self::$app = $this->Base->app();
     $this->loadIniSet();
     $this->loadMemcache();
     $this->loadRedis();
     $this->loadSession();
     $this->loadCookie();
 }
Beispiel #5
0
 public function create($post)
 {
     if (isset($post['obj']) && !empty($post['obj'])) {
         $obj_model = Base::getInstance($post['obj']);
         $obj = $obj_model->find($post['id']);
         if ($obj && $obj['published'] && isset($post['text']) && !empty($post['text'])) {
             $user_model = User::getInstance();
             $user = $user_model->checkUser();
             $isValid = true;
             $comment = $this->mapper;
             $comment->reset();
             if ($user) {
                 $comment->user_id = $user['id'];
             } else {
                 if (isset($post['name']) && !empty($post['name'])) {
                     $comment->name = $post['name'];
                 } else {
                     \helpers\Msg::error('comments.name.empty');
                     $isValid = false;
                 }
             }
             if ($isValid) {
                 $comment->obj_id = $obj['id'];
                 $comment->text = $post['text'];
                 $comment->ip = \Base::instance()->get('IP');
                 $comment->save();
                 \helpers\Msg::success('comments.created');
                 return true;
             }
         }
     }
     if (!$obj) {
         \helpers\Msg::error('comments.obj.empty');
     }
     if (empty($post['text'])) {
         \helpers\Msg::error('comments.text.empty');
     }
     return false;
 }
Beispiel #6
0
 /**
  * Stores template information. This is later used while rendering templates
  *
  * @param string $template
  * @param array  $data
  * @param string $options
  * @return Template
  */
 public function template($template, $data = array(), $options = '')
 {
     $this->template = array($template, $data, $options);
     $template = new Template(Base::getInstance());
     $this->loaderHooks($template, 'pre_template', $class = '', $method_name = '', $data = array());
     return $template;
 }
Beispiel #7
0
 public static function getChainByDefinition(Base $init_entity, $definition)
 {
     $chain = new QueryChain();
     $chain->addElement(new QueryChainElement($init_entity));
     $def_elements = explode('.', $definition);
     $def_elements_size = count($def_elements);
     $prev_entity = $init_entity;
     $i = 0;
     foreach ($def_elements as &$def_element) {
         $is_first_elem = $i == 0;
         $is_last_elem = ++$i == $def_elements_size;
         $not_found = false;
         // all elements should be a Reference field or Entity
         // normal (scalar) field can only be the last element
         if ($prev_entity->hasField($def_element)) {
             // field has been found at current entity
             $field = $prev_entity->getField($def_element);
             if ($field instanceof ReferenceField) {
                 $prev_entity = $field->getRefEntity();
             } elseif ($field instanceof ExpressionField) {
                 // expr can be in the middle too
             } elseif (!$is_last_elem) {
                 throw new \Exception(sprintf('Normal fields can be only the last in chain, `%s` %s is not the last.', $field->getName(), get_class($field)));
             }
             if ($is_last_elem && $field instanceof ExpressionField) {
                 // we should have own copy of build_from_chains to set join aliases there
                 $field = clone $field;
             }
             $chain->addElement(new QueryChainElement($field));
         } elseif ($prev_entity->hasUField($def_element)) {
             // extend chain with utm/uts entity
             $ufield = $prev_entity->getUField($def_element);
             $u_entity = null;
             if ($ufield->isMultiple()) {
                 // add utm entity  user.utm:source_object (1:N)
                 $utm_entity = Base::getInstance($prev_entity->getNamespace() . 'Utm' . $prev_entity->getName());
                 $u_entity = $utm_entity;
                 $chain->addElement(new QueryChainElement(array($utm_entity, $utm_entity->getField('SOURCE_OBJECT')), array('ufield' => $ufield)));
                 if ($ufield->getTypeId() == 'iblock_section' && substr($ufield->getName(), -3) == '_BY' && $prev_entity->hasUField(substr($ufield->getName(), 0, -3))) {
                     // connect next entity
                     $utm_fname = $ufield->getName();
                     $prev_entity = Base::getInstance('Bitrix\\Iblock\\Section');
                 } else {
                     $utm_fname = $ufield->getValueFieldName();
                 }
                 $chain->addElement(new QueryChainElement($utm_entity->getField($utm_fname), array('ufield' => $ufield)));
             } else {
                 // uts table - single value
                 // add uts entity user.uts (1:1)
                 $uts_entity = Base::getInstance($prev_entity->getNamespace() . 'Uts' . $prev_entity->getName());
                 $u_entity = $uts_entity;
                 $chain->addElement(new QueryChainElement($prev_entity->getField('UTS_OBJECT')));
                 // add `value` field
                 $chain->addElement(new QueryChainElement($uts_entity->getField($def_element)));
             }
         } elseif (Base::isExists($def_element) && Base::getInstance($def_element)->getReferencesCountTo($prev_entity->getName()) == 1) {
             // def_element is another entity with only 1 reference to current entity
             // need to identify Reference field
             $ref_entity = Base::getInstance($def_element);
             $field = end($ref_entity->getReferencesTo($prev_entity->getName()));
             $prev_entity = $ref_entity;
             $chain->addElement(new QueryChainElement(array($ref_entity, $field)));
         } elseif (($pos_wh = strpos($def_element, ':')) > 0) {
             $ref_entity_name = substr($def_element, 0, $pos_wh);
             if (strpos($ref_entity_name, '\\') === false) {
                 // if reference has no namespace, then it'is in the namespace of previous entity
                 $ref_entity_name = $prev_entity->getNamespace() . $ref_entity_name;
             }
             if (Base::isExists($ref_entity_name) && Base::getInstance($ref_entity_name)->hasField($ref_field_name = substr($def_element, $pos_wh + 1)) && Base::getInstance($ref_entity_name)->getField($ref_field_name)->getRefEntity()->getName() == $prev_entity->getName()) {
                 // chain element is another entity with >1 references to current entity
                 // def like NewsArticle:AUTHOR, NewsArticle:LAST_COMMENTER
                 // NewsArticle - entity, AUTHOR and LAST_COMMENTER - Reference fields
                 $chain->addElement(new QueryChainElement(array(Base::getInstance($ref_entity_name), Base::getInstance($ref_entity_name)->getField($ref_field_name))));
                 $prev_entity = Base::getInstance($ref_entity_name);
             } else {
                 $not_found = true;
             }
         } elseif ($def_element == '*' && $is_last_elem) {
             continue;
         } else {
             // unknown chain
             $not_found = true;
         }
         if ($not_found) {
             throw new \Exception(sprintf('Unknown field definition `%s` (%s) for %s Entity.', $def_element, $definition, $prev_entity->getName()), 100);
         }
     }
     return $chain;
 }
Beispiel #8
0
 public function postInitialize()
 {
     // базовые свойства
     $classPath = explode('\\', $this->className);
     $this->name = substr(end($classPath), 0, -6);
     // default db table name
     if (is_null($this->dbTableName)) {
         $_classPath = array_slice($classPath, 0, -1);
         $this->dbTableName = 'b_';
         foreach ($_classPath as $i => $_pathElem) {
             if ($i == 0 && $_pathElem == 'Bitrix') {
                 // skip bitrix namespace
                 continue;
             }
             if ($i == 1 && $_pathElem == 'Main') {
                 // also skip Main module
                 continue;
             }
             $this->dbTableName .= strtolower($_pathElem) . '_';
         }
         // add class
         if ($this->name !== end($_classPath)) {
             $this->dbTableName .= Base::camel2snake($this->name);
         } else {
             $this->dbTableName = substr($this->dbTableName, 0, -1);
         }
     }
     $this->primary = array();
     $this->references = array();
     if (empty($this->filePath)) {
         throw new \Exception(sprintf('Parameter `filePath` required for `%s` Entity', $this->name));
     }
     // инициализация атрибутов
     foreach ($this->fieldsMap as $fieldName => &$fieldInfo) {
         $field = $this->initializeField($fieldName, $fieldInfo);
         if ($field instanceof ReferenceField) {
             // references cache
             $this->references[strtolower($fieldInfo['data_type'])][] = $field;
         }
         $this->fields[$fieldName] = $field;
         if ($field instanceof ScalarField && $field->isPrimary()) {
             $this->primary[] = $fieldName;
         }
         // add reference field for UField iblock_section
         if ($field instanceof UField && $field->getTypeId() == 'iblock_section') {
             $refFieldName = $field->GetName() . '_BY';
             if ($field->isMultiple()) {
                 $localFieldName = $field->getValueFieldName();
             } else {
                 $localFieldName = $field->GetName();
             }
             $newFieldInfo = array('data_type' => 'Bitrix\\Iblock\\Section', 'reference' => array($localFieldName, 'ID'));
             $refEntity = Base::getInstance($newFieldInfo['data_type']);
             $newRefField = new ReferenceField($refFieldName, $this, $refEntity, $newFieldInfo['reference'][0], $newFieldInfo['reference'][1]);
             $this->fields[$refFieldName] = $newRefField;
         }
     }
     if (empty($this->primary)) {
         throw new \Exception(sprintf('Primary not found for %s Entity', $this->name));
     }
 }
Beispiel #9
0
/**
 * @return Dispatcher
 */
function &getInstance()
{
    return Base::getInstance();
}
Beispiel #10
0
 /**
  * Creates an instance of the component which is being loaded.
  *
  * @param $namespace
  * @param $class
  * @param $params
  * @param $method_name
  * @param $data
  * @return object
  */
 private function _createInstance($namespace, $class, $params, $method_name, $data)
 {
     $ns_class = $namespace . $class;
     //create a reflection class so that we can pass parameters to the constructor
     $ref_class = new \ReflectionClass($ns_class);
     if (is_array($params) && sizeof($params) > 0) {
         $instance = $ref_class->newInstanceArgs($params);
     } else {
         $instance = new $ns_class();
     }
     $this->logger->info("{$namespace} object created for {$class} class");
     //call the method along with parameters, if they exist !
     if (method_exists($instance, $method_name)) {
         if (is_callable(array($instance, $method_name))) {
             $last_hook = $this->loading[count($this->loading) - 1];
             $this->loaderHooks($instance, "pre_{$last_hook}", $class, $method_name, $data);
             call_user_func_array(array($instance, $method_name), $data);
             $this->loaderHooks($instance, "post_{$last_hook}", $class, $method_name, $data);
         }
     } elseif (is_object($instance)) {
         $last_hook = $this->loading[count($this->loading) - 1];
         $this->loaderHooks($instance, "pre_{$last_hook}", $class, $method_name, $data);
     } else {
         if (strpos($namespace, 'controller') > 0) {
             header("HTTP/1.0 404 Not Found");
             Base::getInstance()->hook('404', array('file' => $ref_class, 'controller' => $class, 'method' => $method_name, 'message' => '404'));
             die;
         }
     }
     $this->clearCurrentLoader();
     return $instance;
 }
Beispiel #11
0
<?php

/**
* Bootstrap
*	File: index.php
*-----------------------------------------------------------------------------*/
/**
* Path to Application directory
*/
define('APPPATH', './application/');
/**
* Path to System directory (Base files)
*/
define('SYSTEMPATH', './system/');
/**
* "Bases Loaded!"
*/
require_once SYSTEMPATH . 'init.php';
//base object instance
$base = Base::getInstance();