Index: branches/5.3.x/core/units/admin/admin_events_handler.php =================================================================== diff -u -N -r15483 -r15574 --- branches/5.3.x/core/units/admin/admin_events_handler.php (.../admin_events_handler.php) (revision 15483) +++ branches/5.3.x/core/units/admin/admin_events_handler.php (.../admin_events_handler.php) (revision 15574) @@ -1,6 +1,6 @@ Conn->Query('OPTIMIZE TABLE ' . $table_name); } } + + /** + * [SCHEDULED TASK] Pre-resizes images, used in templates + * + * @param kEvent $event + * @return void + * @access protected + */ + protected function OnPreResizeImages(kEvent $event) + { + $scheduled_task = $event->MasterEvent->getObject(); + /* @var $scheduled_task kDBItem */ + + $mass_resizer = new MassImageResizer(); + + // rules from scheduled task itself + $mass_resizer->addRules($scheduled_task->GetDBField('Settings')); + + // rules from all enabled themes + $sql = 'SELECT ImageResizeRules + FROM ' . $this->Application->getUnitOption('theme', 'TableName') . ' + WHERE Enabled = 1'; + $mass_resizer->addRules($this->Conn->GetCol($sql)); + + $mass_resizer->run(); + } } +/** + * Resizes multiple images according to given rules + */ +class MassImageResizer extends kBase { + /** + * Rules, that tell how images must be resized + * + * @var Array + * @access private + */ + private $_rules = Array (); + + /** + * Remembers, which fields of which unit are used + * + * @var Array + * @access private + */ + private $_unitFields = Array (); + + /** + * Remembers which fields of which units require which format + * + * @var Array + * @access private + */ + private $_unitFieldFormats = Array (); + + /** + * Adds more resize rules + * + * @param string|Array $rules + * @return void + * @access public + */ + public function addRules($rules) + { + $rules = (array)$rules; + + foreach ($rules as $rule) { + $rule = $this->_cleanup($rule); + + if ( $rule ) { + $this->_rules = array_merge($this->_rules, $rule); + } + } + } + + /** + * Normalizes given set of rules + * + * @param string $rules + * @return Array + * @access private + */ + private function _cleanup($rules) + { + $ret = explode("\n", str_replace(Array ("\r\n", "\r"), "\n", $rules)); + + return array_filter(array_map('trim', $ret)); + } + + /** + * Transforms rules given in raw format into 3D array of easily manageable rules + * + * @return void + * @access private + */ + private function _preProcessRules() + { + foreach ($this->_rules as $raw_rule) { + list ($prefix, $field, $format) = explode(':', $raw_rule, 3); + + if ( !isset($this->_unitFields[$prefix]) ) { + $this->_unitFields[$prefix] = Array (); + $this->_unitFieldFormats[$prefix] = Array (); + } + + $this->_unitFields[$prefix][] = $field; + + if ( !isset($this->_unitFieldFormats[$prefix][$field]) ) { + $this->_unitFieldFormats[$prefix][$field] = Array (); + } + + $this->_unitFieldFormats[$prefix][$field][] = $format; + } + } + + /** + * Performs resize operation + * + * @return void + * @access public + */ + public function run() + { + $this->_preProcessRules(); + + foreach ($this->_unitFields as $prefix => $fields) { + $sql = 'SELECT ' . implode(',', array_unique($fields)) . ' + FROM ' . $this->Application->getUnitOption($prefix, 'TableName'); + $unit_data = $this->Conn->GetIterator($sql); + + if ( !count($unit_data) ) { + continue; + } + + $object = $this->Application->recallObject($prefix . '.resize', null, Array ('skip_autoload' => true)); + /* @var $object kDBItem */ + + foreach ($unit_data as $field_values) { + foreach ($field_values as $field => $value) { + if ( !$value ) { + continue; + } + + $object->SetDBField($field, $value); + + foreach ($this->_unitFieldFormats[$prefix][$field] as $format) { + // will trigger image resize if needed + $object->GetField($field, $format); + } + } + } + } + } +} + + class UnitConfigDecorator { var $parentPath = Array ();