public function save(&$errors = array()) { if (!$this->validate($errors)) { return false; } // Primero la imagenImagen if (is_array($this->image) && !empty($this->image['name'])) { $image = new Image($this->image); if ($image->save($errors)) { $this->image = $image->id; } else { \Goteo\Library\Message::Error(Text::get('image-upload-fail') . implode(', ', $errors)); $this->image = ''; } } $fields = array('id', 'name', 'url', 'image', 'order'); $set = ''; $values = array(); foreach ($fields as $field) { if ($set != '') { $set .= ", "; } $set .= "`{$field}` = :{$field} "; $values[":{$field}"] = $this->{$field}; } try { $sql = "REPLACE INTO sponsor SET " . $set; self::query($sql, $values); if (empty($this->id)) { $this->id = self::insertId(); } Check::reorder($this->id, 'up', 'sponsor'); return true; } catch (\PDOException $e) { $errors[] = Text::_("No se ha guardado correctamente. ") . $e->getMessage(); return false; } }
/** * Guardar usuario. * Guarda los valores de la instancia del usuario en la tabla. * * @param type array $errors Errores devueltos pasados por referencia. * @param type array $skip_validations Crea el usuario aunque estos campos no sean correctos * password, active * @return type bool true|false */ public function save(&$errors = array(), $skip_validations = array()) { if ($this->validate($errors, $skip_validations)) { // Nuevo usuario. if (empty($this->id)) { // bcript $hashed = version_compare(phpversion(), '5.5.0', '>=') ? password_hash($this->password, PASSWORD_BCRYPT) : crypt($this->password); if (!$hashed) { $errors[Text::get('register-hash_fail')]; } $insert = true; $data[':id'] = $this->id = static::idealiza($this->userid); $data[':name'] = $this->name; $data[':location'] = $this->location; $data[':email'] = $this->email; $data[':token'] = $token = md5(uniqid()); if (!in_array('password', $skip_validations)) { $data[':password'] = $hashed; } $data[':created'] = date('Y-m-d H:i:s'); $data[':active'] = true; $data[':confirmed'] = false; $data[':lang'] = \LANG; $data[':node'] = \NODE_ID; //active = 1 si no se quiere comprovar if (in_array('active', $skip_validations) && $this->active) { $data[':active'] = 1; } else { // Obtenemos la plantilla para asunto y contenido $template = Template::get(5); // Sustituimos los datos $subject = $template->title; // En el contenido: $search = array('%USERNAME%', '%USERID%', '%ACTIVATEURL%'); $replace = array($this->name, $this->id, SITE_URL . '/user/activate/' . $token); $content = \str_replace($search, $replace, $template->text); // Activación $mail = new Mail(); $mail->to = $this->email; $mail->toName = $this->name; $mail->subject = $subject; $mail->content = $content; $mail->html = false; $mail->template = $template->id; if ($mail->send($errors)) { Message::Info(Text::get('register-confirm_mail-success')); } else { Message::Error(Text::get('register-confirm_mail-fail', GOTEO_MAIL)); Message::Error(implode('<br />', $errors)); } } } else { $data[':id'] = $this->id; // E-mail if (!empty($this->email)) { if (count($tmp = explode('¬', $this->email)) > 1) { $data[':email'] = $tmp[1]; $data[':token'] = null; } else { $query = self::query('SELECT email FROM user WHERE id = ?', array($this->id)); if ($this->email !== $query->fetchColumn()) { $this->token = md5(uniqid()) . '¬' . $this->email . '¬' . date('Y-m-d'); } } } // Contraseña if (!empty($this->password)) { // bcript $hashed = version_compare(phpversion(), '5.5.0', '>=') ? password_hash($this->password, PASSWORD_BCRYPT) : crypt($this->password); if (!$hashed) { $errors[Text::get('register-hash_fail')]; } $data[':password'] = $hashed; static::query('DELETE FROM user_login WHERE user= ?', $this->id); } if (!is_null($this->active)) { $data[':active'] = $this->active; } if (!is_null($this->confirmed)) { $data[':confirmed'] = $this->confirmed; } if (!is_null($this->hide)) { $data[':hide'] = $this->hide; } // Avatar if (is_array($this->avatar) && !empty($this->avatar['name'])) { $image = new Image($this->avatar); if ($image->save($errors)) { $data[':avatar'] = $image->id; } else { unset($data[':avatar']); } } // Perfil público if (isset($this->name)) { $data[':name'] = $this->name; } // Dónde está if (isset($this->location)) { $data[':location'] = $this->location; } if (isset($this->about)) { $data[':about'] = $this->about; } if (isset($this->keywords)) { $data[':keywords'] = $this->keywords; } if (isset($this->contribution)) { $data[':contribution'] = $this->contribution; } if (isset($this->facebook)) { $data[':facebook'] = $this->facebook; } if (isset($this->google)) { $data[':google'] = $this->google; } if (isset($this->twitter)) { $data[':twitter'] = $this->twitter; } if (isset($this->identica)) { $data[':identica'] = $this->identica; } if (isset($this->linkedin)) { $data[':linkedin'] = $this->linkedin; } // Intereses $interests = User\Interest::get($this->id); if (!empty($this->interests)) { foreach ($this->interests as $interest) { if (!in_array($interest, $interests)) { $_interest = new User\Interest(); $_interest->id = $interest; $_interest->user = $this->id; $_interest->save($errors); $interests[] = $_interest; } } } foreach ($interests as $key => $interest) { if (!in_array($interest, $this->interests)) { $_interest = new User\Interest(); $_interest->id = $interest; $_interest->user = $this->id; $_interest->remove($errors); } } // Webs static::query('DELETE FROM user_web WHERE user= ?', $this->id); if (!empty($this->webs)) { foreach ($this->webs as $web) { if ($web instanceof User\Web) { $web->user = $this->id; $web->save($errors); } } } } try { // Construye SQL. if (isset($insert) && $insert == true) { $query = "INSERT INTO user ("; foreach ($data as $key => $row) { $query .= substr($key, 1) . ", "; } $query = substr($query, 0, -2) . ") VALUES ("; foreach ($data as $key => $row) { $query .= $key . ", "; } $query = substr($query, 0, -2) . ")"; } else { $query = "UPDATE user SET "; foreach ($data as $key => $row) { if ($key != ":id") { $query .= substr($key, 1) . " = " . $key . ", "; } } $query = substr($query, 0, -2) . " WHERE id = :id"; } // Ejecuta SQL. return self::query($query, $data); } catch (\PDOException $e) { $errors[] = Text::_("No se ha grabado correctamente. ") . $e->getMessage(); return false; } } return false; }
public function save(&$errors = array()) { if (!$this->validate($errors)) { return false; } $fields = array('id', 'title', 'text', 'legend', 'media'); $values = array(); foreach ($fields as $field) { if ($set != '') { $set .= ", "; } $set .= "`{$field}` = :{$field} "; $values[":{$field}"] = $this->{$field}; } try { $sql = "REPLACE INTO glossary SET " . $set; self::query($sql, $values); if (empty($this->id)) { $this->id = self::insertId(); } // Luego la imagen if (!empty($this->id) && is_array($this->image) && !empty($this->image['name'])) { $image = new Image($this->image); if ($image->save($errors)) { $this->gallery[] = $image; /** * Guarda la relación NM en la tabla 'glossary_image'. */ if (!empty($image->id)) { self::query("REPLACE glossary_image (glossary, image) VALUES (:glossary, :image)", array(':glossary' => $this->id, ':image' => $image->id)); } } else { Message::Error(Text::get('image-upload-fail') . implode(', ', $errors)); } } return true; } catch (\PDOException $e) { $errors[] = Text::_("No se ha guardado correctamente. ") . $e->getMessage(); return false; } }
public function save(&$errors = array()) { if (empty($this->blog)) { return false; } $fields = array('id', 'blog', 'title', 'text', 'media', 'legend', 'date', 'allow', 'publish', 'home', 'footer', 'author'); $values = array(); foreach ($fields as $field) { if ($set != '') { $set .= ", "; } $set .= "`{$field}` = :{$field} "; $values[":{$field}"] = $this->{$field}; } try { $sql = "REPLACE INTO post SET " . $set; self::query($sql, $values); if (empty($this->id)) { $this->id = self::insertId(); } // Luego la imagen if (!empty($this->id) && 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 'post_image'. */ if (!empty($image->id)) { self::query("REPLACE post_image (post, image) VALUES (:post, :image)", array(':post' => $this->id, ':image' => $image->id)); } } else { Message::Error(Text::get('image-upload-fail') . implode(', ', $errors)); } } // y los tags, si hay if (!empty($this->id) && is_array($this->tags)) { static::query('DELETE FROM post_tag WHERE post= ?', $this->id); foreach ($this->tags as $tag) { $new = new Post\Tag(array('post' => $this->id, 'tag' => $tag)); $new->assign($errors); unset($new); } } return true; } catch (\PDOException $e) { $errors[] = Text::_("No se ha guardado correctamente. ") . $e->getMessage(); return false; } }
public function save(&$errors = array()) { // if (!$this->validate($errors)) return false; // Imagen de fondo de banner if (is_array($this->image) && !empty($this->image['name'])) { $image = new Image($this->image); if ($image->save()) { $this->image = $image->id; } else { \Goteo\Library\Message::Error(Text::get('image-upload-fail') . implode(', ', $errors)); $this->image = ''; } } $fields = array('id', 'node', 'title', 'description', 'url', 'project', 'image', 'order', 'active'); $set = ''; $values = array(); foreach ($fields as $field) { if ($set != '') { $set .= ", "; } $set .= "`{$field}` = :{$field} "; $values[":{$field}"] = $this->{$field}; } try { $sql = "REPLACE INTO banner SET " . $set; self::query($sql, $values); if (empty($this->id)) { $this->id = self::insertId(); } return true; } catch (\PDOException $e) { $errors[] = Text::_("No se ha guardado correctamente. ") . $e->getMessage(); return false; } }
/** * 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; } }