public function projects($option = 'summary', $action = 'list', $id = null) { $user = $_SESSION['user']; $errors = array(); // verificación de proyectos y proyecto de trabajo list($project, $projects) = Dashboard\Projects::verifyProject($user, $action); // teniendo proyecto de trabajo, comprobar si el proyecto esta en estado de tener blog if ($option == 'updates') { $blog = Dashboard\Projects::verifyBlog($project); } // sacaexcel de cofinanciadores if ($option == 'rewards' && $action == 'table') { $response = new \Goteo\Controller\Sacaexcel(); return $response->index('investors', $project->id); } // ojo si no tiene retornos if ($option == 'commons' && empty($project->social_rewards)) { Message::Error('Este proyecto no tiene retornos colectivos'); throw new Redirection('/dashboard/projects/'); } // procesamiento de formularios if ($_SERVER['REQUEST_METHOD'] == 'POST') { switch ($option) { // gestionar retornos (o mensaje a los mensajeros) case 'messegers': case 'rewards': // sacamos del post el filtro y el orden if ($action == 'filter') { $_SESSION['dashboard-rewards-filter'] = isset($_POST['filter']) ? $_POST['filter'] : $_SESSION['dashboard-rewards-filter']; $_SESSION['dashboard-rewards-order'] = isset($_POST['order']) ? $_POST['order'] : $_SESSION['dashboard-rewards-order']; } //procesamos el envio de mails if ($action == 'message') { Dashboard\Projects::process_mailing($option, $project); // y lo devolvemos a donde estaba throw new Redirection('/dashboard/projects/' . $option); } break; // colaboraciones // colaboraciones case 'supports': if ($action == 'save') { $project = Dashboard\Projects::process_supports($project, $errors); } break; case 'updates': // verificación: si no llega blog correcto no lo procesamos if (empty($_POST['blog']) || $_POST['blog'] != $blog->id) { throw new Redirection('/dashboard/projects/summary'); } list($action, $id) = Dashboard\Projects::process_updates($action, $project, $errors); break; } } // SubControlador para add, edit, delete y list // devuelve $post en las acciones add y edit y $posts en delete y list // maneja por referencia $action, $posts y $errors if ($option == 'updates') { list($post, $posts) = Dashboard\Projects::prepare_updates($action, $id, $blog->id); } // view data basico para esta seccion $viewData = array('menu' => self::menu(), 'section' => __FUNCTION__, 'option' => $option, 'action' => $action, 'projects' => $projects, 'errors' => $errors); switch ($option) { case 'summary': // los datos json de invests y visitors_data $viewData['data'] = Dashboard\Projects::graph($project->id); break; // gestionar recompensas // gestionar recompensas case 'rewards': // recompensas ofrecidas $viewData['rewards'] = Model\Project\Reward::getAll($project->id, 'individual', LANG); // aportes para este proyecto $viewData['invests'] = Model\Invest::getAll($project->id); // ver por (esto son orden y filtros) $viewData['filter'] = $_SESSION['dashboard-rewards-filter']; $viewData['order'] = $_SESSION['dashboard-rewards-order']; break; // gestionar retornos // gestionar retornos case 'commons': $icons = Model\Icon::getAll('social'); foreach ($icons as $key => $icon) { $icons[$key] = $icon->name; } $viewData['icons'] = $icons; break; // listar mensajeadores // listar mensajeadores case 'messegers': $viewData['messegers'] = Model\Message::getMessegers($project->id); break; // editar colaboraciones // editar colaboraciones case 'supports': $viewData['types'] = Model\Project\Support::types(); // para mantener registros desplegados if ($_POST) { foreach ($_POST as $k => $v) { if (!empty($v) && preg_match('/support-(\\d+)-edit/', $k, $r)) { $viewData[$k] = true; break; } } } if (!empty($_POST['support-add'])) { $last = end($project->supports); if ($last !== false) { $viewData['support-' . $last->id . '-edit'] = true; } } $project->supports = Model\Project\Support::getAll($project->id); break; // publicar actualizaciones // publicar actualizaciones case 'updates': $viewData['blog'] = $blog; $viewData['posts'] = $posts; $viewData['post'] = $post; break; } $viewData['project'] = $project; return new View('view/dashboard/index.html.php', $viewData); }
/** * actualiza en la tabla los datos del proyecto * @param array $project->errors para guardar los errores de datos del formulario, los errores de proceso se guardan en $project->errors['process'] */ public function save(&$errors = array()) { if ($this->dontsave) { return false; } if (!$this->validate($errors)) { return false; } try { // fail para pasar por todo antes de devolver false $fail = false; // los nif sin guiones, espacios ni puntos $this->contract_nif = str_replace(array('_', '.', ' ', '-', ',', ')', '('), '', $this->contract_nif); $this->entity_cif = str_replace(array('_', '.', ' ', '-', ',', ')', '('), '', $this->entity_cif); // Image if (is_array($this->image) && !empty($this->image['name'])) { $image = new Image($this->image); if ($image->save($errors)) { $this->gallery[] = $image; $this->image = $image->id; /** * Guarda la relación NM en la tabla 'project_image'. */ if (!empty($image->id)) { self::query("REPLACE project_image (project, image) VALUES (:project, :image)", array(':project' => $this->id, ':image' => $image->id)); } } } $fields = array('contract_name', 'contract_nif', 'contract_email', 'contract_entity', 'contract_birthdate', 'entity_office', 'entity_name', 'entity_cif', 'phone', 'address', 'zipcode', 'location', 'country', 'secondary_address', 'post_address', 'post_zipcode', 'post_location', 'post_country', 'name', 'subtitle', 'image', 'description', 'motivation', 'video', 'video_usubs', 'about', 'goal', 'related', 'reward', 'keywords', 'media', 'media_usubs', 'currently', 'project_location', 'scope', 'resource', 'comment', 'evaluation'); $set = ''; $values = array(); foreach ($fields as $field) { if ($set != '') { $set .= ', '; } $set .= "{$field} = :{$field}"; $values[":{$field}"] = $this->{$field}; } // Solamente marcamos updated cuando se envia a revision desde el superform o el admin // $set .= ", updated = :updated"; // $values[':updated'] = date('Y-m-d'); $values[':id'] = $this->id; $sql = "UPDATE project SET " . $set . " WHERE id = :id"; if (!self::query($sql, $values)) { $errors[] = $sql . '<pre>' . print_r($values, 1) . '</pre>'; $fail = true; } // echo "$sql<br />"; // y aquí todas las tablas relacionadas // cada una con sus save, sus new y sus remove // quitar las que tiene y no vienen // añadir las que vienen y no tiene //categorias $tiene = Project\Category::get($this->id); $viene = $this->categories; $quita = array_diff_assoc($tiene, $viene); $guarda = array_diff_assoc($viene, $tiene); foreach ($quita as $key => $item) { $category = new Project\Category(array('id' => $item, 'project' => $this->id)); if (!$category->remove($errors)) { $fail = true; } } foreach ($guarda as $key => $item) { if (!$item->save($errors)) { $fail = true; } } // recuperamos las que le quedan si ha cambiado alguna if (!empty($quita) || !empty($guarda)) { $this->categories = Project\Category::get($this->id); } //skills $tiene = Project\Skill::get($this->id); $viene = $this->skills; $quita = array_diff_assoc($tiene, $viene); $guarda = array_diff_assoc($viene, $tiene); foreach ($quita as $key => $item) { $skill = new Project\Skill(array('id' => $item, 'project' => $this->id)); if (!$skill->remove($errors)) { $fail = true; } } foreach ($guarda as $key => $item) { if (!$item->save($errors)) { $fail = true; } } // recuperamos las que le quedan si ha cambiado alguna if (!empty($quita) || !empty($guarda)) { $this->skills = Project\Skill::get($this->id); } //costes $tiene = Project\Cost::getAll($this->id); $viene = $this->costs; $quita = array_diff_key($tiene, $viene); $guarda = array_diff_key($viene, $tiene); foreach ($quita as $key => $item) { if (!$item->remove($errors)) { $fail = true; } else { unset($tiene[$key]); } } foreach ($guarda as $key => $item) { if (!$item->save($errors)) { $fail = true; } } /* Ahora, los que tiene y vienen. Si el contenido es diferente, hay que guardarlo*/ foreach ($tiene as $key => $row) { // a ver la diferencia con el que viene if ($row != $viene[$key]) { if (!$viene[$key]->save($errors)) { $fail = true; } } } if (!empty($quita) || !empty($guarda)) { $this->costs = Project\Cost::getAll($this->id); } // recalculo de minmax $this->minmax(); //retornos colectivos $tiene = Project\Reward::getAll($this->id, 'social'); $viene = $this->social_rewards; $quita = array_diff_key($tiene, $viene); $guarda = array_diff_key($viene, $tiene); foreach ($quita as $key => $item) { if (!$item->remove($errors)) { $fail = true; } else { unset($tiene[$key]); } } foreach ($guarda as $key => $item) { if (!$item->save($errors)) { $fail = true; } } /* Ahora, los que tiene y vienen. Si el contenido es diferente, hay que guardarlo*/ foreach ($tiene as $key => $row) { // a ver la diferencia con el que viene if ($row != $viene[$key]) { if (!$viene[$key]->save($errors)) { $fail = true; } } } if (!empty($quita) || !empty($guarda)) { $this->social_rewards = Project\Reward::getAll($this->id, 'social'); } //recompenssas individuales $tiene = Project\Reward::getAll($this->id, 'individual'); $viene = $this->individual_rewards; $quita = array_diff_key($tiene, $viene); $guarda = array_diff_key($viene, $tiene); foreach ($quita as $key => $item) { if (!$item->remove($errors)) { $fail = true; } else { unset($tiene[$key]); } } foreach ($guarda as $key => $item) { if (!$item->save($errors)) { $fail = true; } } /* Ahora, los que tiene y vienen. Si el contenido es diferente, hay que guardarlo*/ foreach ($tiene as $key => $row) { // a ver la diferencia con el que viene if ($row != $viene[$key]) { if (!$viene[$key]->save($errors)) { $fail = true; } } } if (!empty($quita) || !empty($guarda)) { $this->individual_rewards = Project\Reward::getAll($this->id, 'individual'); } // colaboraciones $tiene = Project\Support::getAll($this->id); $viene = $this->supports; $quita = array_diff_key($tiene, $viene); // quitar los que tiene y no viene $guarda = array_diff_key($viene, $tiene); // añadir los que viene y no tiene foreach ($quita as $key => $item) { if (!$item->remove($errors)) { $fail = true; } else { unset($tiene[$key]); } } foreach ($guarda as $key => $item) { if (!$item->save($errors)) { $fail = true; } } /* Ahora, los que tiene y vienen. Si el contenido es diferente, hay que guardarlo*/ foreach ($tiene as $key => $row) { // a ver la diferencia con el que viene if ($row != $viene[$key]) { if (!$viene[$key]->save($errors)) { $fail = true; } } } if (!empty($quita) || !empty($guarda)) { $this->supports = Project\Support::getAll($this->id); } //listo return !$fail; } catch (\PDOException $e) { $errors[] = Text::_('No se ha grabado correctamente. ') . $e->getMessage(); //Text::get('save-project-fail'); return false; } }