From bff9e37fa5fa41111b5b06ce6cdcb0eb8e5c21f9 Mon Sep 17 00:00:00 2001 From: Karl 'vollmerk' Vollmer Date: Wed, 12 Sep 2007 07:30:55 +0000 Subject: [PATCH] more work on the localplay stuff, most details ironed out, just needs some code to back it up, also tweaked plugins and threw in some extra goodies in the preference class, also pimped out the error class --- lib/class/dba.class.php | 2 +- lib/class/error.class.php | 64 +++++++++-------- lib/class/localplay.abstract.php | 9 ++- lib/class/preference.class.php | 42 +++++++++++ lib/class/rating.class.php | 12 ++-- localplay.php | 37 +++------- modules/localplay/httpq.controller.php | 57 +++++++++++++-- modules/localplay/mpd.controller.php | 72 +++++++++++++++++-- modules/plugins/Lastfm.plugin.php | 10 +-- modules/plugins/OpenStrands.plugin.php | 5 +- server/ajax.server.php | 4 ++ server/localplay.ajax.php | 38 ++++++++++ templates/show_album.inc.php | 4 +- templates/show_localplay_add_instance.inc.php | 26 +++++++ templates/sidebar_localplay.inc.php | 11 ++- 15 files changed, 308 insertions(+), 85 deletions(-) create mode 100644 server/localplay.ajax.php create mode 100644 templates/show_localplay_add_instance.inc.php diff --git a/lib/class/dba.class.php b/lib/class/dba.class.php index 62d18e00..c2c60fce 100644 --- a/lib/class/dba.class.php +++ b/lib/class/dba.class.php @@ -85,9 +85,9 @@ class Dba { public static function fetch_assoc($resource) { $result = mysql_fetch_assoc($resource); + debug_event('Assoc',self::$_sql,'6'); if (!$result) { -// debug_event('fetch_assoc',self::$_sql,'1'); return array(); } diff --git a/lib/class/error.class.php b/lib/class/error.class.php index 29c1c885..61202532 100644 --- a/lib/class/error.class.php +++ b/lib/class/error.class.php @@ -1,27 +1,5 @@ $error) { + $_SESSION['errors'][$key] = $error; + } + + } // __destruct + /** * add * This is a public static function it adds a new error message to the array @@ -70,24 +62,21 @@ class Error { if (!isset(Error::$errors[$name])) { Error::$errors[$name] = $message; Error::$state = 1; - return true; + $_SESSION['errors'][$key] = $message; } - // They want us to clobber it - if ($clobber) { + elseif ($clobber) { Error::$state = 1; Error::$errors[$name] = $message; - return true; + $_SESSION['errors'][$key] = $message; } - // They want us to append the error, add a BR\n and then the message else { Error::$state = 1; Error::$errors[$name] .= "
\n" . $message; - return true; + $_SESSION['errors'][$key] .= "
\n" . $message; } - } // add /** @@ -116,5 +105,20 @@ class Error { } // display + /** + * auto_init + * This loads the errors from the session back into Ampache + */ + public static function auto_init() { + + if (!is_array($_SESSION['errors'])) { return false; } + + // Re-insert them + foreach ($_SESSION['errors'] as $key=>$error) { + self::add($key,$error); + } + + } // auto_init + } // Error diff --git a/lib/class/localplay.abstract.php b/lib/class/localplay.abstract.php index 1874e390..d8e21387 100644 --- a/lib/class/localplay.abstract.php +++ b/lib/class/localplay.abstract.php @@ -34,11 +34,18 @@ abstract class localplay_controller { abstract public function status(); abstract public function get_version(); // Returns the version of this plugin abstract public function get_description(); // Returns the description - abstract public function actions(); // Return an array of name=>link actions for the sidebar abstract public function is_installed(); // Returns an boolean t/f abstract public function install(); abstract public function uninstall(); + // For display we need the following 'instance' functions + abstract public function add_instance($data); + abstract public function delete_instance($id); + abstract public function get_instances(); + abstract public function instance_fields(); + abstract public function set_active_instance($uid); + abstract public function get_active_instance(); + /** * get_url * This returns the URL for the passed object diff --git a/lib/class/preference.class.php b/lib/class/preference.class.php index 6fb707fe..ef8a0a05 100644 --- a/lib/class/preference.class.php +++ b/lib/class/preference.class.php @@ -175,4 +175,46 @@ class Preference { } // insert + /** + * delete + * This deletes the specified preference, a name or a ID can be passed + */ + public static function delete($preference) { + + // First prepare + if (!is_numeric($preference)) { + $id = self::id_from_name($preference); + $name = $preference; + } + else { + $name = self::name_from_id($preference); + $id = $preference; + } + + $id = Dba::escape($id); + + // Remove the preference, then the user records of it + $sql = "DELETE FROM `preference` WHERE `id`='$id'"; + $db_results = Dba::query($sql); + + self::rebuild_preferences(); + + } // delete + + /** + * rebuild_preferences + * This removes any garbage and then adds back in anything missing preferences wise + */ + public static function rebuild_preferences() { + + // First remove garbage + $sql = "DELETE FROM `user_preference` USING `user_preference` LEFT JOIN `preference` ON `preference`.`id`=`user_preference`.`preference` " . + "WHERE `preference`.`id` IS NULL"; + $db_results = Dba::query($sql); + + // Now add anything that we are missing back in, except System + $sql = "SELECT * FROM `preference` WHERE `type`!='system'"; + + } // rebuild_preferences + } // end Preference class diff --git a/lib/class/rating.class.php b/lib/class/rating.class.php index 1ddd842a..ff7b940d 100644 --- a/lib/class/rating.class.php +++ b/lib/class/rating.class.php @@ -64,12 +64,12 @@ class Rating { $user_id = Dba::escape($user_id); - $sql = "SELECT `score` FROM `rating` WHERE `user`='$user_id' AND `object_id`='$this->id' AND `object_type`='$this->type'"; + $sql = "SELECT `rating` FROM `rating` WHERE `user`='$user_id' AND `object_id`='$this->id' AND `object_type`='$this->type'"; $db_results = Dba::query($sql); $results = Dba::fetch_assoc($db_results); - return $results['score']; + return $results['rating']; } // get_user @@ -82,14 +82,14 @@ class Rating { */ public function get_average() { - $sql = "SELECT `score` FROM `rating` WHERE `object_id`='$this->id' AND `object_type`='$this->type'"; + $sql = "SELECT `rating` FROM `rating` WHERE `object_id`='$this->id' AND `object_type`='$this->type'"; $db_results = Dba::query($sql); $i = 0; while ($r = Dba::fetch_assoc($db_results)) { $i++; - $total += $r['score']; + $total += $r['rating']; } // while we're pulling results if ($total > 0) { @@ -123,11 +123,11 @@ class Rating { $db_results = Dba::query($sql); if ($existing = Dba::fetch_assoc($db_results)) { - $sql = "UPDATE `rating` SET `score`='$score' WHERE `id`='" . $existing['id'] . "'"; + $sql = "UPDATE `rating` SET `rating`='$score' WHERE `id`='" . $existing['id'] . "'"; $db_results = Dba::query($sql); } else { - $sql = "INSERT INTO `rating` (`object_id`,`object_type`,`score`,`user`) VALUES " . + $sql = "INSERT INTO `rating` (`object_id`,`object_type`,`rating`,`user`) VALUES " . " ('$this->id','$this->type','$score','" . $GLOBALS['user']->id . "')"; $db_results = Dba::query($sql); } diff --git a/localplay.php b/localplay.php index 06f081c7..4cc0b094 100644 --- a/localplay.php +++ b/localplay.php @@ -1,7 +1,7 @@ prefs['localplay_level'] < 1) { - access_denied(); - exit(); -} - -/* Scrub in the action */ -$action = scrub_in($_REQUEST['action']); - -show_template('header'); - - -switch ($action) { + break; case 'delete_song': $song_id = scrub_in($_REQUEST['song_id']); $songs = array($song_id); @@ -70,13 +59,7 @@ switch ($action) { require_once (conf('prefix') . '/templates/show_localplay.inc.php'); break; default: - if ($localplay = init_localplay()) { - require_once (conf('prefix') . '/templates/show_localplay.inc.php'); - } - else { - $GLOBALS['error']->add_error('general',_('Localplay Init Failed')); - $GLOBALS['error']->print_error('general'); - } + // Rien a faire? break; } // end switch action diff --git a/modules/localplay/httpq.controller.php b/modules/localplay/httpq.controller.php index 7e812792..fde12a9d 100644 --- a/modules/localplay/httpq.controller.php +++ b/modules/localplay/httpq.controller.php @@ -128,14 +128,63 @@ class AmpacheHttpq extends localplay_controller { } // uninstall /** - * actions - * List all the special kick ass things you can do with MPD + * add_instance + * This takes key'd data and inserts a new MPD instance */ - public function actions() { + public function add_instance($data) { - } // actions + } // add_instance + + /** + * delete_instance + * This takes a UID and deletes the instance in question + */ + public function delete_instance($uid) { + + + } // delete_instance + + /** + * get_instances + * This returns a key'd array of the instance information with + * [UID]=>[NAME] + */ + public function get_instances() { + + + } // get_instances + + /** + * instance_fields + * This returns a key'd array of [NAME]=>array([DESCRIPTION]=>VALUE,[TYPE]=>VALUE) for the + * fields so that we can on-the-fly generate a form + */ + public function instance_fields() { + + + + } // instance_fields + + /** + * set_active_instance + * This sets the specified instance as the 'active' one + */ + public function set_active_instance($uid) { + + + } // set_active_instance + + /** + * get_active_instance + * This returns the UID of the current active instance + * false if none are active + */ + public function get_active_instance() { + + + } // get_active_instance /** * add diff --git a/modules/localplay/mpd.controller.php b/modules/localplay/mpd.controller.php index 1ca7b4f5..d7819c48 100644 --- a/modules/localplay/mpd.controller.php +++ b/modules/localplay/mpd.controller.php @@ -129,10 +129,13 @@ class AmpacheMpd extends localplay_controller { "`host` VARCHAR( 255 ) NOT NULL , " . "`port` INT( 11 ) UNSIGNED NOT NULL DEFAULT '6600', " . "`password` VARCHAR( 255 ) NOT NULL , " . - "`access` SMALLINT( 4 ) UNSIGNED NOT NULL DEFAULT '0' " . + "`access` SMALLINT( 4 ) UNSIGNED NOT NULL DEFAULT '0', " . ") ENGINE = MYISAM"; $db_results = Dba::query($sql); + // Add an internal preference for the users current active instance + Preference::insert('mpd_active','MPD Active Instance','0','25','integer','internal'); + return true; } // install @@ -151,14 +154,73 @@ class AmpacheMpd extends localplay_controller { } // uninstall /** - * actions - * List all the special kick ass things you can do with MPD + * add_instance + * This takes key'd data and inserts a new MPD instance */ - public function actions() { + public function add_instance($data) { - } // actions + } // add_instance + + /** + * delete_instance + * This takes a UID and deletes the instance in question + */ + public function delete_instance($uid) { + + + } // delete_instance + + /** + * get_instances + * This returns a key'd array of the instance information with + * [UID]=>[NAME] + */ + public function get_instances() { + + + } // get_instances + + /** + * instance_fields + * This returns a key'd array of [NAME]=>array([DESCRIPTION]=>VALUE,[TYPE]=>VALUE) for the + * fields so that we can on-the-fly generate a form + */ + public function instance_fields() { + + + + } // instance_fields + + /** + * set_active_instance + * This sets the specified instance as the 'active' one + */ + public function set_active_instance($uid,$user_id='') { + + // Not an admin? bubkiss! + if (!$GLOBALS['user']->has_access('100')) { + $user_id = $GLOBALS['user']->id; + } + + $user_id = $user_id ? $user_id : $GLOBALS['user']->id; + + Preference::update('mpd_instance',$user_id,intval($uid)); + + return true; + + } // set_active_instance + + /** + * get_active_instance + * This returns the UID of the current active instance + * false if none are active + */ + public function get_active_instance() { + + + } // get_active_instance /** * add diff --git a/modules/plugins/Lastfm.plugin.php b/modules/plugins/Lastfm.plugin.php index 098e01e8..0d9c4b18 100644 --- a/modules/plugins/Lastfm.plugin.php +++ b/modules/plugins/Lastfm.plugin.php @@ -70,10 +70,12 @@ class AmpacheLastfm { */ public function uninstall() { - /* We need to remove the preivously added preferences */ - $sql = "DELETE FROM `preference` WHERE `name`='lastfm_pass' OR `name`='lastfm_user' " . - "OR `name`='lastfm_url' OR `name`='lastfm_host' OR `name`='lastfm_port' OR `name`='lastfm_challenge'"; - $db_results = Dba::query($sql); + Preference::delete('lastfm_pass'); + Preference::delete('lastfm_user'); + Preference::delete('lastfm_url'); + Preference::delete('lastfm_host'); + Preference::delete('lastfm_port'); + Preference::delete('lastfm_challenge'); } // uninstall diff --git a/modules/plugins/OpenStrands.plugin.php b/modules/plugins/OpenStrands.plugin.php index f57529b9..d63e9616 100644 --- a/modules/plugins/OpenStrands.plugin.php +++ b/modules/plugins/OpenStrands.plugin.php @@ -57,9 +57,8 @@ class AmpacheOpenStrands { */ function uninstall() { - /* We need to remove the preivously added preferences */ - $sql = "DELETE FROM `preference` WHERE `name`='mystrands_pass' OR `name`='mystrands_user'"; - $db_results = Dba::query($sql); + Preference::delete('mystrands_pass'); + Preference::delete('mystrands_user'); } // uninstall diff --git a/server/ajax.server.php b/server/ajax.server.php index 60e45dcf..26a7a75d 100644 --- a/server/ajax.server.php +++ b/server/ajax.server.php @@ -53,6 +53,10 @@ switch ($_REQUEST['page']) { require_once Config::get('prefix') . '/server/playlist.ajax.php'; exit; break; + case 'localplay': + require_once Config::get('prefix') . '/server/localplay.ajax.php'; + exit; + break; default: // A taste of compatibility break; diff --git a/server/localplay.ajax.php b/server/localplay.ajax.php new file mode 100644 index 00000000..aa8f9db3 --- /dev/null +++ b/server/localplay.ajax.php @@ -0,0 +1,38 @@ + diff --git a/templates/show_album.inc.php b/templates/show_album.inc.php index f2738e93..3a840db3 100644 --- a/templates/show_album.inc.php +++ b/templates/show_album.inc.php @@ -36,10 +36,8 @@ $title = scrub_out($album->name) . ' (' . $album->year . ') -- ' } ?> -
- +
id,'album'); ?> -
:
diff --git a/templates/show_localplay_add_instance.inc.php b/templates/show_localplay_add_instance.inc.php new file mode 100644 index 00000000..c2ab559f --- /dev/null +++ b/templates/show_localplay_add_instance.inc.php @@ -0,0 +1,26 @@ + + + + diff --git a/templates/sidebar_localplay.inc.php b/templates/sidebar_localplay.inc.php index 398f4791..815ffc2b 100644 --- a/templates/sidebar_localplay.inc.php +++ b/templates/sidebar_localplay.inc.php @@ -1,3 +1,12 @@
    -
  • +
  • +
      +
    • +
    +
  • +
  • +
      +
    • +
    +