Example #1
0
 /**
  * New material entity creation controller action
  * @param int $navigation Parent navigation identifier
  */
 public function __new($navigation = array())
 {
     // Create new entity
     $entity = new Material();
     $entity->Active = 1;
     $entity->Created = date('Y-m-d H:m:s');
     // Set user
     $user = $this->system->module('social')->user();
     $entity->UserID = $user->user_id;
     // Persist
     $entity->save();
     // Set name for created material
     $entity->Name = t($this->name, true) . ' №' . $entity->id;
     $entity->Url = utf8_translit($entity->Name);
     // Check unique url for material
     if ($this->query->entity(Material::class)->where(Material::F_IDENTIFIER, utf8_translit($entity->Name))->first()) {
         $entity->Url = md5(utf8_translit($entity->Name));
     }
     // Persist
     $entity->save();
     Event::fire('samsoncms.app.material.new', array(&$entity));
     $navigation = is_array($navigation) ? $navigation : array($navigation);
     // Set navigation relation
     foreach (array_merge($navigation, static::$structures) as $structureID) {
         // Create relation with structure
         $structureMaterial = new NavigationMaterial();
         $structureMaterial->MaterialID = $entity->id;
         $structureMaterial->StructureID = $structureID;
         $structureMaterial->Active = '1';
         $structureMaterial->save();
     }
     // Go to correct form URL
     url()->redirect($this->system->module('cms')->baseUrl . '/' . $this->id . '/form/' . $entity->id);
 }
Example #2
0
 /**
  * Override default entity saving
  */
 public function save()
 {
     // Format url
     $this->Url = str_replace(' ', '-', utf8_translit($this->Url));
     parent::save();
     $relationEntity = CMS::MATERIAL_FIELD_RELATION_ENTITY;
     foreach (static::$fieldIDs as $fieldID => $fieldName) {
         $type = static::$fieldValueColumns[$fieldID];
         // If material field relation exists use it or create new
         $materialField = null;
         if (!$this->query->entity($relationEntity)->where(Field::F_PRIMARY, $fieldID)->where(Material::F_PRIMARY, $this->id)->first($materialField)) {
             /** @var Field $materialfield */
             $materialField = new $relationEntity();
             $materialField->Active = 1;
             $materialField->MaterialID = $this->id;
             $materialField->FieldID = $fieldID;
         }
         // Set locale only if this field is localized
         if (array_key_exists($fieldID, static::$localizedFieldIDs)) {
             $materialField->locale = $this->locale;
         }
         $materialField->{$type} = $this->{$fieldName};
         $materialField->save();
     }
     $this->attachTo(static::$navigationIDs);
 }
Example #3
0
 /**
  * Add new row to table of entity
  * @param $row
  */
 public function addTableRow(Row $row)
 {
     // Get user
     $user = m('socialemail')->user();
     $tableMaterial = new Material();
     $tableMaterial->parent_id = $this->id;
     $tableMaterial->type = 3;
     $tableMaterial->Name = $this->Url . '-' . md5(date('Y-m-d-h-i-s'));
     $tableMaterial->Url = $this->Url . '-' . md5(date('Y-m-d-h-i-s'));
     $tableMaterial->Published = 1;
     $tableMaterial->Active = 1;
     $tableMaterial->priority = 0;
     $tableMaterial->UserID = $user->id;
     $tableMaterial->Created = date('Y-m-d H:m:s');
     $tableMaterial->Modyfied = date('Y-m-d H:m:s');
     $tableMaterial->save();
     // TODO: Ugly way to retrieve static var
     $class = new \ReflectionClass(preg_replace('/Row$/', '', get_class($row)));
     $structureId = $class->getConstant('IDENTIFIER');
     $structureMaterial = new structurematerial();
     $structureMaterial->Active = 1;
     $structureMaterial->MaterialID = $tableMaterial->id;
     $structureMaterial->StructureID = $structureId;
     $structureMaterial->save();
     // TODO: Ugly way to retrieve static var
     $class = new \ReflectionClass(get_class($row));
     $fieldIDs = $class->getStaticPropertyValue('fieldIDs');
     // Iterate and set all fields of row
     foreach ($row as $id => $value) {
         /**
          * Go next if it primary key because its public
          * TODO Fix it
          */
         if ($id === 'primary') {
             continue;
         }
         // Get field id
         $fieldId = $fieldIDs[$id];
         // Add additional field to created material
         $tableMaterial->setFieldByID($fieldId, $value);
     }
     // Save material
     $tableMaterial->save();
 }
Example #4
0
 /**
  * Constructor
  * @param string $material_id Material identifier
  */
 public function __construct($material_id = null, $parentStructure = null)
 {
     // Variable to store navigation ids to get fields by them from structurefields
     $navigationForFields = array();
     // Add structure material condition
     $scg = new Condition('or');
     $scg->arguments[] = new Argument(dbMySQLConnector::$prefix . 'structurematerial_Active', 1);
     $scg->arguments[] = new Argument(dbMySQLConnector::$prefix . 'structurematerial_Active', NULL, dbRelation::ISNULL);
     // Perform Material request with related CMSNavs
     if (dbQuery(ns_classname('Material', 'samson\\cms'))->MaterialID($material_id)->join('structurematerial')->join('structure')->join('user')->Active(1)->cond($scg)->first($this->material)) {
         // Existing material handling
         // If material has relations with cmsnav
         $cmsnavs =& $this->material->onetomany['_structure'];
         if (isset($cmsnavs)) {
             // WYSIWYG query
             $fields_query = dbQuery('\\samson\\cms\\CMSNavField')->join('\\samson\\cms\\CMSField')->order_by('FieldID', 'ASC')->Active(1);
             // If material has related cmsnavs - gather material related cmsnavs info
             foreach ($cmsnavs as $structure) {
                 $this->navs[$structure->id] = $structure;
                 if ($structure->type != 2) {
                     $navigationForFields[] = $structure->id;
                 }
             }
             // Add cmsnavs ids to query
             $fields_query->StructureID($navigationForFields);
             // Perform DB request
             if ($fields_query->exec($fields)) {
                 foreach ($fields as $data) {
                     // Pointer to field object
                     $db_field =& $data->onetoone['_field'];
                     // Add field data to collection
                     $this->fields[] = $db_field;
                     if (isset($db_field->Type) && $db_field->Type == '8') {
                         $this->tabs[] = new MaterialFieldLocalizedTab($this, $db_field, 'WYSIWYG');
                     }
                 }
             }
         }
     } else {
         // Material empty draft creation
         $this->material = new Material();
         $this->material->Draft = $this->material->id;
         $this->material->Name = 'Новый материал';
         $this->material->Created = date('h:m:i d.m.y');
         //			$this->material->user_id = auth()->user->id;
         $this->material->user_id = m('social')->user()->user_id;
         $this->material->Active = 1;
         $this->material->save();
         if (isset($parentStructure)) {
             /** @var \samson\cms\web\navigation\CMSNav $str */
             $str = null;
             if (dbQuery('\\samson\\cms\\web\\navigation\\CMSNav')->id($parentStructure)->first($str)) {
                 while (isset($str)) {
                     $this->navs[$str->id] = $str;
                     $str = $str->parent();
                 }
             }
         }
     }
     // Autoload base tab classes
     class_exists('samsoncms\\app\\material\\MainTab');
     class_exists('samsoncms\\app\\material\\FieldLocalizedTab');
     // Iterate declared classes to find other FormTab children to load to form
     foreach (get_declared_classes() as $class) {
         // If class if samson\cms\web\material\FormTab child
         if (is_subclass_of($class, ns_classname('FormTab', 'samsoncms\\app\\material'))) {
             // Tab supports automatic rendering flag
             eval('$ar = ' . $class . '::$AUTO_RENDER;');
             // PHP 5.2 support
             if ($ar === true) {
                 // Create and add FormTab instance to form tabs collection
                 $this->tabs[] = new $class($this);
             }
         }
     }
     // Sort tabs by their index
     usort($this->tabs, array($this, 'tabs_sorter'));
 }