<?php

class catalyzPopulateTask extends sfBaseTask {
    protected function configure() {
        $this->namespace = 'catalyz';
        $this->name = 'populate';
        $this->briefDescription = 'Populate default site structure';
        $this->detailedDescription = <<<EOF
The [catalyz:populate-englishuk|INFO] task initialize the website with default structure.
Call it with:

  [php symfony catalyz:populate|INFO]
EOF;
        $this->addArgument('application', sfCommandArgument::OPTIONAL, 'The application name', 'frontend');
        // add other arguments here
        $this->addOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'The environment', 'dev');
        $this->addOption('connection', null, sfCommandOption::PARAMETER_REQUIRED, 'The connection name', 'propel');
        // add other options here
    }

    protected function execute($arguments = array(), $options = array()) {
        // Database initialization
        $databaseManager = new sfDatabaseManager($this->configuration);
        $connection = Propel::getConnection($options['connection'] ? $options['connection'] : '');

        $this->ContentTree = ContentTree::instance('fr');
        $this->ContentTree->build(true);
        $this->ContentTree->setCacheEnabled(false);

        $this->createSiteStructure();

        $this->Log('Site loaded');
    }

    protected $default_culture = 'fr';
    protected $ContentTree;
    protected $nodes = array();
    // protected $path_to_image = sfConfig::get('sf_web_dir').'/images/';
    protected $loremIpsum = '<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Donec consequat lobortis sapien. Proin quis justo. Nam rhoncus dolor eget libero. Integer pede libero, lobortis vitae, auctor vel, feugiat a, nisi. Pellentesque mattis suscipit urna. Praesent consequat imperdiet pede. Aliquam eu ipsum nec leo dictum pretium. Praesent arcu quam, blandit a, tincidunt nec, aliquam id, urna. Aliquam vulputate pulvinar risus. Nunc sagittis adipiscing purus.</p>
			<p>Aenean mi ligula, vehicula ut, ultricies in, porta sed, diam. Curabitur commodo. Vivamus pretium venenatis sapien. Donec ante lacus, fermentum in, euismod euismod, ullamcorper ut, augue. Maecenas adipiscing mi nec tortor. Vestibulum pellentesque augue non mi. Nulla pellentesque varius nisi. Sed tempus tristique magna. Vivamus sapien. Integer vel erat. Etiam semper libero sit amet lorem. Integer erat mauris, sodales at, luctus eget, facilisis ac, nisi. Nunc pede. Mauris turpis. Donec congue, libero at aliquam laoreet, sapien nisi egestas arcu, vitae vulputate nisl metus eu dui. Vestibulum mauris. Aenean vitae diam. In neque felis, ultrices nec, pellentesque venenatis, vehicula nec, eros. Ut sagittis posuere massa.</p>
			<p>Donec commodo elementum orci. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Etiam eleifend. Ut cursus, ante consequat pretium condimentum, ante enim ultrices massa, ac faucibus ipsum pede quis elit. Cras sollicitudin sapien a est. Vestibulum lobortis. Morbi placerat justo eu urna. Phasellus malesuada odio id ipsum. Nulla viverra, libero a elementum varius, diam neque molestie nisl, in aliquam ante tellus vitae nisl. Nullam eu eros sed mauris fermentum lobortis. Aliquam blandit mi. Aenean sodales augue sed diam. Ut vestibulum mauris a ante. Phasellus ultricies porta sem. Morbi nec neque.</p>';

    protected $short_loremIpsum = 'Donec consequat lobortis sapien. Proin quis justo. Nam rhoncus dolor eget libero. Integer pede libero, lobortis vitae, auctor vel, feugiat a, nisi. Pellentesque mattis suscipit urna. Praesent consequat imperdiet pede. Aliquam eu ipsum nec leo dictum pretium.';

    protected function notifyNewPage($path, $type = 'page') {
        $this->LogSection($type . '+', $path);
    }

    protected function createSiteStructure() {
        $this->createHome('Accueil');

        $this->createNetwork('Le réseau');
        $this->createPage('Le concept', '/le-reseau');
        $this->createPage('La charte', '/le-reseau');
        $this->createPage('Les statuts', '/le-reseau');
        $this->createPage('Agenda', '/le-reseau');
        $this->createPage('Mail list', '/le-reseau');
        $this->createPage('Devenez coopérateur', '/le-reseau');

        $this->createSitemap('Plan de site', '/');
        $this->createPage('Crédits', '/', false);
        $this->createPage('Mention légales', '/', false);
    }

    protected function createHome($title, $layout = 'katao/default') {
        $node = new Node();
        $node->makeRoot();
        $node->setUrlType(Node::URL_TYPE_CUSTOM, $this->default_culture);
        $node->setTitle($title, $this->default_culture);
        $node->setIsPublic(true);
        $node->setIsVisibleInMenu(false);
        $node->setNodeClass('Home');
        $node->setCurrentLayout($layout);
        $node->setCurrentLayoutType(Node::LAYOUT_CUSTOM);
        $node->setUrlIdentifier('', $this->default_culture);
        $node->setPermsType(Node::PERMS_ALLOWED_TO_ANYBODY);
        $node->save();
        $this->ContentTree->clearCache();

        $child = new NodeHome();
        $child->setNode($node);
        $child->setEditoTitle('Bonjour à vous !');
        $child->setEditoContent('<p>Conscients de la consommation à tout prix et de la financiarisation du commerce au détriment des conditions de vie des hommes et de la nature, nous sommes des citoyens qui décidons de prendre en mains notre avenir et celui de nos enfants ICI ET MAINTENANT.</p>
<p>Regroupés dans un réseau de proximité et de dimension humaine, nous, coopérateurs (acheteurs, producteurs ou intermédiaires) nous nous fixons pour objectif de rendre accessibles au plus grand nombre des biens, des services  et des informations prioritairement locaux, sinon équitables et toujours écologiques afin de réduire notre empreinte écologique pour nos premiers pas vers un réel développement durable.</p>
<p>Vous pouvez dores et déjà télécharger la charte des consom\'acteur de Katao et les statuts de notre association...</p>');
        $child->save();
        $this->ContentTree->clearCache();

        $path = '/' ;
        $this->nodes[$path] = $node->getId();
        $this->notifyNewPage($path, 'home');
    }

    protected function createPage($title, $parentPath = '/', $visible_in_menu = true, $content = null, $layout = 'katao/default') {
        if (null == $content) {
            $content = $this->loremIpsum;
        }
        $node = new Node();
        $node->insertAsLastChildOf(NodePeer::retrieveByPk($this->nodes[$parentPath], null, false));
        $node->setUrltype(Node::URL_TYPE_GENERATED, $this->default_culture);
        $node->setTitle($title, $this->default_culture);
        // $node->setSubTitle('Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Donec consequat lobortis sapien. Proin quis justo.', 'fr');
        $node->setIsPublic(true);
        $node->setIsVisibleInMenu($visible_in_menu);
        $node->setNodeClass('Page');
        $node->setCurrentLayoutType(Node::LAYOUT_CUSTOM);
        $node->setCurrentLayout($layout);
        $node->setPermsType(Node::PERMS_INHERIT);

        $node->save();
        $this->ContentTree->clearCache();

        $child = new NodePage();
        $child->setNode($node);
        $child->setContent($content, $this->default_culture);
        $child->save();
        $this->ContentTree->clearCache();

        if ('/' == $parentPath) {
            $path = '/' . $node->getUrlIdentifier($this->default_culture);
        } else {
            $path = $parentPath . '/' . $node->getUrlIdentifier($this->default_culture);
        }

        $this->nodes[$path] = $node->getId();
        $this->notifyNewPage($path);
    }
    protected function createNetwork($title, $parentPath = '/', $visible_in_menu = true, $content = null, $layout = 'katao/default') {
        if (null == $content) {
            $content = $this->loremIpsum;
        }
        $node = new Node();
        $node->insertAsLastChildOf(NodePeer::retrieveByPk($this->nodes[$parentPath], null, false));
        $node->setUrltype(Node::URL_TYPE_GENERATED, $this->default_culture);
        $node->setTitle($title, $this->default_culture);
        // $node->setSubTitle('Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Donec consequat lobortis sapien. Proin quis justo.', 'fr');
        $node->setIsPublic(true);
        $node->setIsVisibleInMenu($visible_in_menu);
        $node->setNodeClass('Network');
        $node->setCurrentLayoutType(Node::LAYOUT_CUSTOM);
        $node->setCurrentLayout($layout);
        $node->setPermsType(Node::PERMS_INHERIT);

        $node->save();
        $this->ContentTree->clearCache();

        $child = new NodeNetwork();
        $child->setNode($node);
        $child->setIntroduction($content, $this->default_culture);
        $child->save();
        $this->ContentTree->clearCache();

        if ('/' == $parentPath) {
            $path = '/' . $node->getUrlIdentifier($this->default_culture);
        } else {
            $path = $parentPath . '/' . $node->getUrlIdentifier($this->default_culture);
        }

        $this->nodes[$path] = $node->getId();
        $this->notifyNewPage($path);
    }
    protected function createSitemap($title, $parentPath = '/', $layout = 'katao/default') {
        $node = new Node();
        $node->insertAsLastChildOf(NodePeer::retrieveByPk($this->nodes[$parentPath], null, false));
        $node->setUrltype(Node::URL_TYPE_GENERATED, $this->default_culture);
        $node->setTitle($title, $this->default_culture);
        // $node->setSubTitle('');
        $node->setIsPublic(true);
        $node->setIsVisibleInMenu(false);
        $node->setNodeClass('Sitemap');
        $node->setCurrentLayoutType(Node::LAYOUT_CUSTOM);
        $node->setCurrentLayout($layout);
        $node->setPermsType(Node::PERMS_INHERIT);

        $node->save();
        $this->ContentTree->clearCache();

        $child = new NodeSitemap();
        $child->setNode($node);
        $child->save();
        $this->ContentTree->clearCache();

        if ('/' == $parentPath) {
            $path = '/' . $node->getUrlIdentifier($this->default_culture);
        } else {
            $path = $parentPath . '/' . $node->getUrlIdentifier($this->default_culture);
        }
        $this->nodes[$path] = $node->getId();
        $this->notifyNewPage($path, 'sitemap');
    }
}