Overview

Namespaces

  • GLFramework
    • Cache
    • Controller
    • DaMa
      • Manipulators
    • Database
    • HTTP
    • Mail
    • Middleware
    • Model
    • Module
    • Tests
    • Upload
    • Utils
  • None

Classes

  • GLFramework\Bootstrap
  • GLFramework\Cache\Cache
  • GLFramework\Cache\MemcachedCache
  • GLFramework\Cache\MemoryCache
  • GLFramework\Controller
  • GLFramework\Controller\AdminController
  • GLFramework\Controller\APIController
  • GLFramework\Controller\AuthController
  • GLFramework\Controller\ErrorController
  • GLFramework\Controller\ExceptionController
  • GLFramework\DaMa\Association
  • GLFramework\DaMa\DataManipulation
  • GLFramework\DaMa\Manipulator
  • GLFramework\DaMa\Manipulators\CSVManipulator
  • GLFramework\DaMa\Manipulators\ManipulatorCore
  • GLFramework\DaMa\Manipulators\XLSManipulator
  • GLFramework\DaMa\Manipulators\XLSXManipulator
  • GLFramework\Database\Connection
  • GLFramework\Database\MySQLConnection
  • GLFramework\DatabaseManager
  • GLFramework\DBStructure
  • GLFramework\Events
  • GLFramework\Filesystem
  • GLFramework\HTTP\API
  • GLFramework\Log
  • GLFramework\Mail
  • GLFramework\Mail\Mail
  • GLFramework\Mail\MailSystem
  • GLFramework\Mail\PHPMailer
  • GLFramework\MailView
  • GLFramework\Middleware\APIAuthorizationMiddleware
  • GLFramework\Middleware\ControllerMiddleware
  • GLFramework\Model
  • GLFramework\Model\Page
  • GLFramework\Model\User
  • GLFramework\Model\UserPage
  • GLFramework\ModelResult
  • GLFramework\Module\Module
  • GLFramework\Module\ModuleManager
  • GLFramework\Request
  • GLFramework\Response
  • GLFramework\Tests\DatabaseTestCase
  • GLFramework\Tests\TestCase
  • GLFramework\Upload\Upload
  • GLFramework\Upload\Uploads
  • GLFramework\Utils\DataTablesManager
  • GLFramework\Versions
  • GLFramework\View

Interfaces

  • GLFramework\Middleware

Functions

  • array_merge_recursive_ex
  • e
  • file_get_php_classes
  • fix_date
  • fix_date_format
  • fix_decimal
  • fix_url
  • get
  • get_php_classes
  • http_response_code
  • instance_method
  • is_module_enabled
  • now
  • post
  • print_array
  • print_brief_debug
  • print_debug
  • random_str
  • reArrayFiles
  • reArrayPost
  • today
  • Overview
  • Namespace
  • Class
  1: <?php
  2: /**
  3:  * Created by PhpStorm.
  4:  * User: manus
  5:  * Date: 1/03/16
  6:  * Time: 10:19
  7:  */
  8: 
  9: namespace GLFramework\DaMa;
 10: 
 11: 
 12: use GLFramework\DaMa\Manipulators\ManipulatorCore;
 13: use GLFramework\Model;
 14: 
 15: class Manipulator
 16: {
 17: 
 18:     private $currentSheet;
 19:     /**
 20:      * @var Association[]
 21:      */
 22:     private $association = array();
 23:     private $modelName;
 24:     private $filename;
 25:     /**
 26:      * @var ManipulatorCore
 27:      */
 28:     private $core;
 29: 
 30:     /**
 31:      * @return mixed
 32:      */
 33:     public function getFilename()
 34:     {
 35:         return $this->filename;
 36:     }
 37: 
 38:     /**
 39:      * @param mixed $filename
 40:      * @return $this
 41:      */
 42:     public function setFilename($filename)
 43:     {
 44:         $this->filename = $filename;
 45:         return $this;
 46:     }
 47: 
 48:     /**
 49:      * @return ManipulatorCore
 50:      */
 51:     public function getCore()
 52:     {
 53:         return $this->core;
 54:     }
 55: 
 56:     /**
 57:      * @param ManipulatorCore $core
 58:      * @return $this
 59:      */
 60:     public function setCore($core)
 61:     {
 62:         $this->core = $core;
 63:         return $this;
 64:     }
 65:     public function model($modelName)
 66:     {
 67:         $this->modelName = $modelName;
 68:         return $this;
 69:     }
 70: 
 71:     public function field($nameInFile, $nameInModel, $fn = null)
 72:     {
 73:         if($association = $this->getAssociation($nameInModel))
 74:         {
 75:             $association->addNameInFile($nameInFile);
 76:         }
 77:         else
 78:         {
 79:             $association = new Association();
 80:             $association->addNameInFile($nameInFile);
 81:             $association->setNameInModel($nameInModel);
 82:             $association->setParser($fn);
 83:             $this->association[] = $association;
 84:         }
 85:         return $association;
 86:     }
 87: 
 88:     private function getAssociation($nameInModel)
 89:     {
 90:         foreach($this->association as $association)
 91:         {
 92:             if($association->getNameInModel() == $nameInModel)
 93:             {
 94:                 return $association;
 95:             }
 96:         }
 97:         return null;
 98:     }
 99: 
100:     public function constant($nameInModel, $value)
101:     {
102:         if($association = $this->getAssociation($nameInModel))
103:         {
104:             $association->setConstant($value);
105:         }
106:         else
107:         {
108:             $association = new Association();
109:             $association->setNameInModel($nameInModel);
110:             $association->setConstant($value);
111:             $this->association[] = $association;
112:         }
113:     }
114: 
115:     public function sheet($index)
116:     {
117:         $this->currentSheet = $index;
118:     }
119: 
120:     private function init($config = array())
121:     {
122:         $this->getCore()->open($this->getFilename(), $config);
123:         if($this->currentSheet !== null)
124:             $this->getCore()->setSheet($this->currentSheet);
125:     }
126: 
127:     public function exec($config = array())
128:     {
129:         $count = 0;
130:         $this->init($config);
131:         if($header = $this->getCore()->next())
132:         {
133:             while($next = $this->getCore()->next())
134:             {
135:                 if(implode("", $next) != "")
136:                 {
137:                     $model = $this->build($header, $next);
138:                     if($model->valid() && $model->save())
139:                     {
140:                         $count++;
141:                     }
142:                 }
143:             }
144:             return $count;
145:         }
146: 
147:         return false;
148:     }
149: 
150:     /**
151:      * @param $header
152:      * @param $row
153:      * @return Model
154:      */
155:     public function build($header, $row)
156:     {
157:         $associative = array();
158:         foreach($header as $key => $value)
159:         {
160:             $associative[$value] = $row[$key];
161:         }
162:         $model = new $this->modelName();
163:         foreach($this->association as $association)
164:         {
165:             if($association->index)
166:             {
167:                 if($association->fill($model, $associative))
168:                 {
169:                     $value = $model->{$association->nameInModel};
170:                     if($value && !empty($value))
171:                     {
172:                         $model = $model->get(array($association->nameInModel => $value))->getModel();
173:                         break;
174:                     }
175:                 }
176:             }
177:         }
178:         foreach($this->association as $association)
179:         {
180:             $association->fill($model, $associative);
181:         }
182:         return $model;
183:     }
184: 
185:     public function debug($number, $config = array())
186:     {
187:         $tmp = array();
188:         $list = array();
189:         $this->init($config);
190:         if($header = $this->getCore()->next())
191:         {
192:             while($data = $this->getCore()->next())
193:             {
194:                 if($data == null) break;
195:                 $tmp = $data;
196:                 $model = $this->build($header, $data);
197:                 $number--;
198:                 if($number >= 0)
199:                     $list[] = $model;
200:             }
201:         }
202:         print_debug($header, $tmp, $list);
203:     }
204: }
API documentation generated by ApiGen