<?php

/**
 * UserGroup
 * 
 * This class has been auto-generated by the Doctrine ORM Framework
 * 
 * @package    sf
 * @subpackage model
 * @author     Atma
 * @version    SVN: $Id: Builder.php 7490 2010-03-29 19:53:27Z jwage $
 */
class UserGroup extends PluginUserGroup
{
    public static function build(sfWebRequest $request, $obj, $allow_group_id = false, $from_group_id = false)
    {
        $field = trim(strtolower(preg_replace('/([A-Z]{1})/', '_$1', get_class($obj))), '_');
        $group = Doctrine_Query::create()
            ->select("*")
            ->from("UserGroup")
            ->where($field . "_id = " . $obj->getId())
            ->fetchOne();
        if(!$group){
            $group = new UserGroup();
            $group['title'] = $field . '_' . $obj->getId();
            $group[$field . '_id'] = $obj->getId();
            $group->save();
        }
        if($from_group_id){
            Doctrine_Query::create()
                ->delete("UserGroupPermissions")
                ->where("user_group_id = ?", $group->getId())
                ->execute();
            $from_group_permissions = Doctrine_Query::create()
                ->select("*")
                ->from("UserGroupPermissions")
                ->where("user_group_id = ?", $from_group_id)
                ->fetchArray();
            if(count($from_group_permissions) > 0){
                foreach ($from_group_permissions as $from_group_permission){
                    $group_permissions = new UserGroupPermissions();
                    $group_permissions->setPermissionId($from_group_permission['permission_id']);
                    $group_permissions->setUserGroupId($group->getId());
                    $group_permissions->save();
                }
            }
        }else{
            $values = $request->getParameter($field . '_permissions');
            $values = is_array($values) ? $values : array();

            $existing = ProjectUtils::arrayKeysFilter($group->getUserGroupPermissions(), 'id');
            $unlink = array_diff($existing, $values);
            if (count($unlink) > 0) {
                Doctrine_Query::create()
                    ->delete("UserGroupPermissions")
                    ->whereIn("permission_id", array_values($unlink))
                    ->andWhere("user_group_id= ?", $group->getId())
                    ->execute();
            }
            $link = array_diff($values, $existing);

            if($allow_group_id && is_numeric($allow_group_id)){
                $permissions = Doctrine_Query::create()
                    ->select("p.id")
                    ->from("Permission p INDEXBY p.id")
                    ->innerJoin("p.UserGroupPermissions u")
                    ->where("u.user_group_id = ?", $allow_group_id)
                    ->fetchArray();
                $allow_permissions = array_keys($permissions);
            }
            if (count($link) > 0) {
                foreach ($link as $id) {
                    if(isset($allow_permissions) && !in_array($id, $allow_permissions)){
                        continue;
                    }
                    $group_permissions = new UserGroupPermissions();
                    $group_permissions->setPermissionId($id);
                    $group_permissions->setUserGroupId($group->getId());
                    $group_permissions->save();
                }
            }
        }
    }
}
