public function __construct(\samson\cms\CMSMaterial &$parent, $locale = 'ru')
 {
     $this->dbQuery = new dbQuery();
     // Retrieve pointer to current module for rendering
     $this->renderModule =& s()->module($this->renderModule);
     $this->locale = $locale;
     // Save pointer to CMSMaterial
     $this->parent =& $parent;
     // Get all child materials identifiers from parent
     $materialIDs = dbQuery('material')->cond('material.parent_id', $this->parent->id)->cond('material.Active', 1)->cond('material.Draft', 0)->fields('MaterialID');
     // Iterate all parent material structures
     foreach ($parent->cmsnavs() as $structure) {
         // Use only special structures
         if (isset($structure->type) && $structure->type == 1) {
             //$this->structures[$structure->id] = $structure;
             // Iterate all fields
             foreach ($structure->fields() as $field) {
                 // Use only localizable fields
                 if ($locale != '' && $field->local == 1 && !isset($this->fields[$field->id])) {
                     // Gather key => value fields collection
                     $this->fields[$field->id] = $field;
                     // Iterate all child materials
                     foreach ($materialIDs as $materialID) {
                         // Check if they already have this material field
                         if (!dbQuery('materialfield')->MaterialID($materialID)->locale($this->locale)->FieldID($field->id)->first()) {
                             // Create material field record
                             $mf = new \samson\activerecord\materialfield(false);
                             $mf->MaterialID = $materialID;
                             $mf->FieldID = $field->id;
                             $mf->Active = 1;
                             $mf->locale = $this->locale;
                             $mf->save();
                         }
                     }
                 } else {
                     if ($locale == '' && $field->local == 0 && !isset($this->fields[$field->id])) {
                         // Gather key => value fields collection
                         $this->fields[$field->id] = $field;
                         // Iterate all child materials
                         foreach ($materialIDs as $materialID) {
                             // Check if they already have this material field
                             if (!dbQuery('materialfield')->MaterialID($materialID)->locale($this->locale)->FieldID($field->id)->first()) {
                                 // Create material field record
                                 $mf = new \samson\activerecord\materialfield(false);
                                 $mf->MaterialID = $materialID;
                                 $mf->FieldID = $field->id;
                                 $mf->Active = 1;
                                 $mf->save();
                             }
                         }
                     }
                 }
             }
         }
     }
     // Get all child materials material fields for this locale
     $this->query = dbQuery('material')->parent_id($this->parent->id)->join('materialfield')->cond('materialfield_FieldID', array_keys($this->fields))->cond('materialfield_locale', $this->locale);
     // Constructor treed
     parent::__construct($this->query);
 }
示例#2
0
文件: CMS.php 项目: samsonos/cms_api
 public function materialColumnToField($column, $structure)
 {
     // Find first user
     $user = null;
     if (dbQuery('user')->first($user)) {
     }
     // Create structure for all materials
     $db_structure = null;
     if (!dbQuery('structure')->Url('__' . $structure)->cond('Active', 1)->first($db_structure)) {
         $db_structure = new \samson\activerecord\structure(false);
         $db_structure->Name = $structure;
         $db_structure->Url = '__' . $structure;
         $db_structure->Active = 1;
         $db_structure->UserID = $user->id;
         $db_structure->system = 1;
         $db_structure->save();
     }
     $dbField = null;
     if (!dbQuery('field')->Name($column)->first($dbField)) {
         $dbField = new \samson\activerecord\field(false);
         $dbField->Name = $column;
         $dbField->Type = 8;
         $dbField->Active = 1;
         $dbField->system = 1;
         $dbField->save();
     }
     // Create structure field relations
     $db_sf = null;
     if (!dbQuery('structurefield')->FieldID($dbField->id)->StructureID($db_structure->id)->cond('Active', 1)->first($db_sf)) {
         $db_sf = new \samson\activerecord\structurefield(false);
         $db_sf->FieldID = $dbField->id;
         $db_sf->StructureID = $db_structure->id;
         $db_sf->Active = 1;
         $db_sf->save();
     }
     // Iterate all existing materials
     $db_materials = array();
     if (dbQuery('material')->cond('Active', '1')->Draft('0')->exec($db_materials)) {
         trace('Found materials:' . sizeof($db_materials));
         foreach ($db_materials as $db_material) {
             //trace('Updating material:'.$db_material->id);
             // If current material has no connection with new structure
             $db_sm = null;
             if (!dbQuery('structurematerial')->StructureID($db_structure->id)->MaterialID($db_material->id)->first($db_sm)) {
                 // Create this connection
                 $db_sm = new \samson\activerecord\structurematerial(false);
                 $db_sm->StructureID = $db_structure->id;
                 $db_sm->MaterialID = $db_material->id;
                 $db_sm->Active = 1;
                 $db_sm->save();
                 //trace('Updating structurematerial:'.$db_material->id);
             }
             // If this material has no Content field right now
             $db_mf = null;
             if (!dbQuery('materialfield')->MaterialID($db_material->id)->FieldID($dbField->id)->cond('Active', 1)->first($db_mf)) {
                 // Create Content additional field
                 $db_mf = new \samson\activerecord\materialfield(false);
                 $db_mf->MaterialID = $db_material->id;
                 $db_mf->FieldID = $dbField->id;
                 $db_mf->Active = 1;
                 $db_mf->Value = $db_material[$column];
                 $db_mf->save();
                 //trace('Updating materialfield:'.$db_material->id);
             }
         }
     }
     db()->query('ALTER TABLE  `material` DROP  `' . $column . '`');
 }
示例#3
0
 /**
  * Store all additional fields in the material
  * @param $entityID
  * @param int $locale Load locale fields or not
  */
 public function loadAdditionalFields($entityID, $locale = 0, $structureIds = null)
 {
     /** @var \samsonframework\orm\Record[] $structureIDs Get material entity navigation identifiers */
     $structureIDs = array();
     if ($this->query->className('structurematerial')->cond('MaterialID', $entityID)->cond('Active', 1)->fields('StructureID', $structureIDs)) {
         // Get only structure which not material table
         $newStructure = null;
         if ($this->query->className('structure')->cond('StructureID', $structureIDs)->cond('type', array('2'), Relation::NOT_EQUAL)->fields('StructureID', $newStructure)) {
             $structureIDs = $newStructure;
         }
         /** @var \samsonframework\orm\Record[] $structureFields Get structure-fields records for this entity with fields data */
         $structureFields = array();
         if ($this->query->className('structurefield')->cond('field_Type', array('9', '8', '5'), Relation::NOT_EQUAL)->cond('field_local', $locale)->cond('Active', 1)->cond('StructureID', empty($structureIds) ? $structureIDs : $structureIds)->join('field')->group_by('field_FieldID')->order_by('FieldID', 'ASC')->exec($structureFields)) {
             /** @var array $fieldIDs Collection of field identifiers */
             $fieldIDs = array();
             foreach ($structureFields as $structureField) {
                 // Gather used field identifiers
                 $fieldIDs[] = $structureField->FieldID;
                 // If additional field is found
                 $field =& $structureField->onetoone['_field'];
                 if (isset($field)) {
                     // If need get not localized fields
                     if ($locale == 0) {
                         // If there no materialfield item then create it
                         $mf = null;
                         if (!$this->query->className('materialfield')->cond('FieldID', $field->id)->cond('MaterialID', $entityID)->cond('Active', 1)->first($mf)) {
                             // Create new materialfield
                             $mf = new \samson\activerecord\materialfield(false);
                             $mf->locale = '';
                             $mf->Active = 1;
                             $mf->MaterialID = $entityID;
                             $mf->FieldID = $field->id;
                             $mf->save();
                         }
                         // Skip filed which have to be hide
                         if (!$field->showInForm) {
                             continue;
                         }
                         // Create input field grouped by field identifier
                         $this->additionalFields[] = new Generic($field->Name, isset($field->Description[0]) ? t($field->Description, true) : t($field->Name, true), $field->Type);
                         // Save mf
                         $this->materialFields[] = $mf;
                         // It is localized fields
                     } else {
                         // Get current locales
                         $locales = SamsonLocale::$locales;
                         $mf = null;
                         if (sizeof(SamsonLocale::$locales)) {
                             // Init arrays
                             $localeGeneric = array();
                             $localeData = array();
                             // Iterate locale and save their generic and data
                             foreach ($locales as $local) {
                                 // If there no materialfield item then create it
                                 if (!$this->query->className('materialfield')->cond('FieldID', $field->id)->cond('MaterialID', $entityID)->cond('Active', 1)->cond('locale', $local)->first($mf)) {
                                     // Create new materialfield
                                     $mf = new \samson\activerecord\materialfield(false);
                                     $mf->locale = $local;
                                     $mf->Active = 1;
                                     $mf->MaterialID = $entityID;
                                     $mf->FieldID = $field->id;
                                     $mf->save();
                                 }
                                 // Create generic for this field
                                 $localeGeneric[$local] = new Generic($field->Name, isset($field->Description[0]) ? $field->Description : $field->Name, $field->Type);
                                 // Save mf
                                 $localeData[$local] = $mf;
                             }
                             // Save all localized data
                             $this->additionalFields[] = $localeGeneric;
                             $this->materialFields[] = $localeData;
                         }
                     }
                 }
             }
         }
     }
 }
 /** @see \samson\cms\site\Table::row() */
 public function row(&$db_row, Pager &$pager = null, $module = null)
 {
     // Get field metadata
     $db_field =& $db_row->onetoone['_field'];
     // If parent Field object not found countinue
     if (!isset($db_field)) {
         return e('StructureField# ## - Field not found(##)', E_SAMSON_CMS_ERROR, array($db_row->id, $db_row->FieldID));
     }
     // Try to get already created materialfield object by field id
     if (isset($this->materialfields[$db_row->FieldID])) {
         $db_mf =& $this->materialfields[$db_row->FieldID];
     } else {
         //trace('New field for'.$db_field->id);
         $db_mf = new \samson\activerecord\materialfield(false);
         $db_mf->locale = $this->locale;
         $db_mf->Active = 1;
         $db_mf->MaterialID = $this->db_material->id;
         $db_mf->FieldID = $db_field->id;
         $db_mf->save();
     }
     // Create input element for field
     $input = null;
     // Depending on field type
     if ($db_field->Type != 9) {
         // Create input element for field
         $input = m('samsoncms_input_application')->createFieldByType($this->dbQuery, $db_field->Type, $db_mf);
     } else {
         return false;
     }
     if ($db_field->Type == 4) {
         /** @var \samsoncms\input\select\Application $input Input select type */
         $input->build($db_field->Value);
     }
     Event::fire('samson.cms.web.material.input', array($db_field, &$input, &$db_mf, &$this->dbQuery));
     // Render field row
     return m('material')->view($this->row_tmpl)->cmsfield($input)->matfield($db_mf)->fieldname(isset($db_field->Description[0]) ? $db_field->Description : $db_field->Name)->field($db_field)->pager($pager)->output();
 }
示例#5
0
 /**
  * External handler called on samson.cms.input.change event
  *
  * @param \samson\activerecord\dbRecord $dbObject Database entity instance
  * @param string $param Entity field
  * @param mixed $value Entity previous value
  */
 public function saveFieldHandler($dbObject, $param, $value)
 {
     // If our object is material field
     if ($dbObject instanceof \samson\activerecord\materialfield) {
         // Get current material
         $material = dbQuery('material')->id($dbObject->MaterialID)->first();
         // If material can have related materials
         if ($material->type == 1 && $material->parent_id == 0) {
             // Get related materials identifiers
             $children = dbQuery('material')->cond('parent_id', $material->id)->fields('MaterialID');
             // For each child create or update material field record
             foreach ($children as $child) {
                 if (!dbQuery('materialfield')->cond('FieldID', $dbObject->FieldID)->cond('locale', $dbObject->locale)->cond('MaterialID', $child)->first($childMaterialField)) {
                     $childMaterialField = new \samson\activerecord\materialfield(false);
                     $childMaterialField->MaterialID = $child;
                     $childMaterialField->FieldID = $dbObject->FieldID;
                     $childMaterialField->locale = $dbObject->locale;
                     $childMaterialField->Active = 1;
                 }
                 $childMaterialField->Value = $dbObject->Value;
                 if (isset($dbObject->numeric_value)) {
                     $childMaterialField->numeric_value = $dbObject->numeric_value;
                 }
                 $childMaterialField->save();
             }
         }
     }
     if ($dbObject instanceof \samson\activerecord\material && $param == 'remains') {
         /** @var \samson\activerecord\material $parent */
         $parent = null;
         if (dbQuery('material')->cond('MaterialID', $dbObject->parent_id)->first($parent)) {
             $parent->remains = (int) $parent->remains - (int) $value + (int) $dbObject->remains;
             $parent->save();
         }
     }
 }
示例#6
0
 /**
  * Create materialfield record
  *
  * @param string $value Field value for materialfield table
  *
  * @internal param \samson\parse\strind $field_id Field identifier in field table
  * @internal param string $material_id Material identifier in material table
  * @return \samson\activerecord\materialfield MaterialField table object
  */
 public function &parser($value, $data)
 {
     $mf = null;
     if (!dbQuery('\\samson\\activerecord\\materialfield')->cond('MaterialID', $this->material->result->id)->cond('FieldID', $this->db_field->id)->cond('locale', isset($this->locale) ? $this->locale : '')->first($mf)) {
         $mf = new \samson\activerecord\materialfield(false);
     }
     $mf->FieldID = $this->db_field->id;
     $mf->MaterialID = $this->material->result->id;
     // If this is numeric field
     if ($this->type == 7) {
         $mf->numeric_value = $value;
     } else {
         // Other fields
         // If this is wysiwyg field
         if ($this->type == 8) {
             if (is_object($value)) {
                 //trace('ogject!!!!!!!!');
                 //trace($value);
                 $tempValue = '';
                 $elements = $value->getRichTextElements();
                 foreach ($elements as $item) {
                     $excelFont = $item->getFont();
                     $bold = false;
                     if (isset($excelFont)) {
                         if ($excelFont->getBold()) {
                             $bold = true;
                         }
                     }
                     if (!$bold) {
                         $tempValue .= $item->getText();
                     } else {
                         $tempValue .= '<b>' . $item->getText() . '</b>';
                     }
                 }
                 $value = $tempValue;
             }
             $mf->Value = nl2br($value);
         } else {
             $mf->Value = $value;
         }
     }
     $mf->Active = 1;
     $mf->locale = isset($this->locale) ? $this->locale : '';
     $mf->save();
     return $mf;
 }
 public function addEmptyRow()
 {
     /** @var \samsoncms\api\Material $defaultMaterial */
     $defaultMaterial = null;
     if (!dbQuery('material')->cond('parent_id', $this->material->id)->cond('type', 3)->cond('Active', 2)->first()) {
         m('material_table')->__async_add($this->material->id, $this->structure->id, 2, $defaultMaterial);
         foreach ($this->fields as $field) {
             trace($field, 2);
             /** @var \samson\activerecord\materialfield $materialField Create material field record */
             $materialField = new \samson\activerecord\materialfield(false);
             $materialField->MaterialID = $defaultMaterial->id;
             $materialField->FieldID = $field->id;
             $materialField->Active = 1;
             $materialField->locale = empty($field->local) ? '' : $this->locale;
             // Set materialfield locale if locale is set and field is localized
             //                if ($this->locale != '' && $field->local == 1) {
             //                    $materialField->locale = $this->locale;
             //                }
             // Write it to DataBase
             $materialField->save();
         }
     }
 }