diff --git a/3.0/modules/star/controllers/admin_star.php b/3.0/modules/star/controllers/admin_star.php new file mode 100644 index 00000000..983618f2 --- /dev/null +++ b/3.0/modules/star/controllers/admin_star.php @@ -0,0 +1,52 @@ +page_title = t("Star settings"); + $view->content = new View("admin_star.html"); + $view->content->form = $this->_get_admin_form(); + $view->content->title = $view->page_title; + print $view; + } + + public function save() { + access::verify_csrf(); + $form = $this->_get_admin_form(); + $form->validate(); + module::set_var("star", "show", + $form->show->value); + message::success(t("Star settings updated")); + url::redirect("admin/star"); + } + + private function _get_admin_form() { + $form = new Forge("admin/star/save", "", "post", + array("id" => "g-star-admin-form")); + $form->dropdown("show") + ->label(t("Default to showing...")) + ->options(array(0 => "All",1 => "Starred")) + ->selected(module::get_var("star", "show")); + $form->submit("save")->value(t("Save")); + return $form; + } +} diff --git a/3.0/modules/star/controllers/display.php b/3.0/modules/star/controllers/display.php new file mode 100644 index 00000000..cf2fe805 --- /dev/null +++ b/3.0/modules/star/controllers/display.php @@ -0,0 +1,94 @@ + %title item", array("title" => html::purify($item->title))); + + $this->_check_star_permissions($item); + star::star($item); + message::success($msg); + + json::reply(array("result" => "success", "reload" => 1)); + } + + /** + * Allows the given item to be displayed again. + * + * @param int $id the item id + */ + public function unstar($id) { + $item = model_cache::get("item", $id); + $msg = t("Un-starred %title item", array("title" => html::purify($item->title))); + + $this->_check_star_permissions($item); + star::unstar($item); + message::success($msg); + + json::reply(array("result" => "success", "reload" => 1)); + } + + public function star_only_on() { + //$item = model_cache::get("item", $id); + access::verify_csrf(); + $msg = t("Showing starred items."); + + //$this->_check_star_permissions($item); + star::star_only_on(); + message::success($msg); + + json::reply(array("result" => "success", "reload" => 1)); + } + +public function star_only_off() { + //$item = model_cache::get("item", $id); + access::verify_csrf(); + $msg = t("Showing all items."); + + //$this->_check_star_permissions($item); + star::star_only_off(); + message::success($msg); + + json::reply(array("result" => "success", "reload" => 1)); + } + + /** + * Checks whether the given object can be starred by the active user. + * + * @param Item_Model $item the item + */ + private function _check_star_permissions(Item_Model $item) { + access::verify_csrf(); + + access::required("view", $item); + access::required("edit", $item); + + if (!star::can_star()) { + access::forbidden(); + } + } +} diff --git a/3.0/modules/star/helpers/MY_item.php b/3.0/modules/star/helpers/MY_item.php new file mode 100644 index 00000000..281ea650 --- /dev/null +++ b/3.0/modules/star/helpers/MY_item.php @@ -0,0 +1,34 @@ +join("starred_items", "items.id", "starred_items.item_id", "LEFT OUTER") + ->and_where("starred_items.item_id", "IS", TRUE); + } + + return $model; + } +} diff --git a/3.0/modules/star/helpers/star.php b/3.0/modules/star/helpers/star.php new file mode 100644 index 00000000..db9dc3a1 --- /dev/null +++ b/3.0/modules/star/helpers/star.php @@ -0,0 +1,163 @@ +select_list("id", "name"); + // return array_merge(array(self::NONE => t("Nobody")), $options); + // } + + /** + * Returns the starred_item model related to the given item. + * + * There is an attempt to fetch the model from the database through the model + * cache. If it fails, a new unsaved model is created. + * + * @param Item_Model $item the item + * @return Starred_Item_Model the related starred_item model + */ + static function get_starred_item_model(Item_Model $item) { + try { + $model = model_cache::get("item", $id); + } + catch (Exception $e) { + $model = ORM::factory("starred_item"); + $model->item_id = $item->id; + $model->validate(); + } + + return $model; + } + + static function get_star_user_model() { + $model = ORM::factory("starred_only_user"); + $model->user_id = identity::active_user()->id; + $model->validate(); + return $model; + } + + /** + * Returns whether the given item can be starred. + * + * @param Item_Model $item the item + * @return bool + */ + static function can_be_starred(Item_Model $item) { + if (empty($item)) { + return false; + } + + //if ($item->type == "album") { + // return false; + //} + + return true; + } + + /** + * Returns whether the given item is starred. + * + * @param Item_Model $item the item + * @return bool + */ + static function is_starred(Item_Model $item) { + $model = self::get_starred_item_model($item); + return $model->loaded(); + } + + /** + * Stars the given item. + * + * @param Item_Model $item the item to star + */ + static function star(Item_Model $item) { + if (self::is_starred($item)) { + return; + } + + $starred_item = self::get_starred_item_model($item); + $starred_item->save(); + } + + /** + * Allows the given item to be unstarred. + * + * @param Item_Model $item the item to display + */ + static function unstar(Item_Model $item) { + if (!self::is_starred($item)) { + return; + } + + $starred_item = self::get_starred_item_model($item); + $starred_item->delete(); + } + + static function star_only_on() { + if (self::show_only_starred_items()) { + return; + } + + $star_user = self::get_star_user_model(); + $star_user->save(); + } + static function star_only_off() { + if (!self::show_only_starred_items()) { + return; + } + + $star_user = self::get_star_user_model(); + $star_user->delete(); + } + + /** + * Returns whether the active user shows only starred items. + * + * @return bool + */ + static function show_only_starred_items() { + $model = self::get_star_user_model(); + return $model->loaded(); + } + + /** + * Returns whether the active user can star any items. + * + * @return bool + */ + static function can_star() { + if (identity::active_user()->admin) { + return true; + } + + return false; + } +} diff --git a/3.0/modules/star/helpers/star_block.php b/3.0/modules/star/helpers/star_block.php new file mode 100644 index 00000000..12407900 --- /dev/null +++ b/3.0/modules/star/helpers/star_block.php @@ -0,0 +1,51 @@ + t("Star Item")); + } + + static function get($block_id, $theme) { + $item = $theme->item; + switch ($block_id) { + case "star": + + // If Item is movie then... + //if ($item && $item->is_movie() && access::can("view_full", $item)) { + $block = new Block(); + $block->css_id = "g-star-options"; + $block->title = t("Star"); + $block->content = new View("star_block.html"); + return $block; + //} + + // If Item is photo then... + //if ($item && $item->is_photo() && access::can("view_full", $item)) { + // $block = new Block(); + // $block->css_id = "g-download-fullsize"; + // $block->title = t("Download Photo"); + // $block->content = new View("downloadfullsize_block.html"); + // return $block; + //} + } + return ""; + } + +} diff --git a/3.0/modules/star/helpers/star_event.php b/3.0/modules/star/helpers/star_event.php new file mode 100644 index 00000000..e033c41a --- /dev/null +++ b/3.0/modules/star/helpers/star_event.php @@ -0,0 +1,91 @@ +get("settings_menu") + ->append(Menu::factory("link") + ->label(t("Star")) + ->url(url::site("admin/star"))); + } + + static function site_menu($menu, $theme, $item_css_selector) { + $item = $theme->item(); + + if (!empty($item) && star::can_be_starred($item) && star::can_star($item)) { + $csrf = access::csrf_token(); + $link = self::_get_star_link_data($item); + + $menu->get("options_menu") + ->append(Menu::factory("ajax_link") + ->label($link["text"]) + ->ajax_handler("function(data) { window.location.reload() }") + ->url(url::site("display/".$link["action"]."/$item->id?csrf=$csrf"))); + } + } + + static function context_menu($menu, $theme, $item, $thumb_css_selector) { + if (star::can_be_starred($item) && star::can_star($item)) { + $csrf = access::csrf_token(); + $link = self::_get_star_link_data($item); + + $menu + ->get("options_menu") + ->append(Menu::factory("ajax_link") + ->label($link["text"]) + ->ajax_handler("function(data) { window.location.reload() }") + ->url(url::site("display/".$link["action"]."/$item->id?csrf=$csrf"))); + } + } + + /** + * Returns some data used to create a star link. + * + * @param Item_Model $item the related item + * @return array + */ + private static function _get_star_link_data(Item_Model $item) { + if (star::is_starred($item)) { + $action = "unstar"; + $action_label = "Unstar"; + } + else { + $action = "star"; + $action_label = "Star"; + } + + switch ($item->type) { + case "movie": + $item_type_label = "movie"; + break; + case "album": + $item_type_label = "album"; + break; + default: + $item_type_label = "photo"; + break; + } + + $label = t("$action_label this $item_type_label"); + + return array("text" => $label, "action" => $action); + } +} diff --git a/3.0/modules/star/helpers/star_installer.php b/3.0/modules/star/helpers/star_installer.php new file mode 100644 index 00000000..ab260c67 --- /dev/null +++ b/3.0/modules/star/helpers/star_installer.php @@ -0,0 +1,43 @@ +query("CREATE TABLE IF NOT EXISTS {starred_items} ( + `item_id` int(9) NOT NULL, + PRIMARY KEY (`item_id`)) + DEFAULT CHARSET=utf8;"); + $db->query("CREATE TABLE IF NOT EXISTS {starred_only_users} ( + `user_id` int(9) NOT NULL, + PRIMARY KEY (`user_id`)) + DEFAULT CHARSET=utf8;"); + + module::set_var("star", "access_permissions", 0); + module::set_version("star", 1); + } + + static function uninstall() { + $db = Database::instance(); + $db->query("DROP TABLE IF EXISTS {starred_items};"); + $db->query("DROP TABLE IF EXISTS {starred_only_users};"); + } +} diff --git a/3.0/modules/star/helpers/star_theme.php b/3.0/modules/star/helpers/star_theme.php new file mode 100755 index 00000000..3a0a345e --- /dev/null +++ b/3.0/modules/star/helpers/star_theme.php @@ -0,0 +1,23 @@ + +
+

+
+ +
+
diff --git a/3.0/modules/star/views/star_block.html.php b/3.0/modules/star/views/star_block.html.php new file mode 100644 index 00000000..b76c5470 --- /dev/null +++ b/3.0/modules/star/views/star_block.html.php @@ -0,0 +1,141 @@ + + +item->is_photo()) { ?> +item)) { ?> + + + + +
+" + class="g-button ui-icon-left ui-state-default ui-corner-all"> +
+ + + +item->is_photo()) { ?> +item)) { ?> + + + + +
+" + class="g-button ui-icon-left ui-state-default ui-corner-all"> +
+ + + + +item)) { ?> + +
+item->id}?csrf={$csrf}") ?>" + ajax_handler="function(data) { window.location.reload() }" + title="" + class="g-button ui-icon-left ui-state-default ui-corner-all"> +
+ + + + + + + + + +
+ + + +" + class="g-button ui-icon-left ui-state-default ui-corner-all"> +
+ + + + +
+ + + +" + class="g-button ui-icon-left ui-state-default ui-corner-all"> +
+ + + + + + +
+" + ajax_handler="function(data) { window.location.reload() }" + title="" + class="g-ajax-link g-button ui-icon-left ui-state-default ui-corner-all"> +
+