/** * Update watch status * * @access public * @param string $foreign_table target related table * @param int $foreign_id target related table id * @return Response (json) * @throws Exception in Controller_Base::controller_common_api * @see Controller_Base::controller_common_api */ public function post_update_watch_status($foreign_table = null, $foreign_id = null) { $this->controller_common_api(function () use($foreign_table, $foreign_id) { if (!is_enabled('notice')) { throw new \HttpNotFoundException(); } if (\Input::post('foreign_table')) { $foreign_table = \Input::post('foreign_table'); } $foreign_id = intval(\Input::post('foreign_id') ?: $foreign_id); if (!$foreign_table || !$foreign_id) { throw new \HttpNotFoundException(); } if (!in_array($foreign_table, Site_Util::get_accept_foreign_tables())) { throw new \HttpNotFoundException(); } $this->response_body['errors']['message_default'] = term('form.watch') . '状態の変更に失敗しました。'; $model = \Site_Model::get_model_name($foreign_table); $foreign_obj = $model::check_authority($foreign_id); $member_id = $foreign_table == 'album_image' ? $foreign_obj->album->member_id : $foreign_obj->member_id; $this->check_browse_authority($foreign_obj->public_flag, $member_id); if ($member_id == $this->u->id) { throw new \HttpBadRequestException(); } \DB::start_transaction(); $is_registerd = (bool) Model_MemberWatchContent::change_registered_status4unique_key(array('foreign_table' => $foreign_table, 'foreign_id' => $foreign_id, 'member_id' => $this->u->id)); \DB::commit_transaction(); $data = array('result' => $is_registerd, 'message' => $is_registerd ? term('form.watch') . '対象に追加しました。' : term('form.watch') . 'を解除しました。', 'html' => icon_label($is_registerd ? 'form.do_unwatch' : 'form.do_watch', 'both', false)); $this->set_response_body_api($data); }); }
public static function save_comment($parent_table, $parent_id, $member_id, $body = null) { $model = Site_Model::get_model_name($parent_table . '_comment'); $parent_id_prop = $parent_table . '_id'; if (is_null($body)) { $body = 'This is test comment.'; } $comment = $model::forge(array('body' => $body, $parent_id_prop => $parent_id, 'member_id' => $member_id)); $comment->save(); return $comment; }
public static function get_model_field($table, $column, $label = '', $delete_rules = array()) { $model = Site_Model::get_model_name($table); $obj = $model::forge(); $props = $obj::get_property($column, $delete_rules); if (!$props || empty($props['form'])) { throw new \InvalidArgumentException('Second parameter is invalid.'); } if (!$label) { $label = !empty($props['label']) ? $props['label'] : ''; } return array('label' => $label, 'attributes' => $props['form'], 'rules' => !empty($props['validation']) ? Util_Array::convert_for_callback($props['validation']) : array()); }
public static function save_images($file_tmps, $parent_id, $parent_id_field, $related_table, $public_flag = null, $type = 'img') { if (!in_array($type, array('img', 'file'))) { throw new InvalidArgumentException('Parameter type is invalid.'); } $moved_files = array(); $related_table_ids = array(); if (!$file_tmps) { return array($moved_files, $related_table_ids); } $model = Site_Model::get_model_name($related_table); $related_table_obj = $model::forge(); if (!($file_cate = $related_table_obj->get_image_prefix())) { throw new \InvalidArgumentException('Parameter $related_table is invalid.'); } $new_filepath_prefix = Site_Upload::get_filepath_prefix($file_cate, $parent_id); $new_filename_prefix = Site_Upload::convert_filepath2filename($new_filepath_prefix); $is_save_storage = conf('upload.storageType') != 'normal'; if (!$is_save_storage) { $new_file_dir = Site_Upload::get_uploaded_path('raw', 'img', true, false, $new_filepath_prefix); if (!Site_Upload::check_and_make_uploaded_dir($new_file_dir, conf('upload.check_and_make_dir_level'), conf('upload.mkdir_mode'))) { throw newFuelException('Failed to make save dirs.'); } } foreach ($file_tmps as $id => $file_tmp) { $old_file_path = Site_Upload::get_uploaded_file_path($file_tmp->name, 'raw', 'img', true); $moved_files[$file_tmp->id] = array('from' => $old_file_path); if (!$is_save_storage) { $new_file_path = $new_file_dir . $file_tmp->name; Util_file::move($old_file_path, $new_file_path); $moved_files[$file_tmp->id]['to'] = $new_file_path; $moved_files[$file_tmp->id]['filepath_prefix'] = $new_filepath_prefix; } if ($type == 'img') { $moved_files[$file_tmp->id]['from_thumbnail'] = Site_Upload::get_uploaded_file_path($file_tmp->name, 'thumbnail', 'img', true); } $file = Model_File::move_from_file_tmp($file_tmp, $new_filename_prefix, $type); $related_table_obj = $model::forge(); $related_table_obj->{$parent_id_field} = $parent_id; $related_table_obj->file_name = $file->name; $related_table_obj->name = $file_tmp->description; $related_table_obj->shot_at = !empty($file->shot_at) ? $file->shot_at : date('Y-m-d H:i:s'); if (!is_null($public_flag)) { $related_table_obj->public_flag = $public_flag; } $related_table_obj->save(); $related_table_ids[] = $related_table_obj->id; } return array($moved_files, $related_table_ids); }
public static function check_and_create($foreign_table, $foreign_id, $type) { $since_datetime = \Date::forge(strtotime('-' . \Config::get('notice.periode_to_update.default')))->format('mysql'); if (!($obj = self::get_last4foreign_data($foreign_table, $foreign_id, $type, $since_datetime))) { $obj = self::forge(array('foreign_table' => $foreign_table, 'foreign_id' => $foreign_id, 'type' => $type, 'body' => Site_Util::get_notice_body($foreign_table, $type))); if (!in_array($foreign_table, Site_Util::get_accept_parent_tables()) && ($parent_table = \Site_Model::get_parent_table($foreign_table))) { $obj->parent_table = $parent_table; $foreign_obj_name = \Site_Model::get_model_name($foreign_table); $foreign_obj = $foreign_obj_name::find($foreign_id); $parent_id_prop = $parent_table . '_id'; $obj->parent_id = $foreign_obj->{$parent_id_prop}; } $obj->save(); } return $obj; }
public static function delete_file_all4member_id($member_id, $is_tmp = false, $limit = 0) { if (!$limit) { $limit = conf('batch.limit.delete.file', 10); } $model = Site_Model::get_model_name($is_tmp ? 'file_tmp' : 'file'); $query = $model::query(); if ($is_tmp) { $query->where('user_type', 0); } $query->where('member_id', $member_id)->limit($limit); while ($objs = $query->get()) { DB::start_transaction(); foreach ($objs as $obj) { $obj->delete(); } DB::commit_transaction(); } }
/** * Api delete common controller * * @access protected * @param string $table Delete target table * @param int $id Delete target record's id * @param string $method Excecuting method name * @param string $content_name Delete target content name for message * @return Response(json) */ protected function api_delete_common($table, $id = null, $method = null, $content_name = '') { $this->controller_common_api(function () use($table, $id, $method, $content_name) { if (!$method) { $method = 'delete'; } $id = intval(\Input::post('id') ?: $id); $model = Site_Model::get_model_name($table); $obj = $model::check_authority($id, IS_ADMIN ? 0 : $this->u->id); if (is_enabled('album') && $table == 'album') { if ($result = \Album\Site_Util::check_album_disabled_to_update($obj->foreign_table)) { throw new \DisableToUpdateException($result['message']); } } \DB::start_transaction(); if ($table == 'timeline') { $result = \Timeline\Site_Model::delete_timeline($obj, $this->u->id); } else { $result = $obj->{$method}(); } \DB::commit_transaction(); $target_conntent_name = $content_name ?: Site_Model::get_content_name($table); $data = array('result' => (bool) $result, 'message' => sprintf('%s%sしました。', $target_conntent_name ? $target_conntent_name . 'を' : '', term('form.delete'))); $this->set_response_body_api($data); }); }
public static function get_liked_ids($parent_table, $member_id, array $parent_objs, $member_id_prop = 'member_id') { if (!$parent_objs) { return array(); } $like_model = Site_Model::get_model_name($parent_table . '_like'); $parent_foreign_key = $parent_table . '_id'; return $like_model::get_cols($parent_foreign_key, array(array($member_id_prop => $member_id), array($parent_foreign_key, 'in', \Util_Orm::conv_col2array($parent_objs, 'id', true)))); }
/** * Create comment common api controller * * @access protected * @param string $parent_table target parent table * @param int $parent_id target parent record id * @param string $member_related related table for get member_id * @return Response (json) * @throws Exception in Controller_Base::controller_common_api * @see Controller_Base::controller_common_api */ protected function api_create_comment_common($parent_table, $parent_id = null, $member_related = null) { $this->controller_common_api(function () use($parent_table, $parent_id, $member_related) { $parent_id = intval(\Input::post('id') ?: $parent_id); $parent_model = Site_Model::get_model_name($parent_table); $parent_obj = $parent_model::check_authority($parent_id); $this->check_browse_authority($parent_obj->public_flag, $member_related ? $parent_obj->{$member_related}->member_id : $parent_obj->member_id); $parent_id_prop = $parent_table . '_id'; $model = Site_Model::get_model_name($parent_table . '_comment'); // Lazy validation $body = trim(\Input::post('body', '')); if (!strlen($body)) { throw new \ValidationFailedException(sprintf('%sの入力は%sです。', term('form.comment'), term('form.required'))); } if ($parent_table == 'timeline' && \Timeline\Site_Util::check_type_for_post_foreign_table_comment($parent_obj->type)) { throw new \HttpInvalidInputException(); } \DB::start_transaction(); // Create a new comment $comment = $model::forge(array('body' => $body, $parent_id_prop => $parent_id, 'member_id' => $this->u->id)); $result = (int) $comment->save(); \DB::commit_transaction(); $this->set_response_body_api(array('result' => $result, 'id' => $comment->id)); }); }