/**
  * 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]);
     }
 }
Beispiel #2
0
 /** {@inheritdoc} */
 public function toTwig()
 {
     return array('images' => Curry_Array::objectsToArray($this->images, null, array($this, 'getImageProperties')));
 }
Beispiel #3
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;
 }
Beispiel #4
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;
     }
 }
Beispiel #5
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;
 }
Beispiel #6
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());
 }
Beispiel #7
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();
 }
Beispiel #8
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();
 }
Beispiel #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;
 }
Beispiel #10
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);
     }
 }
Beispiel #11
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);
 }
Beispiel #12
0
 /** {@inheritdoc} */
 public function toTwig()
 {
     return array('links' => Curry_Array::objectsToArray($this->links, null, array($this, 'getLinkProperties')));
 }
 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;
 }