ilch Forum » Allgemein » HTML, PHP, SQL,... » Speichern von arrays in Ilch 2.x

Geschlossen
  1. #1
    User Pic
    magicmarkus Mitglied
    Registriert seit
    10.02.2014
    Beitrge
    342
    Beitragswertungen
    12 Beitragspunkte
    Hallo Community.

    Ich arbeite gerade an einem Modul. In diesem Modul wird ein array generiert aus einer bzw. mehreren Tabellen

    <link href="<?=$this->getModuleUrl('static/css/teams.css') ?>" rel="stylesheet">
    
    <?php if (isset ($_COOKIE['role'])):?>
     	<?php $where =$_COOKIE['role'] ?>
    <?php endif;?>
    <?php if ($where == 'executive_officer' OR $where == 'commander'):?>
    <h1>
        <?php if ($this->get('factors') != '') {
            echo $this->getTrans('edit');
        } else {
            echo $this->getTrans('add');
        }
        ?>
        
        <?php $factors_activitie = $this->get('factors_activitie'); ?>
        <?php $factors_rank = $this->get('factors_rank'); ?>
        <a class="badge" data-toggle="modal" data-target="#infoModal">
            <i class="fa fa-info"></i>
        </a>
    </h1>
    <form class="form-horizontal" method="POST" enctype="multipart/form-data">
        <?=$this->getTokenField() ?>
            <br>
        <fieldset>
           <label>
               <div align="center"><h1><?=($this->getRequest()->getParam('clan')) ?></h1></div> 
            </label>
        </fieldset>
        <br>
        <br>
    	<fieldset style="border: 1px solid #EEEEEE;padding: 10px;">
           <legend><?=$this->getTrans('ranks') ?>:</legend>
           <?php foreach ($factors_rank as $rank):?>
    		
    		<div class="form-group">
               <input type="hidden" id="id" name="id" value="<?=$rank->getId();?>" />
               <input type="hidden" id="tabelle" name="tabelle" value="activities_factor_ranks" />
                <label class="col-lg-2">
                    <?=$rank->getMember_Role() ?>
                </label>
                <div class="col-lg-2">
                	<input type="text"
                       class="form-control"
                       id="factor"
                       name="factor"
                       value="<?=($rank != '') ? $this->escape($rank->getFactor()) : $this->originalInput('factor') ?>" />
                    
                </div>
            </div>
                     
           <?php endforeach;?>
        </fieldset>
        
        <br>
        
        
    	<fieldset style="border: 1px solid #EEEEEE;padding: 10px;">
           <legend><?=$this->getTrans('activitie') ?>:</legend>
           <?php $willi = ''; ?>
           <?php foreach ($factors_activitie as $activitie):
           ?>
           <input type="hidden" id="id" name="id" value="<?=$activitie->getId();?>" />
           <input type="hidden" id="tabelle" name="tabelle" value="activities_factor_activitie" />
     	   <?php  
           if ($willi != $activitie->getModus()){
               $willi = $activitie->getModus();
           ?>
    				
    		<legend><?=$activitie->getModus() ?>:</legend>
    			
    		<?php
    }
    		?>
    		<div class="form-group">
                <label class="col-lg-2">
                    <?=$activitie->getActivitie() ?>
                </label>
                <div class="col-lg-2">
                	<input type="text"
                       class="form-control"
                       id="factor"
                       name="factor"
                       value="<?=($activitie != '') ? $this->escape($activitie->getFactor()) : $this->originalInput('factor') ?>" />
                    
                </div>
            </div>
    
           <?php endforeach;?>
        </fieldset>
        
        <?=($this->get('factors') != '')  ? $this->getSaveBar('edit') : $this->getSaveBar('add') ?>
    </form>
    <?php else:?>
    <div align="center"><h1>Keine Berechtigung</h1></div>
    <?php endif?>




    Der Controller

    <?php
    /**
     * @copyright Ilch 2.0
     * @package ilch
     */
    
    namespace Modules\Activities\Controllers\Admin;
    
    use Modules\Activities\Mappers\Clans as ClansMapper;
    use Modules\Activities\Models\Clans as ClansModel;
    use Modules\Activities\Mappers\Activitie as ActivitieMapper;
    use Modules\Activities\Models\Factor as FactorModel;
    use Ilch\Validation;
    
    class Factors extends \Ilch\Controller\Admin
    {
        public function init()
        {
            $items = [
                [
                    'name' => 'overview',
                    'active' => false,
                    'icon' => 'fa fa-microchip',
                    'url' => $this->getLayout()->getUrl(['controller' => 'index', 'action' => 'index'])
                ],
                [
                    'name' => 'settings',
                    'active' => false,
                    'icon' => 'fa fa-cogs',
                    'url' => $this->getLayout()->getUrl(['controller' => 'settings', 'action' => 'index'])
                ],
                [
                    'name' => 'factors',
                    'active' => false,
                    'icon' => 'fas fa-calculator',
                    'url' => $this->getLayout()->getUrl(['controller' => 'settings', 'action' => 'factors'])
                    
                ]
            ];
            
            if ($this->getRequest()->getActionName() == 'treat') {
                $items[2]['active'] = true;
            } else {
                $items[2]['active'] = true;
            }
            
            $this->getLayout()->addMenu
            (
                'menuTeams',
                $items
                );
        }
        
        public function indexAction()
        {
            $this->getLayout()->getAdminHmenu()
            ->add($this->getTranslator()->trans('menuActivities'), ['controller' => 'index', 'action' => 'index'])
            ->add($this->getTranslator()->trans('menuFactors'), ['action' => 'factors']);
            
            $ActivitieMapper = new ActivitieMapper();
    
               
                $ActivitieMapper->activitie_insert();
             
                $ActivitieMapper->rank_insert();
    
            
            if ($ActivitieMapper->getCount_of_activitie() != 0 AND $ActivitieMapper->getCount_of_ranks() != 0) {
                
                $this->getView()->set('clans', $ActivitieMapper->getClans_from_List());
                
                
            }
            
        }
        
            
        public function treatAction()
        {
            
            
           
            $ActivitieMapper = new ActivitieMapper();
            
            if ($this->getRequest()->getParam('clan')) {
                $this->getLayout()->getAdminHmenu()
                ->add($this->getTranslator()->trans('menuActivities'), ['controller' => 'factors', 'action' => 'index'])
                ->add($this->getTranslator()->trans('edit'), ['action' => 'treat']);
                
                $this->getView()->set('factors_activitie', $ActivitieMapper->getFactors_Activitie($this->getRequest()->getParam('clan')));
                $this->getView()->set('factors_rank', $ActivitieMapper->getFactors_Rank($this->getRequest()->getParam('clan')));
                $this->getView()->set('factors', $ActivitieMapper->getFactors($this->getRequest()->getParam('clan')));
            } else {
                $this->getLayout()->getAdminHmenu()
                ->add($this->getTranslator()->trans('menuActivities'), ['controller' => 'factors', 'action' => 'index'])
                ->add($this->getTranslator()->trans('add'), ['action' => 'treat']);
            }
            
    
    
            
            
            if($this->getRequest()->getPost('save') == 'save')
            {
         
    
                
                $id = $this->getRequest()->getParam('id');
                if(is_array($this->getRequest()->getParam('id'))){
                foreach ($this->getRequest()->getParam('id') as $id){
                var_dump($_POST);
                }
                }
                die();
                /**
                if (isset ($_POST['tabelle'])){
                    if ($_POST['tabelle']=="activities_factor_ranks"){
                        $ActivitieMapper->editFactor_Ranks($id, $this->getRequest()->getPost('factor'));
                    }
                    if ($_POST['tabelle']=="activities_factor_activitie"){
                        $ActivitieMapper->editFactor_Activitie($id, $this->getRequest()->getPost('factor'));
                    }
                }
                **/
                /**
    
                $this->redirectToIndex();
                return;
    **/
            }
                
            
        }
        
        private function redirectToIndex($msg = "saveSuccess")
        {
            
            $this->redirect()
            ->withMessage($msg)  //deleteSuccess | saveSuccess
            ->to(['controller' => 'index', 'action' => 'index']);
        }
    }


    Hier komm ich nicht weiter. im bereich treat

    hier noch der mapper


    <?php
    /**
     * @copyright Ilch 2.0
     * @package ilch
     */
    
    namespace Modules\Activities\Mappers;
    
    use Modules\Activities\Models\Factor as FactorModel;
    use Modules\Activities\Models\Clans as ClansModel;
    
    class Activitie extends \Ilch\Mapper
    {
       
        
        /** FOR Factor by Rank **/
        
        /**
         * Gets the Factor_Rank.
         *
         * @param array $where
         * @return FactorModel[]|array
         */
        public function getFactor_Rank($where = [])
        {
            $entryArray = $this->db()->select('*')
            ->from('activities_factor_rank')
            ->where($where)
            ->execute()
            ->fetchRows();
            
            if (empty($entryArray)) {
                return [];
            }
            
            $factor_rank = [];
            foreach ($entryArray as $entries) {
                $entryModel = new FactorModel();
                $entryModel->setId($entries['id']);
                $entryModel->setClan($entries['clan']);
                $entryModel->setMember_Role($entries['member_role']);
                $entryModel->setFactor($entries['factor']);
                $factor_rank[] = $entryModel;
                
            }
            
            return $factor_rank;
        }
        
            
            
        /** FOR Factor by Activitie Modus **/
        
        /**
         * Gets the Factor by Activitie Modus.
         *
         * @param array $where
         * @return FactorModel[]|array
         */
        public function getFactor_Activitie($where = [])
        {
            $entryArray = $this->db()->select('*')
            ->from('activities_factor_activitie')
            ->where($where)
            ->execute()
            ->fetchRows();
            
            if (empty($entryArray)) {
                return [];
            }
            
            $factor_activitie = [];
            foreach ($entryArray as $entries) {
                $entryModel = new FactorModel();
                $entryModel->setId($entries['id']);
                $entryModel->setClan($entries['clan']);
                $entryModel->setModus($entries['modus']);
                $entryModel->setActivitie($entries['activitie']);
                $entryModel->setFactor($entries['factor']);
                $factor_activitie[] = $entryModel;
                
            }
            
            return $factor_activitie;
        }
        
        
            
        /** Clan **/
        
        /**
         * Gets the Clans.
         *
         * @param array $where
         * @return FactorModel[]|array
         */
            
        
        public function getClans_from_List()
        {
            $sql= 'SELECT clan FROM `[prefix]_activities_factor_activitie` GROUP BY `clan`';
            $entryArray = $this->db()->query($sql);
            
            $clans = [];
            
            if (empty($entryArray)) {
                return $clans;
            }
            
            foreach ($entryArray as $entries) {
                $entryModel = new FactorModel();
                $entryModel->setClan($entries['clan']);
                $clans[] = $entryModel;
            }
            
            return $clans;
        }
        
        public function getClanByClan($clan)
        {
            $sql= 'SELECT activities.clan,activities.modus,activities.activitie,activities.factor FROM `[prefix]_activities_factor_activitie` LEFT JOIN [prefix]_activities_factor_rank ON activities.clan = [prefix]_activities_factor_rank.clan GROUP BY `clan` WHERE clan="'.$clan.'"';
            $entryArray = $this->db()->query($sql);
            
            $clans = [];
            
            if (empty($entryArray)) {
                return $clans;
            }
            
            foreach ($entryArray as $entries) {
                $entryModel = new FactorModel();
                $entryModel->setClan($entries['clan']);
                $clans[] = $entryModel;
            }
            
            return $clans;
        }
        
        /** Querry und JOIN der Tabellen **/
    
        public function getFactors($clan)
        {
            $entryArray = $this->db()->select()
            ->fields(['p.clan', 'p.modus', 'p.activitie', 'p.factor'])
            ->from(['p' => 'activities_factor_activitie'])
            ->join(['pc' => 'activities_factor_rank'], 'p.clan = pc.clan', 'LEFT', ['pc.clan', 'pc.member_role', 'pc.factor'])
            ->where(['pc.clan' => $clan])
            ->execute()
            ->fetchRows();
            
            if (empty($entryArray)) {
                return [];
            }
            
            $factors = [];
            foreach ($entryArray as $entries) {
                $entryModel = new FactorModel();
                $entryModel->setClan($entries['clan']);
                $entryModel->setModus($entries['modus']);
                $entryModel->setActivitie($entries['activitie']);
                $entryModel->setMember_Role($entries['member_role']);
                $entryModel->setFactor($entries['factor']);
                $factors[] = $entryModel;
                
            }
            
            return $factors;
        }
    
    
        public function getFactors_Activitie($clan)
        {
            $entryArray = $this->db()->select()
            ->fields(['id', 'clan', 'modus', 'activitie', 'factor'])
            ->from(['activities_factor_activitie'])
            ->where(['clan' => $clan])
            ->execute()
            ->fetchRows();
            
            if (empty($entryArray)) {
                return [];
            }
            
            $factors_activitie = [];
            foreach ($entryArray as $entries) {
                $entryModel = new FactorModel();
                $entryModel->setId($entries['id']);
                $entryModel->setClan($entries['clan']);
                $entryModel->setModus($entries['modus']);
                $entryModel->setActivitie($entries['activitie']);
                $entryModel->setFactor($entries['factor']);
                $factors_activitie[] = $entryModel;
                
            }
            
            return $factors_activitie;
        }
        
        
        public function getFactors_Rank($clan)
        {
            $entryArray = $this->db()->select()
            ->fields(['id', 'clan', 'member_role', 'factor'])
            ->from(['activities_factor_rank'])
            ->where(['clan' => $clan])
            ->execute()
            ->fetchRows();
            
            if (empty($entryArray)) {
                return [];
            }
            
            $factors_rank = [];
            foreach ($entryArray as $entries) {
                $entryModel = new FactorModel();
                $entryModel->setId($entries['id']);
                $entryModel->setClan($entries['clan']);
                $entryModel->setMember_Role($entries['member_role']);
                $entryModel->setFactor($entries['factor']);
                $factors_rank[] = $entryModel;
                
            }
            
            return $factors_rank;
        }
        
        /** Vordefinition der Faktor Tabellen **/
        
        public function activitie_insert()
        {
            $sql = '
            insert into [prefix]_activities_factor_activitie (  
    clan,
      modus,
      activitie,
      factor
    )
    Select
      willi.clantag,
      willi.modus,
      willi.activitie,
      willi.factor
    From
      [prefix]_activities_factor_activitie ziel Right Join
      (Select
        erg.clantag,
        erg.modus,
        erg.activitie,
        (Case
          When isnull([prefix]_activities_factor_activitie.factor)
          Then 0.00
          Else [prefix]_activities_factor_activitie.factor
        End) As factor
      From
        (Select
          aus.clantag,
          [prefix]_activities_modus_config.modus,
          [prefix]_activities_activitie_config.activitie
        From
          [prefix]_activities_activitie_config,
          [prefix]_activities_modus_config,
          (Select
            [prefix]_activities_clans.clantag
          From
            [prefix]_activities_clans) As aus) As erg Left Join
        [prefix]_activities_factor_activitie On [prefix]_activities_factor_activitie.clan =
          erg.clantag And [prefix]_activities_factor_activitie.modus = erg.modus And
          [prefix]_activities_factor_activitie.activitie = erg.activitie) As willi On
        willi.clantag = ziel.clan And willi.modus = ziel.modus And
        willi.activitie = ziel.activitie
    Where
      isnull(ziel.id) = True;
            ';
            $this->db()->query($sql);
        }
        
        
        public function rank_insert()
        {
            $sql = '
            insert into [prefix]_activities_factor_rank(
      clan,
      member_role,
      factor
    )
    Select
      willi.clantag,
      willi.member_role,
      willi.factor
    From
      (Select
        erg.clantag,
        erg.member_role,
        (Case
          When isnull([prefix]_activities_factor_rank.factor)
          Then 0.00
          Else [prefix]_activities_factor_rank.factor
        End) As factor
      From
        (Select
          aus.clantag,
          [prefix]_activities_ranks_config.member_role
        From
          (Select
            [prefix]_activities_clans.clantag
          From
            [prefix]_activities_clans) As aus,
          [prefix]_activities_ranks_config) As erg Left Join
        [prefix]_activities_factor_rank On [prefix]_activities_factor_rank.clan =
          erg.clantag And [prefix]_activities_factor_rank.member_role = erg.member_role)
      As willi Left Join
      [prefix]_activities_factor_rank ziel On willi.clantag = ziel.clan And
        willi.member_role = ziel.member_role
    Where
      isnull(ziel.id) = True;
            ';
            $this->db()->query($sql);
        }
        
        
        public function getCount_of_activitie() {
            
            $entryArray = $this->db()->select()
            ->fields(['clan', 'modus', 'activitie', 'factor'])
            ->from(['activities_factor_activitie'])
            ->where([])
            ->execute();
            
            
            
            $count = $entryArray->getNumRows();
            
            return $count;
          
            }
        
            public function getCount_of_ranks() {
                
                $entryArray = $this->db()->select()
                ->fields(['clan', 'member_role', 'factor'])
                ->from(['activities_factor_rank'])
                ->where([])
                ->execute();
                
                
                
                $count = $entryArray->getNumRows();
                
                return $count;
                
                
            }
            
            
            public function editFactor_Activitie($id, $factor)
            {
                
                $this->db()->update('activities_factor_activitie')
                ->values(['factor' => $factor])
                ->where(['id' => $id])
                ->execute();
    
                    /**
                $id = mysqli_real_escape_string($this->db()->getLink(), $id);
                $factor = mysqli_real_escape_string($this->db()->getLink(), $factor);
                
                $query = "UPDATE [prefix]_activities_factor_activitie SET factor = '$factor' WHERE id = $id;";
                
                $this->db()->query($query);
                
                **/
            }
    
     
            
            public function editFactor_Ranks($id, $factor)
            {
                /**
                $id = mysqli_real_escape_string($this->db()->getLink(), $id);
                $factor = mysqli_real_escape_string($this->db()->getLink(), $factor);
                
                $query = "UPDATE [prefix]_activities_factor_rank SET factor = '$factor' WHERE id = $id;";
                
                $this->db()->query($query);
                **/
                
                $this->db()->update('activities_factor_rank')
                ->values(['factor' => $factor])
                ->where(['id' => $id])
                ->execute();
            }
            
    }



    Wie kann ich dem ILch 2.x sagen, dass er beim Speichern, alles als array nehmen soll und dann auch das array je nach tabelle mit der richtigen update funktion speichern soll.


    verwendete ilch Version: 2.1.x

    betroffene Homepage: www.r1sing.de
    0 Mitglieder finden den Beitrag gut.
Geschlossen

Zurck zu HTML, PHP, SQL,...

Optionen: Bei einer Antwort zu diesem Thema eine eMail erhalten