public function savesfGuardGroupPermissionList($con = null)
 {
     if (!$this->isValid()) {
         throw $this->getErrorSchema();
     }
     if (!isset($this->widgetSchema['sf_guard_group_permission_list'])) {
         // somebody has unset this widget
         return;
     }
     if (null === $con) {
         $con = $this->getConnection();
     }
     $c = new Criteria();
     $c->add(sfGuardGroupPermissionPeer::PERMISSION_ID, $this->object->getPrimaryKey());
     sfGuardGroupPermissionPeer::doDelete($c, $con);
     $values = $this->getValue('sf_guard_group_permission_list');
     if (is_array($values)) {
         foreach ($values as $value) {
             $obj = new sfGuardGroupPermission();
             $obj->setPermissionId($this->object->getPrimaryKey());
             $obj->setGroupId($value);
             $obj->save();
         }
     }
 }
 public function up()
 {
     // Start order picking permission
     $picking = new sfGuardPermission();
     $picking->setName('admin_shop_order_picking');
     $picking->setDescription('Administrator orders in shop');
     $picking->save();
     $o_group = Doctrine::getTable('sfGuardGroup')->findOneByName('admin');
     $group_order = new sfGuardGroupPermission();
     $group_order->setGroupId($o_group->getId());
     $group_order->setPermissionId($picking->getId());
     $group_order->save();
     // End order picking permission
     // Start order dispatch permission
     $dispatch = new sfGuardPermission();
     $dispatch->setName('admin_shop_order_dispatch');
     $dispatch->setDescription('Administrator promotions in shop');
     $dispatch->save();
     $p_group = Doctrine::getTable('sfGuardGroup')->findOneByName('admin');
     $group_promotion = new sfGuardGroupPermission();
     $group_promotion->setGroupId($p_group->getId());
     $group_promotion->setPermissionId($dispatch->getId());
     $group_promotion->save();
     // End order dispatch permission
     // Add new column shipping_code to shop order table
     $this->addColumn('rt_shop_order', 'shipping_code', 'string', '50', array());
 }
 public function up()
 {
     $permission = new sfGuardPermission();
     $permission->setName('show_admin_menu');
     $permission->setDescription('Administration menu to be displayed');
     $permission->save();
     $group = Doctrine::getTable('sfGuardGroup')->findOneByName('admin');
     $group_permission = new sfGuardGroupPermission();
     $group_permission->setGroupId($group->getId());
     $group_permission->setPermissionId($permission->getId());
     $group_permission->save();
 }
 public function up()
 {
     $permission = new sfGuardPermission();
     $permission->setName('admin_snippet');
     $permission->setDescription('Administrator permission for snippets');
     $permission->save();
     $group = Doctrine::getTable('sfGuardGroup')->findOneByName('admin');
     $group_permission = new sfGuardGroupPermission();
     $group_permission->setGroupId($group->getId());
     $group_permission->setPermissionId($permission->getId());
     $group_permission->save();
 }
 public function up()
 {
     $permission = new sfGuardPermission();
     $permission->setName('admin_comment');
     $permission->setDescription('Administration menu to be displayed');
     $permission->save();
     $group = Doctrine::getTable('sfGuardGroup')->findOneByName('admin');
     $group_permission = new sfGuardGroupPermission();
     $group_permission->setGroupId($group->getId());
     $group_permission->setPermissionId($permission->getId());
     $group_permission->save();
     $this->createTable('rt_comment', array('id' => array('type' => 'integer', 'length' => '8', 'autoincrement' => '1', 'primary' => '1'), 'is_active' => array('type' => 'boolean', 'length' => '25'), 'model_id' => array('type' => 'integer', 'length' => '8'), 'model' => array('type' => 'string', 'length' => '100'), 'user_id' => array('type' => 'integer', 'length' => '8'), 'comment_id' => array('type' => 'integer', 'length' => '8'), 'author_name' => array('type' => 'string', 'length' => '255'), 'author_email' => array('type' => 'string', 'length' => '255'), 'author_website' => array('type' => 'string', 'extra' => 'link', 'length' => '255'), 'content' => array('type' => 'string', 'length' => ''), 'moderator_note' => array('type' => 'string', 'length' => ''), 'created_at' => array('notnull' => '1', 'type' => 'timestamp', 'length' => '25'), 'updated_at' => array('notnull' => '1', 'type' => 'timestamp', 'length' => '25')), array('primary' => array(0 => 'id'), 'collate' => 'utf8_unicode_ci', 'charset' => 'utf8'));
     $this->createTable('rt_comment_report', array('id' => array('type' => 'integer', 'length' => '8', 'autoincrement' => '1', 'primary' => '1'), 'reason' => array('type' => 'clob', 'length' => ''), 'referer' => array('type' => 'string', 'length' => '255'), 'status' => array('type' => 'enum', 'values' => array(0 => 'valid', 1 => 'invalid', 2 => 'untreated'), 'default' => 'untreated', 'length' => ''), 'comment_id' => array('type' => 'integer', 'notnull' => '1', 'length' => '8'), 'created_at' => array('notnull' => '1', 'type' => 'timestamp', 'length' => '25'), 'updated_at' => array('notnull' => '1', 'type' => 'timestamp', 'length' => '25')), array('primary' => array(0 => 'id')));
 }
 public function saveGroupsList($con = null)
 {
     if (!$this->isValid()) {
         throw $this->getErrorSchema();
     }
     if (!isset($this->widgetSchema['groups_list'])) {
         // somebody has unset this widget
         return;
     }
     if (is_null($con)) {
         $con = $this->getConnection();
     }
     $q = Doctrine_Query::create()->delete()->from('sfGuardGroupPermission r')->where('r.permission_id = ?', current($this->object->identifier()))->execute();
     $values = $this->getValue('groups_list');
     if (is_array($values)) {
         foreach ($values as $value) {
             $obj = new sfGuardGroupPermission();
             $obj->permission_id = current($this->object->identifier());
             $obj->group_id = $value;
             $obj->save();
         }
     }
 }
 public function up()
 {
     // Start order permission
     $order = new sfGuardPermission();
     $order->setName('admin_shop_order');
     $order->setDescription('Administrator orders in shop');
     $order->save();
     $o_group = Doctrine::getTable('sfGuardGroup')->findOneByName('admin');
     $group_order = new sfGuardGroupPermission();
     $group_order->setGroupId($o_group->getId());
     $group_order->setPermissionId($order->getId());
     $group_order->save();
     // End order permission
     // Start promotion permission
     $promotion = new sfGuardPermission();
     $promotion->setName('admin_shop_promotion');
     $promotion->setDescription('Administrator promotions in shop');
     $promotion->save();
     $p_group = Doctrine::getTable('sfGuardGroup')->findOneByName('admin');
     $group_promotion = new sfGuardGroupPermission();
     $group_promotion->setGroupId($p_group->getId());
     $group_promotion->setPermissionId($promotion->getId());
     $group_promotion->save();
     // End promotion permission
     // Start voucher permission
     $voucher = new sfGuardPermission();
     $voucher->setName('admin_shop_voucher');
     $voucher->setDescription('Administrator vouchers in shop');
     $voucher->save();
     $v_group = Doctrine::getTable('sfGuardGroup')->findOneByName('admin');
     $group_voucher = new sfGuardGroupPermission();
     $group_voucher->setGroupId($v_group->getId());
     $group_voucher->setPermissionId($voucher->getId());
     $group_voucher->save();
     // End voucher permission
 }
Example #8
0
 /**
  * Filter the query by a related sfGuardGroupPermission object
  *
  * @param     sfGuardGroupPermission $sfGuardGroupPermission  the related object to use as filter
  * @param     string $comparison Operator to use for the column comparison, defaults to Criteria::EQUAL
  *
  * @return    sfGuardGroupQuery The current query, for fluid interface
  */
 public function filterBysfGuardGroupPermission($sfGuardGroupPermission, $comparison = null)
 {
     return $this->addUsingAlias(sfGuardGroupPeer::ID, $sfGuardGroupPermission->getGroupId(), $comparison);
 }
 /**
  * Adds an object to the instance pool.
  *
  * Propel keeps cached copies of objects in an instance pool when they are retrieved
  * from the database.  In some cases -- especially when you override doSelect*()
  * methods in your stub classes -- you may need to explicitly add objects
  * to the cache in order to ensure that the same objects are always returned by doSelect*()
  * and retrieveByPK*() calls.
  *
  * @param      sfGuardGroupPermission $value A sfGuardGroupPermission object.
  * @param      string $key (optional) key to use for instance map (for performance boost if key was already calculated externally).
  */
 public static function addInstanceToPool(sfGuardGroupPermission $obj, $key = null)
 {
     if (Propel::isInstancePoolingEnabled()) {
         if ($key === null) {
             $key = serialize(array((string) $obj->getGroupId(), (string) $obj->getPermissionId()));
         }
         // if key === null
         self::$instances[$key] = $obj;
     }
 }
 public function executePermissions(sfWebRequest $request)
 {
     $module = 'sfGuardUser';
     if (!in_array($module, array_keys(sfPlop::getSafePluginModules()))) {
         $this->redirect('@sf_plop_dashboard');
     }
     if ($request->isMethod(sfRequest::POST)) {
         if ($request->isXmlHttpRequest()) {
             $this->setTemplate('ajaxPermissions');
             $this->setLayout(false);
         }
         $group_id = $request->getParameter('g');
         $user_id = $request->getParameter('u');
         $permission_id = $request->getParameter('p');
         if ($group_id) {
             $group_exists = sfPlopGuard::groupExists($group_id);
             if (!$group_exists && $request->isXmlHttpRequest()) {
                 return sfView::ERROR;
             } else {
                 if (!$group_exists) {
                     $this->redirect('@sf_plop_dashboard_permissions');
                 }
             }
         }
         if ($user_id) {
             $user_exists = sfPlopGuard::userExists($user_id);
             if (!$user_exists && $request->isXmlHttpRequest()) {
                 return sfView::ERROR;
             } else {
                 if (!$user_exists) {
                     $this->redirect('@sf_plop_dashboard_permissions');
                 }
             }
         }
         if (isset($group_exists) && isset($user_exists)) {
             $user_group = sfGuardUserGroupPeer::retrieveByPK($user_id, $group_id);
             if ($user_group) {
                 $user_group->delete();
             } else {
                 $user_group = new sfGuardUsergroup();
                 $user_group->setUserId($user_id);
                 $user_group->setGroupId($group_id);
                 $user_group->save();
                 $this->getResponse()->setStatusCode(201);
             }
         }
         if ($permission_id) {
             if ($permission_id == 'super') {
                 if (!sfPlopGuard::isLastSuperAdminUser($user_id)) {
                     $user = sfGuardUserPeer::retrieveByPK($user_id);
                     if ($user->getIsSuperAdmin()) {
                         $user->setIsSuperAdmin(false);
                     } else {
                         $user->setIsSuperAdmin(true);
                     }
                     $user->save();
                 } else {
                     $this->getResponse()->setStatusCode(202);
                     return sfView::ERROR;
                 }
             } else {
                 if (!is_int($permission_id)) {
                     $permission_exists = sfPlopGuard::permissionExists($permission_id);
                     if (!$permission_exists) {
                         $modules = sfPlop::getSafePluginModules();
                         if ($request->isXmlHttpRequest() && !isset($modules[$permission_id])) {
                             return sfView::ERROR;
                         } elseif (!isset($modules[$permission_id])) {
                             $this->redirect('@sf_plop_dashboard_permissions');
                         } else {
                             $module = $modules[$permission_id];
                         }
                         $permission = new sfGuardPermission();
                         $permission->setName($permission_id);
                         $permission->setDescription($module['name']);
                         $permission->save();
                         $permission_id = $permission->getId();
                         $this->getResponse()->setStatusCode(201);
                     } else {
                         $permission_id = sfPlopGuard::getPermission($permission_id)->getId();
                     }
                 } else {
                     $permission_exists = sfPlopGuard::permissionExists($permission_id);
                     if (!$permission_exists && $request->isXmlHttpRequest()) {
                         return sfView::ERROR;
                     } else {
                         if (!$permission_exists) {
                             $this->redirect('@sf_plop_dashboard_permissions');
                         }
                     }
                 }
                 if (isset($user_exists)) {
                     $user_permission = sfGuardUserPermissionPeer::retrieveByPK($user_id, $permission_id);
                     if ($user_permission) {
                         $user_permission->delete();
                     } else {
                         $user_permission = new sfGuardUserPermission();
                         $user_permission->setUserId($user_id);
                         $user_permission->setPermissionId($permission_id);
                         $user_permission->save();
                         $this->getResponse()->setStatusCode(201);
                     }
                 } elseif (isset($group_exists)) {
                     $group_permission = sfGuardGroupPermissionPeer::retrieveByPK($group_id, $permission_id);
                     if ($group_permission) {
                         $group_permission->delete();
                     } else {
                         $group_permission = new sfGuardGroupPermission();
                         $group_permission->setGroupId($group_id);
                         $group_permission->setPermissionId($permission_id);
                         $group_permission->save();
                         $this->getResponse()->setStatusCode(201);
                     }
                 }
             }
         }
         if (!$request->isXmlHttpRequest()) {
             $this->redirect('@sf_plop_dashboard_permissions');
         }
     }
     $this->groups = sfPlopGuard::getAllGroups();
     $this->users = sfPlopGuard::getAllUsers();
     $this->permissions = sfPlopGuard::getAllPermissions();
 }
Example #11
0
 public function grantProjectGroupPermission($group_name, $permission_name)
 {
     $permission = sfGuardPermissionPeer::retrieveByName($this->getUuid() . '-' . $permission_name);
     $group = sfGuardGroupPeer::retrieveByName($this->getUuid() . '-' . $group_name);
     if ($permission == null) {
         sfContext::getInstance()->getLogger()->info('permission not found: [' . $this->getUuid() . $permission_name . ']');
         return false;
     } elseif ($group == null) {
         sfContext::getInstance()->getLogger()->info('group not found: [' . $this->getUuid() . $group_name . ']');
         return false;
     }
     // TODO: make sure that the group permission name does not already exist
     $groupPermission = new sfGuardGroupPermission();
     $groupPermission->setGroupId($group->getPrimaryKey());
     $groupPermission->setPermissionId($permission->getId());
     $groupPermission->save();
     sfContext::getInstance()->getLogger()->info('group permission saved: [' . $this->getUuid() . $group_name . ']:[' . $this->getUuid() . $permission_name . ']');
 }
Example #12
0
 /**
  * @param	sfGuardGroupPermission $sfGuardGroupPermission The sfGuardGroupPermission object to add.
  */
 protected function doAddsfGuardGroupPermission($sfGuardGroupPermission)
 {
     $this->collsfGuardGroupPermissions[] = $sfGuardGroupPermission;
     $sfGuardGroupPermission->setsfGuardGroup($this);
 }
 /**
  * Filter the query by a related sfGuardGroupPermission object
  *
  * @param   sfGuardGroupPermission|PropelObjectCollection $sfGuardGroupPermission  the related object to use as filter
  * @param     string $comparison Operator to use for the column comparison, defaults to Criteria::EQUAL
  *
  * @return   sfGuardPermissionQuery The current query, for fluid interface
  * @throws   PropelException - if the provided filter is invalid.
  */
 public function filterBysfGuardGroupPermission($sfGuardGroupPermission, $comparison = null)
 {
     if ($sfGuardGroupPermission instanceof sfGuardGroupPermission) {
         return $this->addUsingAlias(sfGuardPermissionPeer::ID, $sfGuardGroupPermission->getPermissionId(), $comparison);
     } elseif ($sfGuardGroupPermission instanceof PropelObjectCollection) {
         return $this->usesfGuardGroupPermissionQuery()->filterByPrimaryKeys($sfGuardGroupPermission->getPrimaryKeys())->endUse();
     } else {
         throw new PropelException('filterBysfGuardGroupPermission() only accepts arguments of type sfGuardGroupPermission or PropelCollection');
     }
 }
Example #14
0
 public function addsfGuardGroupPermission(sfGuardGroupPermission $l)
 {
     $this->collsfGuardGroupPermissions[] = $l;
     $l->setsfGuardGroup($this);
 }
 /**
  * Exclude object from result
  *
  * @param     sfGuardGroupPermission $sfGuardGroupPermission Object to remove from the list of results
  *
  * @return    sfGuardGroupPermissionQuery The current query, for fluid interface
  */
 public function prune($sfGuardGroupPermission = null)
 {
     if ($sfGuardGroupPermission) {
         $this->addCond('pruneCond0', $this->getAliasedColName(sfGuardGroupPermissionPeer::GROUP_ID), $sfGuardGroupPermission->getGroupId(), Criteria::NOT_EQUAL);
         $this->addCond('pruneCond1', $this->getAliasedColName(sfGuardGroupPermissionPeer::PERMISSION_ID), $sfGuardGroupPermission->getPermissionId(), Criteria::NOT_EQUAL);
         $this->combine(array('pruneCond0', 'pruneCond1'), Criteria::LOGICAL_OR);
     }
     return $this;
 }
 /**
  * DOCUMENT ME
  * @param mixed $arguments
  * @param mixed $options
  */
 protected function execute($arguments = array(), $options = array())
 {
     // We need a basic context so we can notify events
     $context = sfContext::createInstance($this->configuration);
     // initialize the database connection
     $databaseManager = new sfDatabaseManager($this->configuration);
     $connection = $databaseManager->getDatabase($options['connection'] ? $options['connection'] : null)->getConnection();
     $postTasks = array();
     echo "\nApostrophe Database Migration Task\n  \nThis task will make any necessary database schema changes to bring your \nMySQL database up to date with the current release of Apostrophe and any additional\nApostrophe plugins that you have installed. For other databases see the source code \nor run './symfony doctrine:build-sql' to obtain the SQL commands you may need.\n  \nBACK UP YOUR DATABASE BEFORE YOU RUN THIS TASK. It works fine in our tests, \nbut why take chances with your data?\n\n";
     if (!$options['force']) {
         if (!$this->askConfirmation("Are you sure you are ready to migrate your project? [y/N]", 'QUESTION_LARGE', false)) {
             die("Operation CANCELLED. No changes made.\n");
         }
     }
     $this->migrate = new aMigrate(Doctrine_Manager::connection()->getDbh());
     // If I needed to I could look for the constraint definition like this.
     // But since we added these in the same migration I don't have to. Keep this
     // comment around as sooner or later we'll probably need to check for this
     // kind of thing
     //
     // $createTable = $data[0]['Create Table'];
     // if (!preg_match('/CONSTRAINT `a_redirect_page_id_a_page_id`/', $createTable))
     // {
     //
     // }
     if (!$this->migrate->tableExists('a_redirect')) {
         $this->migrate->sql(array("CREATE TABLE IF NOT EXISTS a_redirect (id INT AUTO_INCREMENT, page_id INT, slug VARCHAR(255) UNIQUE, created_at DATETIME NOT NULL, updated_at DATETIME NOT NULL, INDEX slugindex_idx (slug), INDEX page_id_idx (page_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ENGINE = INNODB;", "ALTER TABLE a_redirect ADD CONSTRAINT a_redirect_page_id_a_page_id FOREIGN KEY (page_id) REFERENCES a_page(id) ON DELETE CASCADE;"));
     }
     if (!$this->migrate->columnExists('a_media_item', 'lucene_dirty')) {
         $this->migrate->sql(array("ALTER TABLE a_media_item ADD COLUMN lucene_dirty BOOLEAN DEFAULT false;"));
     }
     if (!$this->migrate->getCommandsRun()) {
         echo "Your database is already up to date.\n\n";
     } else {
         echo $this->migrate->getCommandsRun() . " SQL commands were run.\n\n";
     }
     if (!$this->migrate->tableExists('a_group_access')) {
         // They don't have a group access table yet. In theory, they don't have an editor permission
         // to grant to groups yet either. However that is a likely name for them to invent on their
         // own, so make sure we don't panic if there is already a permission called eidtor
         $this->migrate->sql(array('CREATE TABLE a_group_access (id BIGINT AUTO_INCREMENT, page_id INT, privilege VARCHAR(100), group_id INT, INDEX pageindex_idx (page_id), INDEX group_id_idx (group_id), PRIMARY KEY(id)) ENGINE = INNODB;', 'ALTER TABLE a_group_access ADD CONSTRAINT a_group_access_page_id_a_page_id FOREIGN KEY (page_id) REFERENCES a_page(id) ON DELETE CASCADE;', 'ALTER TABLE a_group_access ADD CONSTRAINT a_group_access_group_id_sf_guard_group_id FOREIGN KEY (group_id) REFERENCES sf_guard_group(id) ON DELETE CASCADE;', 'INSERT INTO sf_guard_permission (name, description) VALUES ("editor", "For groups that will be granted editing privileges at some point in the site") ON DUPLICATE KEY UPDATE id = id;'));
     }
     $viewLocked = sfConfig::get('app_a_view_locked_sufficient_credentials', 'view_locked');
     // If they haven't customized it make sure it exists. Some pkContextCMS sites might not have it
     if ($viewLocked === 'view_locked') {
         $permission = Doctrine::getTable('sfGuardPermission')->findOneByName($viewLocked);
         if (!$permission) {
             $permission = new sfGuardPermission();
             $permission->setName('view_locked');
             $permission->save();
             $groups = array('editor', 'admin');
             foreach ($groups as $group) {
                 $g = Doctrine::getTable('sfGuardGroup')->findOneByName($group);
                 if ($g) {
                     $pg = new sfGuardGroupPermission();
                     $pg->setGroupId($g->id);
                     $pg->setPermissionId($permission->id);
                     $pg->save();
                 }
             }
         }
     }
     if (!$this->migrate->tableExists('a_category') || !$this->migrate->tableExists('a_category_group')) {
         $this->migrate->sql(array("CREATE TABLE IF NOT EXISTS a_category (id INT AUTO_INCREMENT, name VARCHAR(255) UNIQUE, description TEXT, created_at DATETIME NOT NULL, updated_at DATETIME NOT NULL, slug VARCHAR(255), UNIQUE INDEX a_category_sluggable_idx (slug), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ENGINE = INNODB;", "CREATE TABLE IF NOT EXISTS a_category_group (category_id INT, group_id INT, PRIMARY KEY(category_id, group_id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ENGINE = INNODB;", "CREATE TABLE IF NOT EXISTS a_category_user (category_id INT, user_id INT, PRIMARY KEY(category_id, user_id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ENGINE = INNODB;", "CREATE TABLE `a_page_to_category` (\n          `page_id` INT NOT NULL DEFAULT '0',\n          `category_id` INT NOT NULL DEFAULT '0',\n          PRIMARY KEY (`page_id`,`category_id`),\n          KEY `a_page_to_category_category_id_a_category_id` (`category_id`),\n          CONSTRAINT `a_page_to_category_category_id_a_category_id` FOREIGN KEY (`category_id`) REFERENCES `a_category` (`id`) ON DELETE CASCADE,\n          CONSTRAINT `a_page_to_category_page_id_a_page_id` FOREIGN KEY (`page_id`) REFERENCES `a_page` (`id`) ON DELETE CASCADE\n        ) ENGINE=InnoDB DEFAULT CHARSET=utf8", "CREATE TABLE IF NOT EXISTS `a_media_item_to_category` (\n          `media_item_id` INT NOT NULL DEFAULT '0',\n          `category_id` INT NOT NULL DEFAULT '0',\n          PRIMARY KEY (`media_item_id`,`category_id`),\n          KEY `a_media_item_to_category_category_id_a_category_id` (`category_id`)\n        ) ENGINE=InnoDB DEFAULT CHARSET=utf8"));
         // These constraints might already be present, be tolerant
         $constraints = array("ALTER TABLE a_media_item_to_category ADD CONSTRAINT `a_media_item_to_category_category_id_a_category_id` FOREIGN KEY (`category_id`) REFERENCES `a_category` (`id`) ON DELETE CASCADE", "ALTER TABLE a_media_item_to_category ADD CONSTRAINT `a_media_item_to_category_media_item_id_a_media_item_id` FOREIGN KEY (`media_item_id`) REFERENCES `a_media_item` (`id`) ON DELETE CASCADE", "ALTER TABLE a_category_group ADD CONSTRAINT a_category_group_group_id_sf_guard_group_id FOREIGN KEY (group_id) REFERENCES sf_guard_group(id) ON DELETE CASCADE;", "ALTER TABLE a_category_group ADD CONSTRAINT a_category_group_category_id_a_category_id FOREIGN KEY (category_id) REFERENCES a_category(id) ON DELETE CASCADE;", "ALTER TABLE a_category_user ADD CONSTRAINT a_category_user_user_id_sf_guard_user_id FOREIGN KEY (user_id) REFERENCES sf_guard_user(id) ON DELETE CASCADE;", "ALTER TABLE a_category_user ADD CONSTRAINT a_category_user_category_id_a_category_id FOREIGN KEY (category_id) REFERENCES a_category(id) ON DELETE CASCADE;");
         foreach ($constraints as $c) {
             try {
                 $this->migrate->sql(array($c));
             } catch (Exception $e) {
                 echo "Error creating constraint, most likely already exists, which is OK {$c}\n";
             }
         }
         if ($this->migrate->tableExists('a_media_category')) {
             $oldCategories = $this->migrate->query('SELECT * FROM a_media_category');
         } else {
             $oldCategories = array();
         }
         $newCategories = $this->migrate->query('SELECT * FROM a_category');
         $nc = array();
         foreach ($newCategories as $newCategory) {
             $nc[$newCategory['slug']] = $newCategory;
         }
         $oldIdToNewId = array();
         echo "Migrating media categories to Apostrophe categories...\n";
         foreach ($oldCategories as $category) {
             if (isset($nc[$category['slug']])) {
                 $oldIdToNewId[$category['id']] = $nc[$category['slug']]['id'];
             } else {
                 $this->migrate->query('INSERT INTO a_category (name, description, slug) VALUES (:name, :description, :slug)', $category);
                 $oldIdToNewId[$category['id']] = $this->migrate->lastInsertId();
             }
         }
         echo "Migrating from aMediaItemCategory to aMediaItemToCategory...\n";
         $oldMappings = $this->migrate->query('SELECT * FROM a_media_item_category');
         foreach ($oldMappings as $info) {
             $info['category_id'] = $oldIdToNewId[$info['media_category_id']];
             $this->migrate->query('INSERT INTO a_media_item_to_category (media_item_id, category_id) VALUES (:media_item_id, :category_id)', $info);
         }
     }
     if (!$this->migrate->tableExists('a_embed_media_account')) {
         $this->migrate->sql(array('CREATE TABLE a_embed_media_account (id INT AUTO_INCREMENT, service VARCHAR(100) NOT NULL, username VARCHAR(100) NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ENGINE = INNODB;'));
     }
     if (!$this->migrate->columnExists('a_page', 'edit_admin_lock')) {
         $this->migrate->sql(array('ALTER TABLE a_page ADD COLUMN edit_admin_lock TINYINT(1) DEFAULT "0"', 'ALTER TABLE a_page ADD COLUMN view_admin_lock TINYINT(1) DEFAULT "0"'));
         $options = array('application' => $options['application'], 'env' => $options['env'], 'connection' => $options['connection']);
         $postTasks[] = array('task' => new apostropheCascadeEditPermissionsTask($this->dispatcher, $this->formatter), 'arguments' => array(), 'options' => $options);
     }
     if (!$this->migrate->columnExists('a_page', 'view_guest')) {
         $this->migrate->sql(array('ALTER TABLE a_page ADD COLUMN view_guest TINYINT(1) DEFAULT "1"'));
         $options = array('application' => $options['application'], 'env' => $options['env'], 'connection' => $options['connection']);
         $postTasks[] = array('task' => new apostropheCascadeEditPermissionsTask($this->dispatcher, $this->formatter), 'arguments' => array(), 'options' => $options);
     }
     // Migrate all IDs to BIGINT (the default in Doctrine 1.2) for compatibility with the
     // new version of sfDoctrineGuardPlugin. NOTE: we continue to use INT in create table
     // statements BEFORE this point because we need to set up relations with what they already
     // have - this call will clean that up
     $this->migrate->upgradeIds();
     // Upgrade all charsets to UTF-8 otherwise we can't store a lot of what comes back from embed services
     $this->migrate->upgradeCharsets();
     // We can add these constraints now that we have IDs of the right size
     if (!$this->migrate->constraintExists('a_media_item_to_category', 'a_media_item_to_category_category_id_a_category_id')) {
         $this->migrate->sql(array('ALTER TABLE a_media_item_to_category MODIFY COLUMN category_id BIGINT', 'ALTER TABLE a_media_item_to_category MODIFY COLUMN media_item_id BIGINT', "ALTER TABLE a_media_item_to_category ADD CONSTRAINT `a_media_item_to_category_category_id_a_category_id` FOREIGN KEY (`category_id`) REFERENCES `a_category` (`id`) ON DELETE CASCADE", "ALTER TABLE a_media_item_to_category ADD CONSTRAINT `a_media_item_to_category_media_item_id_a_media_item_id` FOREIGN KEY (`media_item_id`) REFERENCES `a_media_item` (`id`) ON DELETE CASCADE"));
     }
     // sfDoctrineGuardPlugin 5.0.x requires this
     if (!$this->migrate->columnExists('sf_guard_user', 'email_address')) {
         $this->migrate->sql(array('ALTER TABLE sf_guard_user ADD COLUMN first_name varchar(255) DEFAULT NULL', 'ALTER TABLE sf_guard_user ADD COLUMN last_name varchar(255) DEFAULT NULL', 'ALTER TABLE sf_guard_user ADD COLUMN email_address varchar(255) DEFAULT \'\''));
         // Email addresses are mandatory and can't be null. We can't start guessing whether
         // you have them in some other table or not. So the best we can do is stub in
         // the username for uniqueness for now
         $this->migrate->sql(array('UPDATE sf_guard_user SET email_address = concat(username, \'@notavalidaddress\')', 'ALTER TABLE sf_guard_user ADD UNIQUE KEY `email_address` (`email_address`);'));
     }
     if (!$this->migrate->tableExists('sf_guard_forgot_password')) {
         $this->migrate->sql(array('
     CREATE TABLE `sf_guard_forgot_password` (
       `id` bigint(20) NOT NULL AUTO_INCREMENT,
       `user_id` bigint(20) NOT NULL,
       `unique_key` varchar(255) DEFAULT NULL,
       `expires_at` datetime NOT NULL,
       `created_at` datetime NOT NULL,
       `updated_at` datetime NOT NULL,
       PRIMARY KEY (`id`),
       KEY `user_id_idx` (`user_id`),
       CONSTRAINT `sf_guard_forgot_password_user_id_sf_guard_user_id` FOREIGN KEY (`user_id`) REFERENCES `sf_guard_user` (`id`) ON DELETE CASCADE
     ) ENGINE=InnoDB DEFAULT CHARSET=utf8'));
     }
     if (!$this->migrate->columnExists('a_page', 'published_at')) {
         $this->migrate->sql(array('ALTER TABLE a_page ADD COLUMN published_at DATETIME DEFAULT NULL', 'UPDATE a_page SET published_at = created_at WHERE published_at IS NULL'));
     }
     // Remove any orphaned media items created by insufficiently carefully written embed services,
     // these can break the media repository
     $this->migrate->sql(array('DELETE FROM a_media_item WHERE type="video" AND embed IS NULL AND service_url IS NULL'));
     // Rename any tags with slashes in them to avoid breaking routes in Symfony
     $this->migrate->sql(array('UPDATE tag SET name = replace(name, "/", "-")'));
     // A chance to make plugin- and project-specific additions to the schema before Doctrine queries fail to see them
     sfContext::getInstance()->getEventDispatcher()->notify(new sfEvent($this->migrate, "a.migrateSchemaAdditions"));
     $mediaEnginePage = Doctrine::getTable('aPage')->createQuery('p')->where('p.admin IS TRUE AND p.engine = "aMedia"')->fetchOne();
     if (!$mediaEnginePage) {
         $mediaEnginePage = new aPage();
         $root = aPageTable::retrieveBySlug('/');
         $mediaEnginePage->getNode()->insertAsFirstChildOf($root);
     }
     $mediaEnginePage->slug = '/admin/media';
     $mediaEnginePage->engine = 'aMedia';
     $mediaEnginePage->setAdmin(true);
     $mediaEnginePage->setPublishedAt(aDate::mysql());
     $new = $mediaEnginePage->isNew();
     $mediaEnginePage->save();
     if ($new) {
         $mediaEnginePage->setTitle('Media');
     }
     echo "Ensured there is an admin media engine\n";
     echo "Finished updating tables.\n";
     if (count($postTasks)) {
         echo "Invoking post-migration tasks...\n";
         foreach ($postTasks as $taskInfo) {
             $taskInfo['task']->run($taskInfo['arguments'], $taskInfo['options']);
         }
     }
     echo "Done!\n";
 }