c#, using lambdas with collection initialization - c#-3.0

Is it possible to use a lambda expression inside an object initialization expression? Please look at the code below:
XDocument doc = new XDocument(
new XDeclaration("1.0", "utf-8", "yes"),
new XElement("data",
new XElement("album",
new XElement("slide1"),
new XElement("slide2"),
new XElement("slide3")
)
)
);
instead of...
new XElement("slide1"),
new XElement("slide2"),
new XElement("slide3")
...I want to use a lamda expression which returns XElement[]

Func<XElement[]> elementCreatorFunc =
() => new[] { new XElement(...), new XElement(...) };
XDocument doc = new XDocument(
new XDeclaration("1.0", "utf-8", "yes"),
new XElement("data",
new XElement("album",
elementCreatorFunc()
)
)
);

The following is the final solution. So instead of an external function I am doing it inline:
XDocument doc = new XDocument(new XDeclaration("1.0", "utf-8", "yes"),
new XElement("data",
new XElement("album",
(from item in Model.Items
select new XElement("slide",
new XAttribute("title", item.title)))
)
)
);

Related

Remove export option for particular table in zf-datagrid

I want to remove out the datagrid export option ('pdf', 'csv','excel','word' etc..) for some
specific table in the datagrid of zend framework . Its now by default coming for every
table attached to the project , How can i do this ? Thanks . posting the code of the datagrid
helper below
public function dataGrid($sql){
$this->view->headLink()->appendStylesheet($this->view->baseUrl('grid/styles/styles.css'));
$this->view->setEncoding('ISO-8859-1');
$config = new Zend_Config_Ini(APPLICATION_PATH. '/configs/grid.ini', 'production');
$grid = Bvb_Grid::factory('Table', $config, 'grid');
$grid->setExport(array());
$backendName = Zend_Cache::$standardBackends[0];
$frontendName = Zend_Cache::$standardFrontends[0];
$frontendOptions = array('automatic_serialization'=> true);
$backendOptions = array('auto_refresh_fast_cache'=> true);
$cache = Zend_Cache::factory($frontendName,
$backendName,$frontendOptions,$backendOptions);
$grid->setExport(array('pdf', 'csv','excel','word'));
$grid->addFiltersRenderDir(APPLICATION_PATH.'/views/helpers/Filters/', 'Filters');
$grid->setPaginationInterval(array(100 =>100, 200 => 200, 500 => 500, 1000 => 1000));
$grid->setUseKeyEventsOnFilters(true);
$grid->query($sql);
return $grid;
}
$grid->setGridColumns($grid_columns);
$grid->setExport(array());
$gd = $grid->deploy();
You can set the export option into null by passing a null array to the setExport function

silverstripe dataobject searchable

I´m trying to have certain DataObjects (News) displayed in the default SearchResult Page. So the result should display normal Pages and News.
Is there an easy way to accomplish that in Silverstripe 3?
Or is it recommended to code it completely custom - I mean a custom controller/action which handles the search request and creates a result list, which I display then in a custom template?
I found this, but obviously search is disabled right now:
https://github.com/arambalakjian/DataObjects-as-Pages
Thx and regards,
Florian
I usually but together a custom search function after enabling FulltextSearchable. So in _config.php I would have
FulltextSearchable::enable();
Object::add_extension('NewsStory', "FulltextSearchable('Name,Content')");
replacing Name and Content with whatever DBField you want to be searchable. And each searchable DataObject have this in their class to enable search indexes (pretty sure this needs to be added and run dev/build before enabling the extension, and only works on MySQL DB).
static $create_table_options = array(
'MySQLDatabase' => 'ENGINE=MyISAM'
);
then in my PageController I have my custom searchForm and results functions.
Here is the search function that returns the search form, called with $search in the template:
public function search()
{
if($this->request && $this->request->requestVar('Search')) {
$searchText = $this->request->requestVar('Search');
}else{
$searchText = 'Search';
}
$f = new TextField('Search', false, $searchText);
$fields = new FieldList(
$f
);
$actions = new FieldList(
new FormAction('results', 'Go')
);
$form = new Form(
$this,
'search',
$fields,
$actions
);
//$form->disableSecurityToken();
$form->setFormMethod('GET');
$form->setTemplate('SearchForm');
return $form;
}
and here the custom results function to handle the queries...
function results($data, $form, $request)
{
$keyword = trim($request->requestVar('Search'));
$keyword = Convert::raw2sql($keyword);
$keywordHTML = htmlentities($keyword, ENT_NOQUOTES, 'UTF-8');
$pages = new ArrayList();
$news = new ArrayList();
$mode = ' IN BOOLEAN MODE';
//$mode = ' WITH QUERY EXPANSION';
//$mode = '';
$siteTreeClasses = array('Page');
$siteTreeMatch = "MATCH( Title, MenuTitle, Content, MetaTitle, MetaDescription, MetaKeywords ) AGAINST ('$keyword'$mode)
+ MATCH( Title, MenuTitle, Content, MetaTitle, MetaDescription, MetaKeywords ) AGAINST ('$keywordHTML'$mode)";
$newsItemMatch = "MATCH( Name, Content ) AGAINST ('$keyword'$mode)
+ MATCH( Name, Content ) AGAINST ('$keywordHTML'$mode)";
//Standard pages
foreach ( $siteTreeClasses as $c )
{
$query = DataList::create($c)
->where($siteTreeMatch);
$query = $query->dataQuery()->query();
$query->addSelect(array('Relevance' => $siteTreeMatch));
$records = DB::query($query->sql());
$objects = array();
foreach( $records as $record )
{
if ( in_array($record['ClassName'], $siteTreeClasses) )
$objects[] = new $record['ClassName']($record);
}
$pages->merge($objects);
}
//news
$query = DataList::create('NewsStory')->where($newsItemMatch);
$query = $query->dataQuery()->query();
$query->addSelect(array('Relevance' => $newsItemMatch));
$records = DB::query($query->sql());
$objects = array();
foreach( $records as $record ) $objects[] = new $record['ClassName']($record);
$news->merge($objects);
//sorting results
$pages->sort(array(
'Relevance' => 'DESC',
'Title' => 'ASC'
));
$news->sort(array(
'Relevance' => 'DESC',
'Date' => 'DESC'
));
//output
$data = array(
'Pages' => $pages,
'News' => $news,
'Query' => $keyword
);
return $this->customise($data)->renderWith(array('Search','Page'));
}
I add all the Page classes I want to be searched and that extend SiteTree in the $siteTreeClasses array, and the News parts can be pretty much copied for any other DataObjectI need searchable.
I am not saying this is the best solution and this can definitely be improved on, but it works for me and this might be a good stating point.
I have adapted #colymba's solution into a silverstripe module: https://github.com/burnbright/silverstripe-pagesearch
It allows setting the pagetype in the url.
You'll need to substantially overwrite SearchForm->getResults().
It uses Database->searchEngine(), but those are tailored towards SiteTree and Page classes.
The "proper" solution is to feed the data into a search engine like Solr or Sphinx.
We have the SS3-compatible "fulltextsearch" module for this purpose:
https://github.com/silverstripe-labs/silverstripe-fulltextsearch
It's going to take some upfront setup, and is only feasible if you can either host Solr yourself, or are prepared to pay for a SaaS provider. Once you've got it running though, the possibilities are endless, its a great tool!

display a symfony post-validation error in an embeded Form

I have a form witch contains embededForms.
the Post-Validator:
$this->validatorSchema->setPostValidator(
new sfValidatorCallback(
array('callback' => array($this, 'myPostValidator'))
)
);
I throw an error in the myPostValidator method:
$error = new sfValidatorError($validator, 'invalid ' . $values['embededform1']['field']);
throw new sfValidatorErrorSchema($validator, array('field' => $error));
now I get an global error for the root-form.
I want to display the error next to the correct field.
$values['embededform1']['field']
My first suggestion would be to add the post validator in the embedded form. While embedding it will copy the validator to the container form, and your code for throwing the exception should work as expected.
Alternative you can try the following in the callback on your container form:
$error = new sfValidatorError($validator, 'invalid');
$errorschema = new sfValidatorErrorSchema($validator, array('field' => $error));
throw new sfValidatorErrorSchema($validator, array('embeddedformname' => $errorschema));
$error = new sfValidatorError($validator, 'invalid');
$errorschema = new sfValidatorErrorSchema($validator, array('field' => $error));
throw new sfValidatorErrorSchema($validator, array('embeddedformname' => $errorschema));
this worked for me.

generate annotated doctrine2 entites from db schema

Is it possible to generate Doctrine 2 entities, with the relevant docblock annotations, from an existing database schema?
I had to made these changes for the above code to work..
<?php
use Doctrine\ORM\Tools\EntityGenerator;
ini_set("display_errors", "On");
$libPath = __DIR__; // Set this to where you have doctrine2 installed
// autoloaders
require_once $libPath . '/Doctrine/Common/ClassLoader.php';
$classLoader = new \Doctrine\Common\ClassLoader('Doctrine', $libPath);
$classLoader->register();
$classLoader = new \Doctrine\Common\ClassLoader('Entities', __DIR__);
$classLoader->register();
$classLoader = new \Doctrine\Common\ClassLoader('Proxies', __DIR__);
$classLoader->register();
// config
$config = new \Doctrine\ORM\Configuration();
$config->setMetadataDriverImpl($config->newDefaultAnnotationDriver(__DIR__ . '/Entities'));
$config->setMetadataCacheImpl(new \Doctrine\Common\Cache\ArrayCache);
$config->setProxyDir(__DIR__ . '/Proxies');
$config->setProxyNamespace('Proxies');
$connectionParams = array(
'path' => 'test.sqlite3',
'driver' => 'pdo_sqlite',
);
$em = \Doctrine\ORM\EntityManager::create($connectionParams, $config);
// custom datatypes (not mapped for reverse engineering)
$em->getConnection()->getDatabasePlatform()->registerDoctrineTypeMapping('set', 'string');
$em->getConnection()->getDatabasePlatform()->registerDoctrineTypeMapping('enum', 'string');
// fetch metadata
$driver = new \Doctrine\ORM\Mapping\Driver\DatabaseDriver(
$em->getConnection()->getSchemaManager()
);
$em->getConfiguration()->setMetadataDriverImpl($driver);
$cmf = new \Doctrine\ORM\Tools\DisconnectedClassMetadataFactory($em);
$cmf->setEntityManager($em);
$classes = $driver->getAllClassNames();
$metadata = $cmf->getAllMetadata();
$generator = new EntityGenerator();
$generator->setUpdateEntityIfExists(true);
$generator->setGenerateStubMethods(true);
$generator->setGenerateAnnotations(true);
$generator->generate($metadata, __DIR__ . '/Entities');
print 'Done!';
?>
and mysql connection configuration like :
$connectionParams = array(
'driver' => 'pdo_mysql',
'host' => 'localhost',
'port' => '3306',
'user' => 'root',
'password' => 'root',
'dbname' => 'database',
'charset' => 'utf8',
);
Yes it possible though RDBMS data types are not fully supported, so you might have to play with your code a bit before using it in your project. It's not straight forward as Doctrine 1.x used to be but still rather easy. Here some sample code I used myself (create folders properly before using it)
use Doctrine\ORM\Tools\EntityGenerator;
ini_set("display_errors", "On");
$libPath = __DIR__ . '/../lib/doctrine2';
// autoloaders
require_once $libPath . '/Doctrine/Common/ClassLoader.php';
$classLoader = new \Doctrine\Common\ClassLoader('Doctrine', $libPath);
$classLoader->register();
$classLoader = new \Doctrine\Common\ClassLoader('Entities', __DIR__);
$classLoader->register();
$classLoader = new \Doctrine\Common\ClassLoader('Proxies', __DIR__);
$classLoader->register();
// config
$config = new \Doctrine\ORM\Configuration();
$config->setMetadataDriverImpl($config->newDefaultAnnotationDriver(__DIR__ . '/Entities'));
$config->setMetadataCacheImpl(new \Doctrine\Common\Cache\ArrayCache);
$config->setProxyDir(__DIR__ . '/Proxies');
$config->setProxyNamespace('Proxies');
$connectionParams = array(
'dbname' => 'xx',
'user' => 'root',
'password' => '',
'host' => 'localhost',
'driver' => 'pdo_mysql',
);
$em = \Doctrine\ORM\EntityManager::create($connectionParams, $config);
// custom datatypes (not mapped for reverse engineering)
$em->getConnection()->getDatabasePlatform()->registerDoctrineTypeMapping('set', 'string');
$em->getConnection()->getDatabasePlatform()->registerDoctrineTypeMapping('enum', 'string');
// fetch metadata
$driver = new \Doctrine\ORM\Mapping\Driver\DatabaseDriver(
$em->getConnection()->getSchemaManager()
);
$classes = $driver->getAllClassNames();
foreach ($classes as $class) {
//any unsupported table/schema could be handled here to exclude some classes
if (true) {
$metadata[] = $cmf->getMetadataFor($class);
}
}
$em->getConfiguration()->setMetadataDriverImpl($driver);
$cmf = new \Doctrine\ORM\Tools\DisconnectedClassMetadataFactory($em);
$generator = new EntityGenerator();
$generator->setUpdateEntityIfExists(true);
$generator->setGenerateStubMethods(true);
$generator->setGenerateAnnotations(true);
$generator->generate($metadata, __DIR__ . '/Entities');
print 'Done!';
I have implemented new command to achieve that https://github.com/umpirsky/doctrine2/blob/master/lib/Doctrine/ORM/Tools/Console/Command/GenerateEntitiesDbCommand.php
Just add it like this:
$cli->addCommands(array(
// DBAL Commands
new \Doctrine\DBAL\Tools\Console\Command\RunSqlCommand(),
new \Doctrine\DBAL\Tools\Console\Command\ImportCommand(),
// ORM Commands
new \Doctrine\ORM\Tools\Console\Command\ClearCache\MetadataCommand(),
new \Doctrine\ORM\Tools\Console\Command\ClearCache\ResultCommand(),
new \Doctrine\ORM\Tools\Console\Command\ClearCache\QueryCommand(),
new \Doctrine\ORM\Tools\Console\Command\SchemaTool\CreateCommand(),
new \Doctrine\ORM\Tools\Console\Command\SchemaTool\UpdateCommand(),
new \Doctrine\ORM\Tools\Console\Command\SchemaTool\DropCommand(),
new \Doctrine\ORM\Tools\Console\Command\EnsureProductionSettingsCommand(),
new \Doctrine\ORM\Tools\Console\Command\ConvertDoctrine1SchemaCommand(),
new \Doctrine\ORM\Tools\Console\Command\GenerateRepositoriesCommand(),
new \Doctrine\ORM\Tools\Console\Command\GenerateEntitiesCommand(),
new \Doctrine\ORM\Tools\Console\Command\GenerateEntitiesDbCommand(),
new \Doctrine\ORM\Tools\Console\Command\GenerateProxiesCommand(),
new \Doctrine\ORM\Tools\Console\Command\ConvertMappingCommand(),
new \Doctrine\ORM\Tools\Console\Command\RunDqlCommand(),
new \Doctrine\ORM\Tools\Console\Command\ValidateSchemaCommand(),
));
$cli->run();
As of https://github.com/doctrine/doctrine2/blob/master/lib/Doctrine/ORM/Tools/Console/ConsoleRunner.php , generating entities is already supported by Doctrine's default CLI

Zend Framework: Zend_translate and routing related issue

I have implemented Zend_Navigation, Zend_Translate in my application.
The routing is setup in Bootstrap.php like below.
$fc = Zend_Controller_Front::getInstance();
$zl=new Zend_Locale();
Zend_Registry::set('Zend_Locale',$zl);
$lang=$zl->getLanguage().'_'.$zl->getRegion();
$router = $fc->getRouter();
$route = new Zend_Controller_Router_Route(':lang/:module/:controller/:action/*',
array(
'lang'=>$lang, 'module'=>'default', 'controller'=>'index', 'action'=>'index'
));
$router->addRoute('default', $route);
$fc->setRouter($router);
$fc->registerPlugin( new Plugin_LanguageSetup());
in LaunguageSetup Plugin i have defined the dispatchLoopStartup method to do the checking of the language param
public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request) {
$this->createLangUrl($request);
$this->_language = $request->getParam('lang');
if ((!isset($this->_language)) || !in_array($this->_language, $this->_languagesArray)) {
$this->_language = 'en_US';
$request->setParam('lang', 'en_US');
}
$file = APPLICATION_PATH.$this->_directory.$this->_language.'.csv';
$translate = new Zend_Translate('csv', $file, $this->_language);
Zend_Registry::set('Zend_Translate', $translate);
$zl = Zend_Registry::get('Zend_Locale');
$zl->setLocale($this->_language);
Zend_Registry::set('Zend_Locale', $zl);
// $fc = Zend_Controller_Front::getInstance();
// $router = $fc->getRouter();
// $route = new Zend_Controller_Router_Route(':lang/:module/:controller/:action/*', array(
// 'lang'=>$this->_language, 'module'=>'default', 'controller'=>'index', 'action'=>'index'
// ));
// $router->addRoute('default', $route);
// $fc->setRouter($router);
}
What happen is the language always have the default value, the 'lang' param never default lang value in route, even if i type it in the address bar manually i.e /en_US/module/controller/action/ It always get revert back to the default Zend_locale();
Only way i can fix it is to setup the route again in the plugin and inject a correct language value as default. Any Idea why?
try and do a var_dump of the 2 vars ( _language, _languagesArray ) before this line
if ((!isset($this->_language)) || !in_array($this->_language, $this->_languagesArray)) {
I suspect that there it should be the problem, because you put yor plugin on dispatchLoopStartup, and then the params might not be populated, i put my plugin on routeShutdown see my implementation of the languange plugin.
Sort of a partial solution.
in dispatchLoopStartup
add
$fc = Zend_Controller_Front::getInstance();
$router = $fc->getRouter();
$router->setGlobalParam('lang',$this->_language);
better than redefine and overwrite the route again and 'fake' the language param by changing the default 'lang' value.
it's just less than perfect. Zend_router suppose to pick up the 'lang' param and have them placed in Zend_navigation->menu();