id = intval($id); $this->blacklist_type = trim(strtolower($type)); $this->wrapperObj = $wrapperObj; $this->setBlacklistInfo(); } /** * Fills $blacklist_info array with data based on current $id */ public function setBlacklistInfo() { $fields = array('blacklist_id', 'type', 'track_name', 'keywords', 'artist_keywords', 'songwriters', 'status', 'active'); $fields = implode(', ', $fields); if (empty($this->blacklist_info)) { $sql = 'SELECT '. $fields .' FROM blacklisted_songs WHERE blacklist_id = ?'; $result = $this->wrapperObj->executeQuery($sql, [intval($this->id)], QueryWrapper::RETURN_AFFECTED_ROWS); if ($result === false) { show_pdo_error($sql, $this->wrapperObj->getLastStatement()); } $this->blacklist_info = $result; } } /** * Returns current contents of $blacklist_info array * @param $key - if $key is provided, it is used to determine returned value. * If it is an array, an array of values is returned * @return $returnValue */ public function getBlacklistInfo($key = null) { if (empty($this->blacklist_info)) { $this->setBlacklistInfo(); } if (is_null($key)) { return $this->blacklist_info; } else { if (is_array($key)) { $arr = array(); foreach ($key as $k) { $arr[] = $this->blacklist_info[$k]; } return $arr; } else { return $this->blacklist_info[$key]; } } } /** * Updates Blacklist record in database with $data * @param array $data - values for Blacklist record * @return int $result - 0 if update fails, number of updated rows if successful */ public function updateBlacklist($data) { $update = ''; if ($this->blacklist_type == 'publisher') { $fields = array('track_name', 'keywords', 'songwriters', 'status', 'active'); } else if ($this->blacklist_type == 'master') { $fields = array('track_name', 'keywords', 'artist_keywords', 'songwriters', 'status', 'active'); } $dataParams = []; foreach ($fields as $key) { if (key_exists($key, $data)) { $update_string = trim($data[$key]) ? '?' : 'NULL'; $dataParams[] = $data[$key]; $update[] = $key . " = " .$update_string; } } $update = ($update) ? implode(', ', $update) : ''; if ($update) { $sql = 'UPDATE blacklisted_songs SET ' . $update . ' WHERE blacklist_id = ? LIMIT 1'; $dataParams[] = $this->id; $result = $this->wrapperObj->executeQuery($sql, $dataParams, QueryWrapper::RETURN_AFFECTED_ROWS); if ($result === false) { show_pdo_error($sql, $this->wrapperObj->getLastStatement()); } return $result; } return 0; } /** * Creates new Blacklist record in database * @param array $data - values for new record * @param string $blacklist_type - 'publisher' or 'master' * @return int $result - 0 if insert fails, ID of inserted row if successful */ public static function createBlacklist ($data, $blacklist_type) { if (strtolower($blacklist_type) == 'publisher') { $fields = array('type', 'track_name', 'keywords', 'songwriters', 'status', 'active'); } else if (strtolower($blacklist_type) == 'master') { $fields = array('type', 'track_name', 'keywords', 'artist_keywords', 'songwriters', 'status', 'active'); } $data['type'] = $blacklist_type; $insert_fields = ''; $insert_values = ''; $dataParams = []; foreach ($fields as $key) { if (key_exists($key, $data)) { $insert_string = trim($data[$key]) ? '?' : 'NULL'; $comma = ($insert_fields == '') ? '' : ', '; $dataParams[] = $data[$key]; $insert_values .= $comma.$insert_string; $insert_fields .= $comma.$key; } } if ($insert_fields != '') { $sql = 'INSERT INTO blacklisted_songs (' . $insert_fields . ') VALUES (' . $insert_values . ')'; $last_inserted_id = $this->wrapperObj->executeQuery($sql, $dataParams, QueryWrapper::RETURN_INSERT_ID); if ($last_inserted_id === false) { show_pdo_error($sql, $this->wrapperObj->getLastStatement()); } return $last_inserted_id; // id of new row should be returned } return 0; } /** * Deletes a Blacklist record from the database * @return int $result - 0 if deletion fails, number of deleted rows otherwise */ public function deleteBlacklist () { $sql = 'DELETE FROM blacklisted_songs WHERE blacklist_id = ?'; $dataParams = []; $dataParams[] = $this->id; $result = $this->wrapperObj->executeQuery($sql, $dataParams, QueryWrapper::RETURN_AFFECTED_ROWS); if ($result === false) { show_pdo_error($sql, $this->wrapperObj->getLastStatement()); return 0; } $this->blacklist_info = array(); return $result; } /** * Returns 1 if blacklist record with values provided already exists * Returns 0 otherwise * @param string $data['type'] - 'publisher' or 'master' * @param string $data['track_name'] - blacklisted track name * @param string $data['keywords'] - blacklisted keywords * @param string $data['artist_keywords'] - blacklisted artist keywords (apply only to master blacklists) * @param string $data['songwriters'] - blacklisted songwriters * @param string $data['status'] - blacklist status * @param enum $data['active'] - shows if this blacklist is active ('Y') or not ('N') * * @return int $return_value */ public static function blacklistExists ($data) { $dataParams = []; $master_sql = (strtolower($data['type']) == 'master') ? ' AND LOWER(artist_keywords) = ?' : ""; $sql = 'SELECT blacklist_id FROM blacklisted_songs WHERE LOWER(type) = ? AND LOWER(track_name) = ? AND LOWER(keywords) = ? ' . $master_sql . ' AND LOWER(songwriters) = ? AND LOWER(status) = ? AND active = "Y"'; $dataParams[] = strtolower($data['type']); $dataParams[] = strtolower($data['track_name']); $dataParams[] = strtolower($data['keywords']); if (strtolower($data['type']) == 'master') { $dataParams[] = strtolower($data['artist_keywords']); } $dataParams[] = strtolower($data['songwriters']); $dataParams[] = strtolower($data['status']); $result = $this->wrapperObj->executeQuery($sql, $dataParams); if ($result === false) { show_pdo_error($sql, $this->wrapperObj->getLastStatement()); } if (count($result) > 0) { return 1; } return 0; } } ?>