Example #1
0
 /**
  * Swap position of modules.
  *
  * @todo Fix ugly $_POST hack.
  *
  * @throws Exception
  */
 public function showSwapModules()
 {
     $page = self::getPage(PageAccessPeer::PERM_CONTENT);
     $pageRevision = $page->getPageRevision();
     $a = (int) $_POST['a'];
     $b = (int) $_POST['b'];
     $modules = Curry_Array::objectsToArray($pageRevision->getModules(), null, 'getPageModuleId');
     $aIndex = array_search($a, $modules);
     $bIndex = array_search($b, $modules);
     if ($aIndex === false || $bIndex === false) {
         throw new Exception('Unable to find modules to swap.');
     }
     $modules[$aIndex] = $b;
     $modules[$bIndex] = $a;
     $list = new Curry_Backend_ContentList($this, $pageRevision);
     $_POST['item'] = array_map('json_encode', $modules);
     $list->sortItems(array());
 }
Example #2
0
 /**
  * Move column.
  *
  * @param string $name
  * @param int $position
  */
 public function moveColumn($name, $position)
 {
     $options = $this->columns[$name];
     Curry_Array::insertAt($this->columns, array($name => $options), $position);
 }
Example #3
0
 public static function toTwig(BaseObject $obj, $checkToTwig = true, $includeRelated = true, $includeVirtual = true, $includeI18n = true)
 {
     if ($checkToTwig && method_exists($obj, 'toTwig')) {
         return $obj->toTwig();
     }
     $tableMap = PropelQuery::from(get_class($obj))->getTableMap();
     $p = $obj->toArray(BasePeer::TYPE_PHPNAME);
     if ($includeRelated) {
         foreach ($tableMap->getRelations() as $relation) {
             if (in_array($relation->getType(), array(RelationMap::ONE_TO_MANY, RelationMap::MANY_TO_MANY))) {
                 $name = $relation->getPluralName();
                 $p[lcfirst($name)] = new Curry_OnDemand(function () use($obj, $name) {
                     return Curry_Array::objectsToArray($obj->{'get' . $name}(), null, array('Curry_Propel', 'toTwig'));
                 });
             } else {
                 $name = $relation->getName();
                 $p[lcfirst($name)] = new Curry_OnDemand(function () use($obj, $name) {
                     $rel = $obj->{'get' . $name}();
                     return $rel ? Curry_Propel::toTwig($rel) : null;
                 });
             }
         }
     }
     // Automatic URL
     $p['Url'] = new Curry_OnDemand(function () use($obj, $tableMap) {
         $params = array();
         foreach ($tableMap->getPrimaryKeys() as $pk) {
             $params[$pk->getName()] = $obj->{'get' . $pk->getPhpName()}();
         }
         return url(L(get_class($obj) . 'Url'), $params);
     });
     // Virtual columns
     if ($includeVirtual) {
         $p = array_merge($p, $obj->getVirtualColumns());
     }
     // I18n behavior columns
     if ($includeI18n && self::hasBehavior('i18n', $tableMap)) {
         $translation = $obj->getCurrentTranslation();
         $p = array_merge($p, $translation->toArray());
     }
     return $p;
 }
Example #4
0
 public function setOptions(array $options)
 {
     if (isset($options['actions'])) {
         foreach ($options['actions'] as $name => $action) {
             $this->addAction($name, $action);
         }
         unset($options['actions']);
     }
     if (isset($options['columns'])) {
         foreach ($options['columns'] as $name => $column) {
             $this->addColumn($name, $column);
         }
         unset($options['columns']);
     }
     foreach ($options as $k => $v) {
         if (method_exists($this, 'set' . $k)) {
             $this->{'set' . $k}($v);
         } else {
             if (property_exists($this, $k)) {
                 if (is_array($this->{$k})) {
                     Curry_Array::extend($this->{$k}, $v);
                 } else {
                     $this->{$k} = $v;
                 }
             } else {
                 continue;
             }
         }
         unset($options[$k]);
     }
     Curry_Array::extend($this->options, $options);
 }
Example #5
0
 /** {@inheritdoc} */
 public function toTwig()
 {
     return array('images' => Curry_Array::objectsToArray($this->images, null, array($this, 'getImageProperties')));
 }
 protected function getManagedfilesFromCache($ttl = 1800)
 {
     $cacheName = __CLASS__ . '_' . md5(__METHOD__);
     try {
         if (($ret = Curry_Core::$cache->load($cacheName)) === false) {
             trace('Rebuilding Managedfiles cache.');
             $colManagedfile = ManagedfileQuery::create()->find();
             $ret = Curry_Array::objectsToArray($colManagedfile, 'getFilepath');
             Curry_Core::$cache->save($ret, $cacheName, array(), $ttl);
         }
     } catch (Exception $e) {
         throw $e;
     }
     return $ret;
 }
Example #7
0
 /**
  * Get path details.
  *
  * @param string $path
  * @param string $parent
  * @param array $highlighted
  * @param array $selected
  * @return array
  */
 public function getPathInfo($path, $parent, $highlighted, $selected)
 {
     $p = array('Path' => $parent, 'IsRoot' => false, 'UploadUrl' => (string) url('', array('module', 'path' => $parent, 'action' => 'Upload')), 'files' => array());
     $realpath = realpath(Curry_Core::$config->curry->wwwPath . DIRECTORY_SEPARATOR . $path);
     $dit = new DirectoryIterator($realpath);
     foreach ($dit as $entry) {
         $name = $entry->getFilename();
         $vpath = $parent . '/' . $name;
         if ($name[0] === '.') {
             continue;
         }
         if (isset($_GET['filter'])) {
             if ($_GET['filter'] == 'folder' && !$entry->isDir()) {
                 continue;
             }
             if ($entry->isFile() && !self::matchFilter($_GET['filter'], $name)) {
                 continue;
             }
         }
         $p['files'][] = array('Name' => $name, 'IsHighlighted' => $name == $highlighted, 'IsSelected' => in_array($vpath, $selected), 'IsFolder' => $entry->isDir(), 'Icon' => $entry->isDir() ? 'icon_folder' : Curry_Util::getIconFromExtension(pathinfo($entry->getPathname(), PATHINFO_EXTENSION)), 'Url' => (string) url('', array('module', 'path' => $vpath)), 'Path' => $vpath);
     }
     Curry_Array::sortOn($p['files'], array('IsFolder', 'Name'), array(Curry_Array::SORT_PROPERTY | Curry_Array::SORT_REVERSE, Curry_Array::SORT_PROPERTY | Curry_Array::SORT_CASEINSENSITIVE));
     return $p;
 }
Example #8
0
 /**
  * Internal function to read and cascade all ModuleData objects.
  */
 protected function populateModuleData()
 {
     // get PageRevision ancestors
     $ancestors = array_reverse($this->getPageRevision()->getInheritanceChain(true));
     $ancestors = Curry_Array::objectsToArray($ancestors, null, 'getPageRevisionId');
     $ancestors = array_flip($ancestors);
     $keys = array();
     $depth = array();
     $lang = array();
     $moduleDatas = $this->getPageModule()->getModuleDatas();
     foreach ($moduleDatas as $key => $moduleData) {
         if ($moduleData->getLangcode() && $moduleData->getLangcode() !== $this->langcode) {
             continue;
         }
         if (!array_key_exists($moduleData->getPageRevisionId(), $ancestors)) {
             continue;
         }
         $keys[] = $key;
         $depth[] = $ancestors[$moduleData->getPageRevisionId()];
         $lang[] = $moduleData->getLangcode() ? 1 : 0;
     }
     $moduleDatas->clearIterator();
     // PropelCollection causes memory leak in php 5.3 unless we explicitly clear the iterator
     array_multisort($depth, $lang, $keys);
     foreach ($keys as $key) {
         $this->addData($moduleDatas[$key]);
     }
 }
Example #9
0
 /**
  * Restore database from file.
  * 
  * @todo Fix $maxExecutionTime.
  *
  * @param string|resource $file
  * @param array|null $tables
  * @param float $maxExecutionTime
  * @param int $continueLine
  * @param Curry_Backend|null $backend 
  * @return bool	True on success, false otherwise.
  */
 public static function restoreFromFile($file, $tables = null, $maxExecutionTime = 0, $continueLine = 0, Curry_Backend $backend = null)
 {
     global $CURRY_DATABASE_RESTORE;
     $CURRY_DATABASE_RESTORE = true;
     $fp = is_string($file) ? fopen($file, "r") : $file;
     $t = microtime(true);
     $total = 0;
     $skipped = 0;
     $failed = 0;
     $session = new Zend_Session_Namespace(__CLASS__);
     $con = Propel::getConnection();
     $con->beginTransaction();
     $adapter = Propel::getDB();
     if ($adapter instanceof DBMySQL) {
         $con->exec("SET foreign_key_checks = 0");
     }
     // Read header
     $firstline = stream_get_line($fp, self::MAX_LINE_LENGTH, "\n");
     $header = json_decode($firstline, true);
     if (is_array($header) && isset($header['header'])) {
         $header = $header['header'];
         // Check header version
         $version = isset($header['version']) ? (int) $header['version'] : 0;
         if ($version > self::VERSION) {
             throw new Exception('Unsupported database version. The file you are trying to restore from is from a newer version of currycms.');
         }
         // Check page version
         $pageVersion = isset($header['page-version']) ? (int) $header['page-version'] : 0;
         if ($pageVersion > Page::VERSION) {
             throw new Exception('Unsupported page version. The file you are trying to restore from is from a newer version of currycms.');
         }
         if ($backend) {
             $backend->addMessage("Restoring from " . $header['date']);
         }
         if ($pageVersion !== Page::VERSION) {
             if ($backend) {
                 $backend->addMessage("Migrating data from version {$pageVersion} to " . Page::VERSION, Curry_Backend::MSG_WARNING);
             }
             Page::preMigrate($pageVersion);
         }
     } else {
         throw new Exception('Invalid header');
     }
     // Empty tables
     if ($continueLine == 0) {
         foreach (Curry_Propel::getModels() as $classes) {
             foreach ($classes as $table) {
                 try {
                     if (is_array($tables) && !in_array($table, $tables)) {
                         continue;
                     }
                     if (!method_exists($table, 'delete')) {
                         if ($backend) {
                             $backend->addMessage("Skipping read-only table: {$table}", Curry_Backend::MSG_WARNING);
                         }
                         continue;
                     }
                     $tableName = PropelQuery::from($table)->getTableMap()->getName();
                     // use basePeer to avoid foreign key emulation in Normal peer class
                     BasePeer::doDeleteAll($tableName, $con);
                 } catch (Exception $e) {
                     throw new Exception('Unable to empty table ' . $table . ': ' . $e->getMessage());
                 }
             }
         }
         if ($backend) {
             $backend->addMessage("Cleared tables in " . round(microtime(true) - $t, 2) . "s");
         }
         $t = microtime(true);
     } else {
         $total = $session->total;
         $skipped = $session->skipped;
         $failed = $session->failed;
         if ($backend) {
             $backend->addMessage("Continuing from line {$continueLine}.");
         }
         for ($i = 0; $i < $continueLine; ++$i) {
             stream_get_line($fp, self::MAX_LINE_LENGTH, "\n");
         }
     }
     $currentTable = null;
     $buffer = array();
     while (!feof($fp)) {
         // Read line
         $data = json_decode(stream_get_line($fp, self::MAX_LINE_LENGTH, "\n"), true);
         ++$total;
         if (is_array($data) && isset($data['table'])) {
             if (is_array($tables) && !in_array($data['table'], $tables) || !method_exists($data['table'], 'delete')) {
                 ++$skipped;
                 continue;
             }
             // Verify columns for new table
             if ($data['table'] !== $currentTable && $currentTable !== null && $backend) {
                 $backend->addMessage('Restoring rows for table ' . $data['table']);
                 $columns = Curry_Array::objectsToArray(PropelQuery::from($data['table'])->getTableMap()->getColumns(), null, 'getPhpName');
                 $added = array_diff($columns, array_keys($data['values']));
                 $removed = array_diff(array_keys($data['values']), $columns);
                 if (count($added)) {
                     $backend->addMessage('New column(s): ' . join(', ', $added), Curry_Backend::MSG_WARNING);
                 }
                 if (count($removed)) {
                     $backend->addMessage('Removed column(s): ' . join(', ', $removed), Curry_Backend::MSG_WARNING);
                 }
             }
             // Flush buffer when changing tables
             if ($data['table'] !== $currentTable || count($buffer) >= self::MULTIINSERT_MAXBUFFER) {
                 if ($currentTable !== null && count($buffer)) {
                     Curry_Propel::doMultiInsert($currentTable, $buffer);
                 }
                 $currentTable = $data['table'];
                 $buffer = array();
             }
             // Migrate data
             if ($pageVersion !== Page::VERSION) {
                 if (!Page::migrateData($data['table'], $data['values'], $pageVersion)) {
                     continue;
                 }
             }
             $buffer[] = $data['values'];
         } else {
             if ($backend) {
                 $backend->addMessage('Unable to read data on line ' . $total, Curry_Backend::MSG_ERROR);
             }
             ++$failed;
         }
         // check execution time
         if ($maxExecutionTime && Curry_Core::getExecutionTime() > $maxExecutionTime) {
             if ($currentTable !== null && count($buffer)) {
                 Curry_Propel::doMultiInsert($currentTable, $buffer);
             }
             $session->total = $total;
             $session->skipped = $skipped;
             $session->failed = $failed;
             $params = array('module' => 'Curry_Backend_Database', 'view' => 'ContinueRestore', 'file' => $file, 'tables' => $tables, 'line' => $total, 'max_execution_time' => $maxExecutionTime);
             url('', $params)->redirect(302, true);
         }
     }
     // Flush buffer
     if ($currentTable !== null && count($buffer)) {
         Curry_Propel::doMultiInsert($currentTable, $buffer);
     }
     if ($pageVersion !== Page::VERSION) {
         Page::postMigrate($pageVersion);
     }
     if ($adapter instanceof DBMySQL) {
         $con->exec("SET foreign_key_checks = 1");
     }
     $con->commit();
     $CURRY_DATABASE_RESTORE = false;
     if ($backend) {
         if ($skipped) {
             $backend->addMessage("Skipped {$skipped} rows");
         }
         if ($failed) {
             $backend->addMessage("Failed to add {$failed} rows", Curry_Backend::MSG_ERROR);
         }
         $backend->addMessage("Added " . ($total - $skipped - $failed) . " / {$total} rows in " . round(microtime(true) - $t, 2) . "s", !$failed ? Curry_Backend::MSG_SUCCESS : Curry_Backend::MSG_ERROR);
     }
     if (is_string($file)) {
         fclose($fp);
     }
     return !$failed;
 }
Example #10
0
 /**
  * Load configuration.
  *
  * @param string|array|null $config
  * @param bool $loadUserConfig
  * @return array
  */
 private static function getConfig($config, $loadUserConfig = true)
 {
     $userConfig = array();
     $configPath = null;
     $projectPath = null;
     if (is_string($config)) {
         // Load configuration from file
         $configPath = realpath($config);
         if (!$configPath) {
             throw new Exception('Configuration file not found: ' . $config);
         }
         $projectPath = Curry_Util::path(true, dirname($configPath), '..');
         if ($loadUserConfig) {
             $userConfig = (require $configPath);
         }
     } else {
         if (is_array($config)) {
             // Configuration provided by array
             if ($loadUserConfig) {
                 $userConfig = $config;
             }
         } else {
             if ($config === null || $config === false) {
                 // Skip configuration
             } else {
                 throw new Exception('Unknown configuration format.');
             }
         }
     }
     // Attempt to find project path
     if (!$projectPath) {
         $projectPath = Curry_Util::path(true, getcwd(), '..', 'cms');
     }
     if (!$projectPath) {
         $projectPath = Curry_Util::path(true, getcwd(), 'cms');
     }
     // default config
     $config = array('curry' => array('name' => "untitled", 'baseUrl' => '/', 'adminEmail' => "*****@*****.**", 'divertOutMailToAdmin' => false, 'statistics' => false, 'applicationClass' => 'Curry_Application', 'defaultGeneratorClass' => 'Curry_PageGenerator_Html', 'forceDomain' => false, 'revisioning' => false, 'umask' => 02, 'liveEdit' => true, 'secret' => 'SECRET', 'errorNotification' => false, 'errorReporting' => E_ALL ^ E_NOTICE, 'generator' => 'Curry_PageGenerator_Html', 'internalEncoding' => 'utf-8', 'outputEncoding' => 'utf-8', 'basePath' => Curry_Util::path(true, dirname(__FILE__), '..', '..'), 'projectPath' => $projectPath, 'wwwPath' => getcwd(), 'configPath' => $configPath, 'cache' => array('method' => 'auto'), 'mail' => array('method' => 'sendmail'), 'log' => array('method' => 'none'), 'maintenance' => array('enabled' => false), 'defaultEditor' => 'tinyMCE', 'migrationVersion' => self::MIGRATION_VERSION, 'pageCache' => true, 'autoPublish' => false, 'developmentMode' => false, 'autoUpdateIndex' => false, 'password' => array('algorithm' => PASSWORD_BCRYPT, 'options' => array('cost' => 10)), 'debug' => array('moduleTimeLimit' => 0.5, 'moduleCpuLimit' => 0.25, 'moduleMemoryLimit' => 5 * 1024 * 1024, 'moduleSqlLimit' => 8)));
     if ($loadUserConfig) {
         Curry_Array::extend($config, $userConfig);
     }
     // Fix base url
     $config['curry']['baseUrl'] = url($config['curry']['baseUrl'])->getAbsolute();
     if (!$config['curry']['projectPath']) {
         throw new Exception('Project path could not be found, please use a configuration file to specify the path');
     }
     $secondaryConfig = array('curry' => array('vendorPath' => Curry_Util::path($config['curry']['basePath'], 'vendor'), 'tempPath' => self::getTempPath($config['curry']['projectPath']), 'trashPath' => Curry_Util::path($config['curry']['projectPath'], 'data', 'trash'), 'hooksPath' => Curry_Util::path($config['curry']['projectPath'], 'config', 'hooks.php'), 'autoBackup' => $config['curry']['developmentMode'] ? 0 : 86400, 'propel' => array('conf' => Curry_Util::path($config['curry']['projectPath'], 'config', 'propel.php'), 'projectClassPath' => Curry_Util::path($config['curry']['projectPath'], 'propel', 'build', 'classes')), 'template' => array('root' => Curry_Util::path($config['curry']['projectPath'], 'templates'), 'options' => array('debug' => (bool) $config['curry']['developmentMode'], 'cache' => Curry_Util::path($config['curry']['projectPath'], 'data', 'cache', 'templates'), 'base_template_class' => 'Curry_Twig_Template')), 'backend' => array('placeholderExclude' => array(), 'theme' => 'vindaloo', 'loginCookieExpire' => 31536000, 'loginTokenExpire' => 31536000), 'mail' => array('options' => array()), 'domainMapping' => array('enabled' => false, 'domains' => array())));
     $config = Curry_Array::extend($secondaryConfig, $config);
     return $config;
 }
Example #11
0
 /**
  * Import data into table from CSV file.
  *
  * @todo Add support for propel advanced columns (array).
  *
  * @throws Exception
  */
 public function showImport()
 {
     $modelClass = $_GET['table'];
     $tableMap = PropelQuery::from($modelClass)->getTableMap();
     $columnOptions = Curry_Array::objectsToArray($tableMap->getColumns(), 'getName', 'getPhpName');
     $pks = array();
     foreach ($tableMap->getColumns() as $column) {
         if ($column->isPrimaryKey()) {
             $pks[] = $column->getName();
         }
     }
     $form = new Curry_Form(array('method' => 'post', 'action' => url('', $_GET), 'elements' => array('file' => array('file', array('label' => 'CSV File', 'valueDisabled' => true)), 'mode' => array('select', array('label' => 'Mode', 'multiOptions' => array(self::IMPORT_REPLACE => 'Replace', self::IMPORT_APPEND => 'Append', self::IMPORT_UPDATE => 'Update', self::IMPORT_UPDATE_OR_INSERT => 'Update or insert'))), 'skip_first' => array('checkbox', array('label' => 'Skip first line', 'value' => true)), 'columns' => array('text', array('label' => 'Columns in file', 'value' => join(',', array_keys($columnOptions)))), 'use_columns' => array('multiselect', array('label' => 'Columns to use', 'multiOptions' => $columnOptions, 'value' => array_keys($columnOptions), 'size' => min(10, count($columnOptions)))), 'delimiter' => array('text', array('label' => 'Delimiter', 'value' => ',')), 'enclosure' => array('text', array('label' => 'Enclosure', 'value' => '"')), 'escape' => array('text', array('label' => 'Escape', 'value' => '\\')), 'null_value' => array('text', array('label' => 'Null', 'value' => 'Ø')), 'submit' => array('submit', array('label' => 'Import')))));
     $fields = array_slice(array_keys($form->getElements()), 2, -1);
     $form->addDisplayGroup($fields, 'advanced', array('legend' => 'Advanced options', 'class' => 'advanced', 'order' => 2));
     $this->addMainContent('<h2>Import: ' . htmlspecialchars($modelClass) . '</h2>');
     if (isPost() && $form->isValid($_POST)) {
         $mode = $form->mode->getValue();
         $skipFirst = $form->skip_first->getValue();
         $columns = explode(',', $form->columns->getValue());
         $useColumns = $form->use_columns->getValue();
         $delimiter = $form->delimiter->getValue();
         $enclosure = $form->enclosure->getValue();
         $escape = $form->escape->getValue();
         $nullValue = $form->null_value->getValue();
         if (!$form->file->isUploaded()) {
             throw new Exception('Error when uploading file.');
         }
         // Check for non-existent columns
         $nonExistent = array_filter(array_diff($columns, array_keys($columnOptions)));
         if (count($nonExistent)) {
             throw new Exception('Unknown column in column list: ' . join(', ', $nonExistent));
         }
         // Open csv file
         $fileInfo = $form->file->getFileInfo();
         $fp = fopen($fileInfo['file']['tmp_name'], "r");
         if (!$fp) {
             throw new Exception('Unable to open file');
         }
         // Wrap in transaction
         $deleted = 0;
         $updated = 0;
         $inserted = 0;
         $con = Propel::getConnection(PropelQuery::from($modelClass)->getDbName());
         $con->beginTransaction();
         try {
             // Replace will empty the table
             if ($mode === self::IMPORT_REPLACE) {
                 $deleted = PropelQuery::from($modelClass)->deleteAll();
             }
             // Read csv lines
             while (($data = fgetcsv($fp, 0, $delimiter, $enclosure, $escape)) !== false) {
                 if (count($data) !== count($columns)) {
                     throw new Exception('Invalid column count ' . count($data) . ', expected ' . count($columns));
                 }
                 if ($skipFirst) {
                     $skipFirst = false;
                     continue;
                 }
                 $data = array_combine($columns, $data);
                 $pkData = array();
                 // Check for null values and collect primary key
                 foreach ($data as $k => $v) {
                     if ($v === $nullValue) {
                         $data[$k] = $v = null;
                     }
                     if (in_array($k, $pks)) {
                         $pkData[$k] = $v;
                     }
                 }
                 $obj = null;
                 if ($mode === self::IMPORT_UPDATE || $mode === self::IMPORT_UPDATE_OR_INSERT) {
                     // attempt to find existing object using pk
                     if (count($pkData) === count($pks)) {
                         $obj = new $modelClass();
                         $obj->fromArray($pkData, BasePeer::TYPE_FIELDNAME);
                         $obj = PropelQuery::from($modelClass)->findPk($obj->getPrimaryKey());
                     }
                     if (!$obj && $mode === self::IMPORT_UPDATE_OR_INSERT) {
                         // not found, create new
                         $obj = new $modelClass();
                     }
                 } else {
                     // REPLACE, APPEND
                     $obj = new $modelClass();
                 }
                 // Remove unused columns
                 foreach ($data as $k => $v) {
                     if (!in_array($k, $useColumns)) {
                         unset($data[$k]);
                     }
                 }
                 if ($obj) {
                     // Unset primary key columns in data when appending
                     if ($mode === self::IMPORT_APPEND) {
                         foreach ($pks as $pk) {
                             if (array_key_exists($pk, $data)) {
                                 unset($data[$pk]);
                             }
                         }
                     }
                     $obj->fromArray($data, BasePeer::TYPE_FIELDNAME);
                     if ($obj->isNew()) {
                         // allows insert of custom primary key
                         BasePeer::doInsert($obj->buildCriteria(), $con);
                         ++$inserted;
                     } else {
                         $updated += $obj->save();
                     }
                 }
             }
             $con->commit();
         } catch (Exception $e) {
             $con->rollBack();
             throw $e;
         }
         if ($deleted) {
             $this->addMessage('Deleted: ' . $deleted);
         }
         if ($inserted) {
             $this->addMessage('Inserted: ' . $inserted);
         }
         if ($updated) {
             $this->addMessage('Updated: ' . $updated);
         }
         $this->addMessage('All done.', self::MSG_SUCCESS);
     } else {
         $this->addMainContent($form);
     }
 }
Example #12
0
 protected static function getBasePageSelect(Page $page = null, $basePageId = null, $advanced = false)
 {
     $pages = array('' => '[ Do not inherit ]');
     $templatePage = Curry_Backend_Page::getTemplatePage();
     if ($templatePage) {
         $pages['Templates'] = PagePeer::getSelect($templatePage);
         if ($advanced) {
             $pages['Pages'] = array_diff_key(PagePeer::getSelect(), $pages['Templates']);
         } else {
             if ($basePageId && !array_key_exists($basePageId, $pages['Templates'])) {
                 $basePage = PageQuery::create()->findPk($basePageId);
                 $pages['Pages'] = array($basePageId => $basePage ? $basePage->getName() : '<Unknown>');
             }
         }
     } else {
         $pages += PagePeer::getSelect();
     }
     $dependantPages = array();
     if ($page) {
         $dependantPages = Curry_Array::objectsToArray($page->getDependantPages(), null, 'getPageId');
         $dependantPages[] = $page->getPageId();
     }
     $pageSelect = array('select', array('label' => 'Base page', 'multiOptions' => $pages, 'value' => $basePageId, 'description' => 'The page which content and templates will be inherited from.', 'disable' => $dependantPages, 'onchange' => "\$(this).closest('form').find('.base-preview').attr('src', '" . url('', array('module', 'view' => 'BasePreview')) . "&page_id=' + \$(this).val());"));
     $imageElement = array('rawHtml', array('label' => 'Preview', 'value' => '<img src="' . url('', array('module', 'view' => 'BasePreview', 'page_id' => $basePageId)) . '" class="base-preview" />'));
     return array($pageSelect, $imageElement);
 }
Example #13
0
 /** {@inheritdoc} */
 public function toTwig()
 {
     return array('links' => Curry_Array::objectsToArray($this->links, null, array($this, 'getLinkProperties')));
 }
Example #14
0
 /**
  * Upgrade package.
  * 
  * @param string $name
  * @param bool $simulate
  * @return bool
  */
 public static function upgradePackage($name, $simulate = false)
 {
     $package = Curry_PackageManager::getPackage($name);
     if (!$package) {
         return false;
     }
     $installedPackage = PackageQuery::create()->findPk($name);
     if (!$installedPackage) {
         return false;
     }
     $oldPackage = Curry_PackageManager::getPackage($installedPackage->getName(), $installedPackage->getVersion());
     if (!$oldPackage) {
         return false;
     }
     // make sure we are trying to install a newer package
     if (version_compare($package['version'], $installedPackage->getVersion()) <= 0) {
         return false;
     }
     // run preUpgrade task
     if (!$simulate && !self::execTask($package, 'preUpgrade', true, array('fromVersion' => $installedPackage->getVersion(), 'toVersion' => $package['version']))) {
         return false;
     }
     $diff = '/usr/bin/diff';
     $patch = '/usr/bin/patch';
     $installedFiles = Curry_Array::objectsToArray($installedPackage->getPackageFiles(), 'getFilename');
     $tar = new Curry_Archive($package['source']);
     $oldTar = new Curry_Archive($oldPackage['source']);
     $tempFile = tempnam('/tmp', 'curry');
     foreach ($tar as $tarFile) {
         $file = $tarFile->getPathname();
         try {
             $target = PackageFile::mapFile($file);
         } catch (Exception $e) {
             self::log('Skipping: ' . $file);
             continue;
         }
         // create directory
         if ($tarFile->isDir()) {
             if (!$simulate) {
                 if (!file_exists($target)) {
                     mkdir($target, 0777, true);
                 }
             }
             continue;
         }
         // file is already installed?
         if (array_key_exists($file, $installedFiles)) {
             $packageFile = $installedFiles[$file];
             unset($installedFiles[$file]);
             // do not remove this file
             // read checksum of new file
             if ($tarFile->getSize() > 102400) {
                 $tarFile->extract($tempFile);
                 $newChecksum = sha1_file($tempFile);
             } else {
                 $newChecksum = sha1($tarFile->getContents());
             }
             if (!file_exists($target)) {
                 // Installed file is missing
                 self::log('Re-installing: ' . $file, Curry_Backend::MSG_WARNING);
                 if (!$simulate) {
                     $tarFile->extract($target);
                     $packageFile->setChecksum($newChecksum);
                     $packageFile->save();
                 }
             } else {
                 if ($packageFile->getChecksum() == $newChecksum) {
                     // File hasnt changed in package, so skip it
                     self::log('Unchanged: ' . $file);
                 } else {
                     if ($packageFile->fileIsModified()) {
                         // Installed file was modified
                         self::log('Updating modified: ' . $file, Curry_Backend::MSG_SUCCESS);
                         if (!$simulate) {
                             $backupFile = $packageFile->backup();
                             $tarFile->extract($target);
                             $packageFile->setChecksum($newChecksum);
                             $packageFile->save();
                             // Diff
                             $oldTar->getFile($file)->extract($tempFile);
                             $command = $diff . ' -u ' . escapeshellarg($tempFile) . ' ' . escapeshellarg($backupFile);
                             $p = `{$command}`;
                             // Patch file
                             file_put_contents($tempFile, $p);
                             $command = $patch . ' ' . escapeshellarg($target) . ' ' . escapeshellarg($tempFile);
                             $d = `{$command}`;
                         }
                     } else {
                         // file is not modified so we should be able to just delete it and install the new one
                         self::log('Updating: ' . $file);
                         if (!$simulate) {
                             unlink($target);
                             $tarFile->extract($target);
                             $packageFile->setChecksum(sha1_file($target));
                             $packageFile->save();
                         }
                     }
                 }
             }
         } else {
             self::log('Adding: ' . $file, Curry_Backend::MSG_SUCCESS);
             if (!$simulate) {
                 if (file_exists($target)) {
                     // backup file before overwriting
                     $backupTarget = $target . "." . date("Ymd_His");
                     if (file_exists($backupTarget)) {
                         throw new Exception('Unable to backup existing file.');
                     }
                     rename($target, $backupTarget);
                 }
                 $tarFile->extract($target);
                 $packageFile = new PackageFile();
                 $packageFile->setPackage($installedPackage);
                 $packageFile->setFilename($file);
                 $packageFile->setChecksum(sha1_file($target));
                 $packageFile->save();
             }
         }
     }
     // remove remaining files in $installedFiles
     foreach ($installedFiles as $installedFile) {
         self::log('Remove: ' . $installedFile->getFilename(), Curry_Backend::MSG_WARNING);
         if (!$simulate) {
             if (!$installedFile->fileIsModified()) {
                 $installedFile->backup();
             } else {
                 unlink($installedFile->getRealpath());
             }
             $installedFile->delete();
         }
     }
     if (!$simulate) {
         $installedPackage->setVersion($package['version']);
         $installedPackage->save();
     }
     if (!$simulate) {
         self::execTaskWithReload($package, 'postUpgrade', true, array('fromVersion' => $installedPackage->getVersion(), 'toVersion' => $package['version']));
     }
     self::$installed = null;
     return true;
 }
Example #15
0
 public function sortItems($params)
 {
     ModuleSortorderQuery::create()->filterByPageRevision($this->pageRevision)->delete();
     $wrappers = $this->pageRevision->getPageModuleWrappers();
     $unsortedIds = Curry_Array::objectsToArray($wrappers, false, 'getPageModuleId');
     $wrapperById = Curry_Array::objectsToArray($wrappers, 'getPageModuleId');
     // Get primary keys
     $items = $_POST['item'];
     if (!is_array($items)) {
         throw new Exception('Expected array POST variable `item`.');
     }
     $sortedIds = array();
     foreach ($items as $item) {
         $pk = json_decode($item, true);
         if ($pk === null) {
             throw new Exception('Invalid primary key for item: ' . $item);
         }
         if (!array_key_exists($pk, $wrapperById)) {
             throw new Exception('Module not found when sorting');
         }
         $sortedIds[] = $pk;
     }
     if ($sortedIds !== $unsortedIds) {
         foreach ($wrappers as $wrapper) {
             $sortorder = $wrapper->getSortorder(true);
             $sortorder->insertAtBottom();
             $sortorder->save();
         }
         $pks = array();
         foreach ($sortedIds as $id) {
             $pks[] = array($id, $this->pageRevision->getPageRevisionId());
         }
         Curry_Propel::sortableReorder($pks, 'ModuleSortorder');
     }
     $this->pageRevision->setUpdatedAt(time());
     $this->pageRevision->save();
 }
Example #16
0
 /**
  * Run flexigrid commands.
  */
 protected function runCommands()
 {
     // execute commands before we return the list
     if (!isset($_POST['cmd'])) {
         return;
     }
     switch ($_POST['cmd']) {
         case 'delete':
             if (isset($_POST['id'])) {
                 $id = $_POST['id'];
                 PropelQuery::from($this->modelClass)->findPks(is_array($id) ? $id : array($id))->delete();
             }
             break;
         case 'reorder':
             if (isset($_POST['reorder'])) {
                 $reorder = array();
                 parse_str($_POST['reorder'], $reorder);
                 if (isset($this->columns['sort_index'])) {
                     // OLD SORTING METHOD
                     // get objects
                     $objs = array();
                     foreach ($reorder['row'] as $rowId) {
                         $objs[] = call_user_func(array($this->modelClass . 'Peer', 'retrieveByPk'), $rowId);
                     }
                     // get sort indices
                     $sortIndices = array();
                     foreach ($objs as $obj) {
                         $sortIndices[] = $obj->getSortIndex();
                     }
                     // sort our indices
                     sort($sortIndices);
                     // set new sort indices
                     $i = 0;
                     foreach ($objs as $obj) {
                         $obj->setSortIndex($sortIndices[$i++]);
                         $obj->save();
                     }
                 } else {
                     // get ranks
                     $objs = PropelQuery::from($this->modelClass)->findPks($reorder['row']);
                     // move all objs with null rank to the bottom
                     $ranks = array();
                     foreach ($objs as $obj) {
                         if ($obj->getRank() === null) {
                             $obj->insertAtBottom();
                             $obj->save();
                         }
                         $ranks[] = $obj->getRank();
                     }
                     // check for duplicate ranks
                     $dups = array_filter(array_count_values($ranks), create_function('$a', 'return $a > 1;'));
                     if ($dups) {
                         // Duplicate indices, move to bottom
                         $tmp = $this->tableMap->getBehaviors();
                         $scope = null;
                         if (strtolower($tmp['sortable']['use_scope']) == 'true') {
                             // need scope, find from one object
                             $scope = PropelQuery::from($this->modelClass)->findPk(reset($reorder['row']))->getScopeValue();
                         }
                         foreach ($dups as $rank => $f) {
                             $objs = PropelQuery::from($this->modelClass)->filterByRank($rank, $scope)->offset(1)->find();
                             foreach ($objs as $obj) {
                                 $obj->insertAtBottom();
                                 $obj->save();
                             }
                         }
                         $ranks = Curry_Array::objectsToArray($objs, null, 'getRank');
                     }
                     // sort our indices
                     sort($ranks);
                     // reorder
                     PropelQuery::from($this->modelClass)->reorder(array_combine($reorder['row'], $ranks));
                 }
             }
             break;
     }
 }
Example #17
0
 /**
  * Render a page and return content.
  *
  * @param array $vars
  * @param array $options
  * @return string
  */
 public function render(array $vars = array(), array $options = array())
 {
     $twig = Curry_Twig_Template::getSharedEnvironment();
     // Todo: Rename curry to app?
     $appVars = Curry_Application::getInstance()->getGlobalVariables();
     if (isset($vars['curry'])) {
         Curry_Array::extend($appVars, $vars['curry']);
     }
     $vars['curry'] = Curry_Array::extend($appVars, $this->getGlobals());
     foreach ($vars as $k => $v) {
         $twig->addGlobal($k, $v);
     }
     $moduleContent = $this->generate($options);
     if (isset($options['pageModuleId'])) {
         $pageModuleId = $options['pageModuleId'];
         $pageModuleWrappers = $this->getPageModuleWrappers();
         if (isset($pageModuleWrappers[$pageModuleId])) {
             $pageModuleWrapper = $pageModuleWrappers[$pageModuleId];
             return $moduleContent[$pageModuleWrapper->getTarget()];
         } else {
             throw new Exception('PageModule with id = ' . $pageModuleId . ' not found on page.');
         }
     }
     $template = $this->getTemplateObject();
     return $this->renderTemplate($template, $moduleContent);
 }
Example #18
0
 protected function getFileAccessList($query = null, $formOptions = array(), $listOptions = array())
 {
     if (!$query) {
         $query = FilebrowserAccessQuery::create();
     }
     $o = array('columnElements' => array('path' => array('filebrowser', array('filebrowserOptions' => array('local' => false), 'finderOptions' => array('type' => 'folder'), 'allowEmpty' => false, 'validators' => array(array('callback', false, array('callback' => array($this, 'validatePath')))))), 'write' => array('select', array('label' => 'Access', 'multiOptions' => array('' => 'Read', '1' => 'Read / Write')))));
     Curry_Array::extend($o, $formOptions);
     $modelForm = new Curry_Form_ModelForm('FilebrowserAccess', $o);
     $modelForm->path->getValidator('callback')->setMessage("Invalid permissions to path '%value%'");
     $form = new Curry_ModelView_Form($modelForm);
     $o = array('title' => 'File permissions', 'modelForm' => $form, 'columns' => array('write' => array('label' => 'Access', 'display' => '{{Write?"Read / Write":"Read"}}')));
     Curry_Array::extend($o, $listOptions);
     $list = new Curry_ModelView_List($query, $o);
     return $list;
 }
Example #19
0
 /**
  * Restore page from serialized object.
  *
  * @param Page $page
  * @param array $pageData
  * @param array $pageMap
  */
 public static function restorePage(Page $page, array $pageData, array $pageMap)
 {
     unset($pageData['id']);
     unset($pageData['uid']);
     unset($pageData['name']);
     unset($pageData['url']);
     if ($pageData['redirect_page']) {
         $page->setRedirectPage(self::findPageByMap($pageData['redirect_page'], $pageMap));
     }
     $page->fromArray($pageData, BasePeer::TYPE_FIELDNAME);
     $page->save();
     if (!$pageData['revision']) {
         return;
     }
     // Create new revision
     $pr = new PageRevision();
     $pr->setPage($page);
     $pr->setBasePage(self::findPageByMap($pageData['revision']['base_page'], $pageMap));
     $pr->fromArray($pageData['revision'], BasePeer::TYPE_FIELDNAME);
     $pr->setDescription('Copied');
     $page->setWorkingPageRevision($pr);
     $page->save();
     // Add module data...
     $order = array();
     $parentPages = Curry_Array::objectsToArray($page->getWorkingPageRevision()->getInheritanceChain(true), null, 'getPageId');
     $inheritedModules = $pr->getModules();
     foreach ($pageData['revision']['modules'] as $module) {
         $pm = null;
         if (!$module['is_inherited']) {
             $pm = PageModuleQuery::create()->findOneByUid($module['uid']);
             if ($pm && !in_array($pm->getPageId(), $parentPages)) {
                 // Page module exists, but is not in our "inheritance chain"
                 // Give the module a new unique-id, and create the module here
                 $pm->setUid(Curry_Util::getUniqueId());
                 $pm->save();
                 $pm = null;
             }
         } else {
             // find inherited module
             foreach ($inheritedModules as $inheritedModule) {
                 if ($inheritedModule->getUid() == $module['uid']) {
                     $pm = $inheritedModule;
                     break;
                 }
             }
         }
         if (!$pm) {
             $pm = new PageModule();
             $pm->setPage($page);
             $pm->fromArray($module, BasePeer::TYPE_FIELDNAME);
         }
         if (!$module['is_inherited']) {
             $rm = new RevisionModule();
             $rm->setPageModule($pm);
             $rm->setPageRevision($pr);
         }
         foreach ($module['datas'] as $moduleData) {
             $md = new ModuleData();
             $md->setPageModule($pm);
             $md->setPageRevision($pr);
             $md->fromArray($moduleData, BasePeer::TYPE_FIELDNAME);
         }
         $order[] = $pm->getUid();
     }
     $pr->save();
     $modules = Curry_Array::objectsToArray($pr->getModules(), 'getUid');
     if (array_keys($modules) !== $order) {
         foreach ($order as $uid) {
             $module = $modules[$uid];
             $sortorder = new ModuleSortorder();
             $sortorder->setPageModule($module);
             $sortorder->setPageRevision($pr);
             $sortorder->insertAtBottom();
             $sortorder->save();
         }
     }
     $page->setActivePageRevision($pr);
     $page->save();
 }
Example #20
0
 /** {@inheritdoc} */
 public function toTwig()
 {
     $flashvars = array();
     foreach ($this->flashvars as $flashvar) {
         $flashvars[$flashvar['name']] = $flashvar['value'];
     }
     if ($this->module) {
         $moduleTemplate = $this->template ? Curry_Twig_Template::loadTemplate($this->template) : null;
         $flashvars[$this->moduleFlashvar] = $this->module->showFront($moduleTemplate);
     }
     if (in_array("get", $this->addToFlashvars)) {
         Curry_Array::extend($flashvars, $_GET);
     }
     if (in_array("post", $this->addToFlashvars)) {
         Curry_Array::extend($flashvars, $_POST);
     }
     if (in_array("cookie", $this->addToFlashvars)) {
         Curry_Array::extend($flashvars, $_COOKIE);
     }
     $options = array();
     $options['expressInstall'] = $this->expressInstall;
     $options['target'] = $this->target;
     $options['attributes'] = count($this->attributes) ? $this->attributes : null;
     $options['params'] = count($this->params) ? $this->params : null;
     $options['flashvars'] = count($flashvars) ? $flashvars : null;
     $options['alternativeContent'] = $this->alternativeContent;
     $flashContent = Curry_Flash::embed($this->method, $this->flash, $this->width, $this->height, $this->version, $options);
     return array('Source' => $this->flash, 'Target' => $this->target, 'AlternativeContent' => $this->alternativeContent, 'Html' => $flashContent['html'], 'Script' => $flashContent['script']);
 }
Example #21
0
 /**
  * Twig callback to get subpages for page.
  *
  * @param Page $page
  * @return array|Curry_Twig_CollectionWrapper
  */
 public function twigGetSubpages(Page $page)
 {
     if ($page->isLeaf()) {
         return array();
     }
     $subpages = array();
     $children = Page::getCachedChildren($page);
     foreach ($children as $subpage) {
         if ($subpage->getEnabled() && ($this->showHidden || $subpage->getVisible())) {
             $subpages[] = $subpage;
         }
     }
     if ($children instanceof PropelCollection) {
         $children->clearIterator();
     }
     // set order
     switch ($this->sortOrder) {
         case self::ORDER_SORTINDEX_DESC:
             Curry_Array::sortOn($subpages, 'getTreeLeft', Curry_Array::SORT_REVERSE);
             break;
         case self::ORDER_NAME_ASC:
             Curry_Array::sortOn($subpages, 'getName');
             break;
         case self::ORDER_NAME_DESC:
             Curry_Array::sortOn($subpages, 'getName', Curry_Array::SORT_REVERSE);
             break;
     }
     return new Curry_Twig_CollectionWrapper($subpages, array($this, 'twigGetPage'));
 }