src/App.php
public function __async_add($materialId, $structureId, $afterAction, $afterMaterialId, $afterStructureId)
    {
        /** @var \samson\cms\CMSMaterial $material Current material object */
        $material = null;

        // If there are no such row yet
        if ($this->query->className('\samson\cms\CMSMaterial')->cond('MaterialID', $materialId)->first($material)) {

            /** @var array $structures Array of structures of this material */
            $structureIds = $this->query->className('structurematerial')->cond('MaterialID', $material->id)->fields('StructureID');
            $structures = $this->query->className('structure')->cond('StructureID', $structureIds)->exec();

            // If there are some structures
            if (!empty($structures)) {

                /** @var \samson\cms\Navigation $structure Table structure */
                foreach ($structures as $structure) {

                    // If current material has incoming structure
                    // TODO: Sometimes array has empty values not object - needs to checked
                    if (isset($structure->StructureID) && ($structure->StructureID == $structureId)) {

                        /** @var \samson\social\Core $socialModule Social module object */
                        $socialModule = m('social');
                        /** @var \samson\activerecord\user $user User object */
                        $user = $socialModule->user();

                        // Get max priority of this structure
                        $maxPriority = $this->getMaxPriority($materialId, $structureId);

                        /** @var \samson\cms\CMSMaterial $tableMaterial New table material (table row) */
                        $tableMaterial = new \samson\cms\CMSMaterial(false);
                        $tableMaterial->type = 3;
                        $tableMaterial->Name = $structure->Name;
                        $tableMaterial->Url = $structure->Name . '-' . md5(date('Y-m-d-h-i-s'));
                        $tableMaterial->parent_id = $material->MaterialID;
                        $tableMaterial->Published = 1;
                        $tableMaterial->Active = 1;
                        $tableMaterial->priority = $maxPriority+1;
                        $tableMaterial->UserID = $user->id;
                        $tableMaterial->Created = date('Y-m-d H:m:s');
                        $tableMaterial->Modyfied = $tableMaterial->Created;
                        $tableMaterial->save();

                        /** @var \samson\cms\CMSNavMaterial $structureMaterial Relation between created table material
                         * and table structure */
                        $structureMaterial = new \samson\cms\CMSNavMaterial(false);
                        $structureMaterial->StructureID = $structureId;
                        $structureMaterial->MaterialID  = $tableMaterial->MaterialID;
                        $structureMaterial->Active = 1;
                        $structureMaterial->save();
                    }
                }

                $result = $this->__async_table($afterMaterialId, $afterStructureId);

                Event::fire('samson.cms.web.materialtable.add', array($material->id, $structureId));
                // Set success status and return result
//                return array('status' => 1);
                return $result;
            }
        }
        // Set fail status and return result
        return array('status' => 0);
    }
public function __async_add($materialId, $structureId, $afterAction, $afterMaterialId, $afterStructureId)
    {
        /** @var \samson\cms\CMSMaterial $material Current material object */
        $material = null;

        // If there are no such row yet
        if ($this->query->className('\samson\cms\CMSMaterial')->cond('MaterialID', $materialId)->first($material)) {

            /** @var array $structures Array of structures of this material */
            $structureIds = $this->query->className('structurematerial')->cond('MaterialID', $material->id)->fields('StructureID');
            $structures = $this->query->className('structure')->cond('StructureID', $structureIds)->exec();

            // If there are some structures
            if (!empty($structures)) {

                /** @var \samson\cms\Navigation $structure Table structure */
                foreach ($structures as $structure) {

                    // If current material has incoming structure
                    // TODO: Sometimes array has empty values not object - needs to checked
                    if (isset($structure->StructureID) && ($structure->StructureID == $structureId)) {

                        /** @var \samson\social\Core $socialModule Social module object */
                        $socialModule = m('social');
                        /** @var \samson\activerecord\user $user User object */
                        $user = $socialModule->user();

                        // Get max priority of this structure
                        $maxPriority = $this->getMaxPriority($materialId, $structureId);

                        /** @var \samson\cms\CMSMaterial $tableMaterial New table material (table row) */
                        $tableMaterial = new \samson\cms\CMSMaterial(false);
                        $tableMaterial->type = 3;
                        $tableMaterial->Name = $structure->Name;
                        $tableMaterial->Url = $structure->Name . '-' . md5(date('Y-m-d-h-i-s'));
                        $tableMaterial->parent_id = $material->MaterialID;
                        $tableMaterial->Published = 1;
                        $tableMaterial->Active = 1;
                        $tableMaterial->priority = $maxPriority+1;
                        $tableMaterial->UserID = $user->id;
                        $tableMaterial->Created = date('Y-m-d H:m:s');
                        $tableMaterial->Modyfied = $tableMaterial->Created;
                        $tableMaterial->save();

                        /** @var \samson\cms\CMSNavMaterial $structureMaterial Relation between created table material
                         * and table structure */
                        $structureMaterial = new \samson\cms\CMSNavMaterial(false);
                        $structureMaterial->StructureID = $structureId;
                        $structureMaterial->MaterialID  = $tableMaterial->MaterialID;
                        $structureMaterial->Active = 1;
                        $structureMaterial->save();
                    }
                }

                $result = $this->__async_table($afterMaterialId, $afterStructureId);

                Event::fire('samson.cms.web.materialtable.add', array($material->id, $structureId));
                // Set success status and return result
//                return array('status' => 1);
                return $result;
            }
        }
        // Set fail status and return result
        return array('status' => 0);
    }
public function __async_add ( $ materialId , $ structureId , $ afterAction , $ afterMaterialId , $ afterStructureId ) { /** @var \samson\cms\CMSMaterial $material Current material object */ $ material = null ; // If there are no such row yet if ( $ this -> query -> className ( '\samson\cms\CMSMaterial' ) -> cond ( 'MaterialID' , $ materialId ) -> first ( $ material ) ) { /** @var array $structures Array of structures of this material */ $ structureIds = $ this -> query -> className ( 'structurematerial' ) -> cond ( 'MaterialID' , $ material -> id ) -> fields ( 'StructureID' ) ; $ structures = $ this -> query -> className ( 'structure' ) -> cond ( 'StructureID' , $ structureIds ) -> exec ( ) ; // If there are some structures if ( ! empty ( $ structures ) ) { /** @var \samson\cms\Navigation $structure Table structure */ foreach ( $ structures as $ structure ) { // If current material has incoming structure // TODO: Sometimes array has empty values not object - needs to checked if ( isset ( $ structure -> StructureID ) && ( $ structure -> StructureID == $ structureId ) ) { /** @var \samson\social\Core $socialModule Social module object */ $ socialModule = m ( 'social' ) ; /** @var \samson\activerecord\user $user User object */ $ user = $ socialModule -> user ( ) ; // Get max priority of this structure $ maxPriority = $ this -> getMaxPriority ( $ materialId , $ structureId ) ; /** @var \samson\cms\CMSMaterial $tableMaterial New table material (table row) */ $ tableMaterial = new \ samson \ cms \ CMSMaterial ( false ) ; $ tableMaterial -> type = 3 ; $ tableMaterial -> Name = $ structure -> Name ; $ tableMaterial -> Url = $ structure -> Name . '-' . md5 ( date ( 'Y-m-d-h-i-s' ) ) ; $ tableMaterial -> parent_id = $ material -> MaterialID ; $ tableMaterial -> Published = 1 ; $ tableMaterial -> Active = 1 ; $ tableMaterial -> priority = $ maxPriority + 1 ; $ tableMaterial -> UserID = $ user -> id ; $ tableMaterial -> Created = date ( 'Y-m-d H:m:s' ) ; $ tableMaterial -> Modyfied = $ tableMaterial -> Created ; $ tableMaterial -> save ( ) ; /** @var \samson\cms\CMSNavMaterial $structureMaterial Relation between created table material
                         * and table structure */ $ structureMaterial = new \ samson \ cms \ CMSNavMaterial ( false ) ; $ structureMaterial -> StructureID = $ structureId ; $ structureMaterial -> MaterialID = $ tableMaterial -> MaterialID ; $ structureMaterial -> Active = 1 ; $ structureMaterial -> save ( ) ; } } $ result = $ this -> __async_table ( $ afterMaterialId , $ afterStructureId ) ; Event :: fire ( 'samson.cms.web.materialtable.add' , array ( $ material -> id , $ structureId ) ) ; // Set success status and return result //                return array('status' => 1); return $ result ; } } // Set fail status and return result return array ( 'status' => 0 ) ; }
Creating new material table row
@param int $materialId Current material identifier
@param int $structureId Current table structure identifier
@param $afterAction
@param $afterMaterialId
@param $afterStructureId
@return array AJAX response
Creating new material table row