fetchOne($sql); } catch (Zend_Exception $e){ Common_ErrorRep::repZendException($e); return $enum; } if(!empty($res)){ $enumStr = $res; // Parse the string in the form of enum('REGISTERED','ACTIVE','FROZEN') if(mlib_st_startsWith($enumStr, 'enum')){ $enumStr = str_replace ('enum(', '', $enumStr); $enumStr = str_replace (')', '', $enumStr); $enumStr = str_replace ("'", '', $enumStr); $enum = split(',', $enumStr); } } return $enum; } /** * * @param String $str The string that is to be escaped. * @param $linkEdentifier The MySQL connection. If the link identifier * is not specified, the last link opened by mysql_connect() is assumed. If no * such link is found, it will try to create one as if mysql_connect() was * called with no arguments. If by chance no connection is found or established, * an E_WARNING level error is generated. * @return String escaped string */ function mlib_sq_escapeString($str, $linkEdentifier = null){ if(!isset($linkEdentifier)){ return mysql_real_escape_string($str); } return mysql_real_escape_string($str, $linkEdentifier); } /** * * @param $db * @param $tableName * @param $columnName * @param $pfx * @param $criteria * @return */ function mlib_sq_makeUniqueRandomStr( Zend_Db_Adapter_Pdo_Mysql &$db, $tableName, $columnName, $pfx = '', $criteria = '' ){ // make a unique val $genVal = $pfx.mlib_st_makeRandomStr(10); // see if it exists. $sql = "SELECT $columnName FROM $tableName WHERE 1=1 "; Common_Persist::aD($sql, "SQL in ".__FILE__.' '.__FUNCTION__); if(!empty($criteria)){ $sql .= $criteria; } try{ $res = $db->fetchOne($sql); } catch (Zend_Exception $e){ Common_ErrorRep::repZendException($e); return $str; } // if it exists then try it again if($res == $genVal){ $genVal = mlib_sq_makeUniqueRandomStr( $db, $tableName, $columnName, $pfx, $criteria ); } return $genVal; } # === # st ~ STRING LIB # === /** * @param string $subjectStr * @param string $prefixStr * @return boolean */ function mlib_st_startsWith($subjectStr, $prefixStr){ return substr($subjectStr, 0, strlen($prefixStr)) == $prefixStr; } /** * @param string $subjectStr * @param string $suffixStr * @return boolean */ function mlib_st_endsWith($subjectStr, $suffixStr){ $sLen = strlen($suffixStr); return substr($subjectStr, strlen($subjectStr) - $sLen) == $suffixStr; } /** * Get left most part of string until $stopString occurs. * @param string $haystack * @param string $stopString * @return string */ function mlib_st_strLeft($haystack, $stopString){ return substr($haystack, 0, strpos($haystack, $stopString)); } function mlib_st_makeURLSafeStr($str){ $str = strtolower($str); $str = mlib_st_stripNoneAlphanumeric($str); $str = str_replace(' ', '-', $str); $str = str_replace('_', '-', $str); return $str; } function mlib_st_stripNoneAlphanumeric($str){ $str = preg_replace('/[^a-zA-Z0-9_\s]*/', '', $str); return $str; } function mlib_st_makeRandomStr($length = 7){ $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789012345678901234567890123456789"; srand((double)microtime()*1000000); $i = 0; $str = '' ; while ($i <= $length) { $num = rand() % strlen($chars); $tmp = substr($chars, $num, 1); $str = $str . $tmp; $i++; } return $str; } # === # ar ~ ARRAY LIB # === /** * @desc For example, beets and carrots are not fruits... $fruit_inventory = mlib_ar_arrayRemoveKey( $fruit_inventory, "beets", "carrots" ); */ function mlib_ar_arrayRemoveKey() { $args = func_get_args(); return array_diff_key($args[0],array_flip(array_slice($args,1))); } /** * @desc Let's also remove 'out of season' and 'no longer sold' fruit... $fruit_inventory = mlib_ar_arrayRemoveValue( $fruit_inventory, "out of season", "no longer sold" ); */ function mlib_ar_arrayRemoveValue() { $args = func_get_args(); return array_diff($args[0],array_slice($args,1)); } /** * @desc Strips prefixes from array keys */ function mlib_ar_stripPrefixKeys($hash, $prefixStr){ $hashNew = array(); foreach($hash as $k => $d){ $k = mlib_st_startsWith($k, $prefixStr) ? substr($k, strlen($prefixStr)) : $k; $hashNew[$k] = $d; } return $hashNew; } /** * Gets array of members of hash starting with prefix * @param $hash * @param $prefixStr */ function mlib_ar_getPrefixed($hash, $prefixStr){ $hashNew = array(); foreach($hash as $k => $d){ if(mlib_st_startsWith($k, $prefixStr)){ $hashNew[$k] = $d; } } return $hashNew; } function mlib_ar_arrayInsert(&$array, $insert, $position){ array_splice($array,$position,0,$insert); } # === # du ~ DATE UTILS # === /** * @desc Date Utils. Fetches the last day of the month from month and year. * @param int $month * @param int $year * @return int Last day of the month */ function mlib_du_getLastDayOfMonth($month, $year){ $month = (int) $month; $year = (int) $year; return idate('d', mktime(0, 0, 0, ($month + 1), 0, $year)); } /** * @desc Make a formmatted date (using php date function format param from date * in mySql format. * @param string $mySqlDate In the form of YYYY-MM-DD HH:MM:SS. * @param string $dateFormat * @return string Formatted date */ function mlib_du_getFormattedMySQLDate($mySqlDate, $dateFormat){ $uTime = mlib_du_getTimeFromMySQLDate($mySqlDate); return date($dateFormat, $uTime); } /** * @desc Date Utils. Fetches timestamp from a date in mysql format. * @param string $mySqlDate In the form of YYYY-MM-DD HH:MM:SS. Time may be * ommitted from the string. * @return int Unix timestamp */ function mlib_du_getTimeFromMySQLDate($mySqlDate){ $hr = '00'; $mn = '00'; $sc = '00'; list($date, $time) = split(' ', $mySqlDate); list($yy, $mm, $dd) = split('-', $date); if(!empty($time)) list($hr, $mn, $sc) = split(':', $time); $uTime = mktime((int) $hr, (int) $mn, (int) $sc, (int) $mm, (int) $dd, (int) $yy); return $uTime; } /** * @desc Date Utils. Gets a duration string from timestamps. * @param int $startTime * @param int $startTime * @param string $cutOff The accuracy to display. One of 'years', 'weeks', 'days' * 'hours', or 'minutes' * @return string Date string in the form of 16 years 40 weeks 6 days 7 hours * 44 minutes */ function mlib_du_getDurationStr($startTime, $endTime, $cutOff = 'minutes'){ /* Find out the seconds between the dates */ $timestamp = $endTime - $startTime; /* Cleaver Maths! */ $years=floor($timestamp/(60*60*24*365));$timestamp%=60*60*24*365; $weeks=floor($timestamp/(60*60*24*7));$timestamp%=60*60*24*7; $days=floor($timestamp/(60*60*24));$timestamp%=60*60*24; $hrs=floor($timestamp/(60*60));$timestamp%=60*60; $mins=floor($timestamp/60);$secs=$timestamp%60; // Display for date if ($years >= 1) { $str.= $years.' year'.(($years==1) ? ', ' : 's, '); } if($cutOff != 'years'){ if ($weeks >= 1) { $str.= $weeks.' week'.(($weeks==1) ? ', ' : 's, '); } if($cutOff != 'weeks'){ if ($days >= 1) { $str.= $days.' day'.(($days==1) ? ', ' : 's, '); } if($cutOff != 'days'){ if ($hrs >= 1) { $str.= $hrs.' hour'.(($hrs==1) ? ', ' : 's, '); } if($cutOff != 'hours'){ if ($mins >= 1) { $str.= $mins.' minute'.(($mins==1) ? ', ' : 's, '); } } } } } // strip off last comma $str = substr($str, 0, strlen($str) - 2); // @todo, replace last comma with and if needed. return $str; } function mlib_du_getDurationStrMySQL($startTimeMySQL, $endTimeMySQL, $cutoff = 'minutes'){ $startTime = mlib_du_getTimeFromMySQLDate($startTimeMySQL); $endTime = mlib_du_getTimeFromMySQLDate($endTimeMySQL); return mlib_du_getDurationStr($startTime, $endTime, $cutOff); } /** * @desc Date Utils. Fetches a MySQL formatted date from a unix timestamp e.g. * YYYY-MM-DD HH:MM:SS * @param int $timestamp Unix timestamp. * @return string */ function mlib_du_getMySQLDate($timestamp){ return date('Y-m-d H:i:s', $timestamp); } # === # fu ~ FORM UTILS # === /** * @desc Will turn something like pfx_my-form-field_name into 'My form field name'. * @param string $fieldName * @param string $stripPrefix * @param string $seperator * @return boolean */ function mlib_fu_makeFieldNameAlias($fieldName, $stripPrefix = '', $seperator = '-'){ $fieldName = substr($fieldName, strlen($stripPrefix)); $fieldName = str_replace($seperator, ' ', $fieldName); $fieldName = ucfirst($fieldName); return $fieldName; } function mlib_fu_getChecked($val1, $val2){ return $val1 == $val2 ? ' checked="checked" ' : ''; } function mlib_fu_getSelected($val1, $val2){ return $val1 == $val2 ? ' selected="selected" ' : ''; } # === # nv ~ NAVIGATION FUNCTIONS # === /** * @desc * @param string $ssrURL Site root ralative URL * @param string $className * @return string class */ function mlib_nv_getHereByURL($ssrURL, $className = 'here'){ return $_SERVER['PHP_SELF'] == $ssrURL ? $className : ''; } function mlib_nv_getHere($thisNavLoc, $currNavLoc, $className = 'here'){ return $thisNavLoc == $currNavLoc ? $className : ''; } # === # im ~ IMAGE FUNCTONS # === /** * * @param string $imgPath The image source * @param string $imgPathDest The image destination * @param int $w (optional) The new width * @param int $h (optional) The new heights * @param float $percent (optional) * @param Boolean $constrain (optional) Defaults to true * @return Boolean success */ function mlib_im_rescaleImage( $imgPath, $imgPathDest = null, $w = null, $h = null, $percent = 0, $constrain = true, $outputQuality = 75 ){ Common_Persist::aD($imgPath, 'mlib_im_rescaleImage called'); if(!is_file($imgPath)) return false; $imgPathDest = !empty($imgPathDest) ? $imgPathDest : $imgPath; // get image size of img $x = @getimagesize($imgPath); // image width $sw = $x[0]; // image height $sh = $x[1]; Common_Persist::aD($x, 'mlib_im_rescaleImage image size'); if ($percent > 0) { // calculate resized height and width if percent is defined $percent = $percent * 0.01; $w = $sw * $percent; $h = $sh * $percent; } else { if (isset ($w) AND !isset ($h)) { // autocompute height if only width is set $h = (100 / ($sw / $w)) * .01; $h = @round ($sh * $h); } elseif (isset ($h) AND !isset ($w)) { // autocompute width if only height is set $w = (100 / ($sh / $h)) * .01; $w = @round ($sw * $w); } elseif (isset ($h) AND isset ($w) AND isset ($constrain)) { // get the smaller resulting image dimension if both height // and width are set and $constrain is also set $hx = (100 / ($sw / $w)) * .01; $hx = @round ($sh * $hx); $wx = (100 / ($sh / $h)) * .01; $wx = @round ($sw * $wx); if ($hx < $h) { $h = (100 / ($sw / $w)) * .01; $h = @round ($sh * $h); } else { $w = (100 / ($sh / $h)) * .01; $w = @round ($sw * $w); } } } $im = @ImageCreateFromJPEG ($imgPath) or // Read JPEG Image $im = @ImageCreateFromPNG ($imgPath) or // or PNG Image $im = @ImageCreateFromGIF ($imgPath) or // or GIF Image $im = false; // If image is not JPEG, PNG, or GIF if (!$im) { // We get errors from PHP's ImageCreate functions... return false; } // Create the resized image destination $thumb = @ImageCreateTrueColor ($w, $h); // Copy from image source, resize it, and paste to image destination @ImageCopyResampled ($thumb, $im, 0, 0, 0, 0, $w, $h, $sw, $sh); // Output resized image $success = @ImageJPEG($thumb, $imgPathDest, $outputQuality); return $success; } # === # em ~ EMAILER FUNCTIONS # === /** * @desc Makes a generic plain text emailer string from a hashArr. * @param string $heading * @param string $intro * @param string $hashArr * @param string $mustStartKey If provided, hash key in hashArr */ function mlib_em_makeEmailString($heading, $intro, $hashArr, $mustStartKey = ''){ $es = ""; // es ~ email string if(!empty($heading)) $es .= "\n".$heading; if(!empty($intro)) $es .= "\n\n".$intro; $es .= "\n"; // [...fetch output fields prefexed with mustStartKey ...] if(is_array($hashArr)){ foreach($hashArr as $key => $data){ if(mlib_st_startsWith($key, $mustStartKey)){ if(!empty($data)){ $alias = mlib_fu_makeFieldNameAlias($key, $mustStartKey); $data = "\n".$data; // [...prefix multiline bits with tabs...] $data = str_replace("\n", "\n\t", $data); $es .= "\n\n".$alias.":\n".$data; } } } } return $es; } # === # ur ~ URL utils # === function mlib_ur_getURL(){ $s = empty($_SERVER["HTTPS"]) ? "" : ($_SERVER["HTTPS"] == "on") ? "s" : ""; $protocol = mlib_st_strLeft(strtolower($_SERVER["SERVER_PROTOCOL"]), "/").$s; $port = ($_SERVER["SERVER_PORT"] == "80") ? "" : (":".$_SERVER["SERVER_PORT"]); return $protocol."://".$_SERVER['SERVER_NAME'].$port.$_SERVER['REQUEST_URI']; } # === # du ~ DEVELOPER UTILS # === /* function mlib_du_makeArrStr($hashArr, $arrayName){ $str = '$'.$arrayName.' = array('; foreach($hashArr as $key => $str){ $str .= "\n\t'$key' => '$data'"; } $str .= "\n);"; return $str; } */ # === # em ~ sb ~ STUB # === // function mlib_sb_renderTextImage($str, $styleKey = ""){ $alt = $str; $enc = urlencode($str); $html = "\"$alt\""; return $html; } # === # ALIASES. l_ ~ mlib_, g ~ get # === function l_fu_gChk($val1, $val2){ return mlib_fu_getChecked($val1, $val2); } function l_fu_gSel($val1, $val2){ return mlib_fu_getSelected($val1, $val2); } function l_nv_gHere($thisNavLoc, $currNavLoc){ return mlib_nv_getHere($thisNavLoc, $currNavLoc); } function l_nv_gHereByURL($ssrURL, $className = 'here'){ return mlib_nv_getHereClass($ssrURL, $className); } function l_sb_RTI($str, $styleKey = ""){ return mlib_sb_renderTextImage($str, $styleKey); } # === # Super shorthad strings # === /** * * @param String $str The string that is to be escaped. * @param $linkEdentifier The MySQL connection. If the link identifier * is not specified, the last link opened by mysql_connect() is assumed. If no * such link is found, it will try to create one as if mysql_connect() was * called with no arguments. If by chance no connection is found or established, * an E_WARNING level error is generated. * @return String escaped string */ function esc($str, $linkEdentifier = null){ return mlib_sq_escapeString($str, $linkEdentifier); } ?>
Warning: The magic method __isset() must have public visibility and cannot be static in E:\Web Root\coffeecups.co.uk\app\lib\Zend\Config.php on line 182

Warning: The magic method __unset() must have public visibility and cannot be static in E:\Web Root\coffeecups.co.uk\app\lib\Zend\Config.php on line 194

Warning: The magic method __call() must have public visibility and cannot be static in E:\Web Root\coffeecups.co.uk\app\lib\Zend\Db\Select.php on line 1057

Warning: The magic method __get() must have public visibility and cannot be static in E:\Web Root\coffeecups.co.uk\app\lib\Zend\Session\Namespace.php on line 247

Warning: The magic method __set() must have public visibility and cannot be static in E:\Web Root\coffeecups.co.uk\app\lib\Zend\Session\Namespace.php on line 269

Warning: The magic method __isset() must have public visibility and cannot be static in E:\Web Root\coffeecups.co.uk\app\lib\Zend\Session\Namespace.php on line 354

Warning: The magic method __unset() must have public visibility and cannot be static in E:\Web Root\coffeecups.co.uk\app\lib\Zend\Session\Namespace.php on line 374