
Note a few things. First, we have 3 main directories: core / , assets / and _build / . Usually MODx Revo add-ons are divided into two directories: core / components / myextra / and assets / components / myextra / . The assets / components / directory contains only web-specific files — JavaScript, CSS, and so on. These are files that are publicly available on the Internet. All PHP files will be in the core / components / directory. The core / directory can be moved out of the webroot for additional security. These directories will be packed into our component's transport package in order to be able to quickly install from the “Package Management” section of the administrative part of the site. The _build / directory will not be packaged in a transport package. It is needed only at the stage of creating this package. This will be discussed in the last part of the lesson.$doodles = $modx->getService('doodles','Doodles',$modx->getOption('doodles.core_path',null,$modx->getOption('core_path').'components/doodles/').'model/doodles/',$scriptProperties); if (!($doodles instanceof Doodles)) return ''; $defaultDoodlesCorePath = $modx->getOption('core_path').'components/doodles/'; $doodlesCorePath = $modx->getOption('doodles.core_path',null,$defaultDoodlesCorePath); $doodles = $modx->getService('doodles','Doodles',$doodlesCorePath.'model/doodles/',$scriptProperties); <?php class Doodles { public $modx; public $config = array(); function __construct(modX &$modx,array $config = array()) { $this->modx =& $modx; $basePath = $this->modx->getOption('doodles.core_path',$config,$this->modx->getOption('core_path').'components/doodles/'); $assetsUrl = $this->modx->getOption('doodles.assets_url',$config,$this->modx->getOption('assets_url').'components/doodles/'); $this->config = array_merge(array( 'basePath' => $basePath, 'corePath' => $basePath, 'modelPath' => $basePath.'model/', 'processorsPath' => $basePath.'processors/', 'chunksPath' => $basePath.'elements/chunks/', 'jsUrl' => $assetsUrl.'js/', 'cssUrl' => $assetsUrl.'css/', 'assetsUrl' => $assetsUrl, 'connectorUrl' => $assetsUrl.'connector.php', ),$config); } } $dood = $modx->getService('doodles','Doodles',$modx->getOption('doodles.core_path',null,$modx->getOption('core_path').'components/doodles/').'model/doodles/',$scriptProperties); if (!($dood instanceof Doodles)) return ''; /* setup default properties */ $tpl = $modx->getOption('tpl',$scriptProperties,'rowTpl'); $sort = $modx->getOption('sort',$scriptProperties,'name'); $dir = $modx->getOption('dir',$scriptProperties,'ASC'); $output = ''; return $output; <?xml version="1.0" encoding="UTF-8"?> <model package="doodles" baseClass="xPDOObject" platform="mysql" defaultEngine="MyISAM"> <object class="Doodle" table="doodles" extends="xPDOSimpleObject"> <field key="name" dbtype="varchar" precision="255" phptype="string" null="false" default=""/> <field key="description" dbtype="text" phptype="string" null="false" default=""/> <field key="createdon" dbtype="datetime" phptype="datetime" null="true"/> <field key="createdby" dbtype="int" precision="10" attributes="unsigned" phptype="integer" null="false" default="0" /> <field key="editedon" dbtype="datetime" phptype="datetime" null="true"/> <field key="editedby" dbtype="int" precision="10" attributes="unsigned" phptype="integer" null="false" default="0" /> <aggregate alias="CreatedBy" class="modUser" local="createdby" foreign="id" cardinality="one" owner="foreign"/> <aggregate alias="EditedBy" class="modUser" local="editedby" foreign="id" cardinality="one" owner="foreign"/> </object> </model> <model package="doodles" baseClass="xPDOObject" platform="mysql" defaultEngine="MyISAM"> <aggregate alias="CreatedBy" class="modUser" local="createdby" foreign="id" cardinality="one" owner="foreign"/> <aggregate alias="EditedBy" class="modUser" local="editedby" foreign="id" cardinality="one" owner="foreign"/> <?php define('MODX_BASE_PATH', '../'); define('MODX_CORE_PATH', MODX_BASE_PATH . 'core/'); define('MODX_MANAGER_PATH', MODX_BASE_PATH . 'manager/'); define('MODX_CONNECTORS_PATH', MODX_BASE_PATH . 'connectors/'); define('MODX_ASSETS_PATH', MODX_BASE_PATH . 'assets/'); define('MODX_BASE_URL','/modx/'); define('MODX_CORE_URL', MODX_BASE_URL . 'core/'); define('MODX_MANAGER_URL', MODX_BASE_URL . 'manager/'); define('MODX_CONNECTORS_URL', MODX_BASE_URL . 'connectors/'); define('MODX_ASSETS_URL', MODX_BASE_URL . 'assets/'); <?php require_once dirname(__FILE__).'/build.config.php'; include_once MODX_CORE_PATH . 'model/modx/modx.class.php'; $modx= new modX(); $modx->initialize('mgr'); $modx->loadClass('transport.modPackageBuilder','',false, true); echo '<pre>'; /* used for nice formatting of log messages */ $modx->setLogLevel(modX::LOG_LEVEL_INFO); $modx->setLogTarget('ECHO'); $root = dirname(dirname(__FILE__)).'/'; $sources = array( 'model' => $root.'core/components/doodles/model/', 'schema_file' => $root.'core/components/doodles/model/schema/doodles.mysql.schema.xml', ); $manager= $modx->getManager(); $generator= $manager->getGenerator(); if (!is_dir($sources['model'])) { $modx->log(modX::LOG_LEVEL_ERROR,'Model directory not found!'); die(); } if (!file_exists($sources['schema_file'])) { $modx->log(modX::LOG_LEVEL_ERROR,'Schema file not found!'); die(); } $generator->parseSchema($sources['schema_file'],$sources['model']); echo '.'; exit(); 
$this->modx->addPackage('doodles',$this->config['modelPath']); <?php return include $file; [[!include? &file=`[[++doodles.core_path]]elements/snippets/snippet.doodles.php`]] $m = $modx->getManager(); $created = $m->createObjectContainer('Doodle'); return $created ? ' .' : ' .'; $tablexists = $modx->query("SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = '".$modx->getOption('dbname')."' AND table_name = '".$modx->getOption('table_prefix')."doodles'"); if(!$tablexists->fetch(PDO::FETCH_COLUMN)){ $m = $modx->getManager(); $created = $m->createObjectContainer('Doodle'); } $doodles = $modx->getCollection('Doodle'); $output = count($doodles); $doodle = $modx->newObject('Doodle'); $doodle->fromArray(array( 'name' => 'TestDoodle', 'description' => 'A test doodle' )); $doodle->save(); $c = $modx->newQuery('Doodle'); $c->sortby($sort,$dir); $doodles = $modx->getCollection('Doodle',$c); [[!include? &file=`[[++doodles.core_path]]elements/snippets/snippet.doodles.php`&sort=`name`&dir=`DESC`]] public function getChunk($name,$properties = array()) { $chunk = null; if (!isset($this->chunks[$name])) { $chunk = $this->_getTplChunk($name); if (empty($chunk)) { $chunk = $this->modx->getObject('modChunk',array('name' => $name)); if ($chunk == false) return false; } $this->chunks[$name] = $chunk->getContent(); } else { $o = $this->chunks[$name]; $chunk = $this->modx->newObject('modChunk'); $chunk->setContent($o); } $chunk->setCacheable(false); return $chunk->process($properties); } private function _getTplChunk($name,$postfix = '.chunk.tpl') { $chunk = false; $f = $this->config['chunksPath'].strtolower($name).$postfix; if (file_exists($f)) { $o = file_get_contents($f); $chunk = $this->modx->newObject('modChunk'); $chunk->set('name',$name); $chunk->setContent($o); } return $chunk; } <li><strong>[[+name]]</strong> - [[+description]]</li> foreach ($doodles as $doodle) { $doodleArray = $doodle->toArray(); $output .= $dood->getChunk($tpl,$doodleArray); } <?php $dood = $modx->getService('doodles','Doodles',$modx->getOption('doodles.core_path',null,$modx->getOption('core_path').'components/doodles/').'model/doodles/',$scriptProperties); if (!($dood instanceof Doodles)) return ''; /* setup default properties */ $tpl = $modx->getOption('tpl',$scriptProperties,'rowTpl'); $sort = $modx->getOption('sort',$scriptProperties,'name'); $dir = $modx->getOption('dir',$scriptProperties,'ASC'); $output = ''; $tablexists = $modx->query("SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = '".$modx->getOption('dbname')."' AND table_name = '".$modx->getOption('table_prefix')."doodles'"); if(!$tablexists->fetch(PDO::FETCH_COLUMN)){ $m = $modx->getManager(); $created = $m->createObjectContainer('Doodle'); } $c = $modx->newQuery('Doodle'); $c->sortby($sort,$dir); $doodles = $modx->getCollection('Doodle',$c); foreach ($doodles as $doodle) { $doodleArray = $doodle->toArray(); $output .= $dood->getChunk($tpl,$doodleArray); } return $output; 
Source: https://habr.com/ru/post/127722/
All Articles