| 1 |
<?php
|
| 2 |
// $Id: cafepress.module,v 1.3 2006/04/25 21:48:18 rooey Exp $
|
| 3 |
|
| 4 |
/*
|
| 5 |
cafepress.module
|
| 6 |
|
| 7 |
To use this module, just drop it into the modules directory in your drupal installation.
|
| 8 |
Then download images from your cafe' press store (keep the filenames as they are, or else
|
| 9 |
they will not link correctly) and put them someplace in your site's directory tree. Go
|
| 10 |
into your module configuration (make sure that this module is activated) for the cafepress
|
| 11 |
module, and set all the settings. The image directory is the directory that you have your
|
| 12 |
cafe' press images loaded into. The cafe press id is the store id so that you can actualy
|
| 13 |
make money off of your stuff... That's always helpful...
|
| 14 |
That's it! I hope it works for you!
|
| 15 |
|
| 16 |
If you would like to avoid downloading specific images from your store, you can download
|
| 17 |
cafepressbox from http://www.getfreesofts.com/script/883/8648/CafePress_Random_Product.html
|
| 18 |
and place the class.cafepressbox.php file in your modules/cafepress directory. It will
|
| 19 |
get the list of products and images directly from your store.
|
| 20 |
|
| 21 |
******************************************************
|
| 22 |
Feb 12 2004 - First revision of code. Basic features.
|
| 23 |
|
| 24 |
******************************************************
|
| 25 |
TODO:
|
| 26 |
Make the variable names and whatnot adhere better to the drupal coding standards
|
| 27 |
Make the block title configurable.
|
| 28 |
Add a way to upload the images right here and add text with the images
|
| 29 |
$Id: cafepress.module,v 1.3 2006/04/25 21:48:18 rooey Exp $
|
| 30 |
|
| 31 |
******************************************************
|
| 32 |
Apr 24 2006 - Updated for Drupal 4.5/4.6 compatibility.
|
| 33 |
|
| 34 |
******************************************************
|
| 35 |
ADDED:
|
| 36 |
* new shop page showing all items available.
|
| 37 |
* configurable column width options
|
| 38 |
* configurable links
|
| 39 |
|
| 40 |
******************************************************
|
| 41 |
Aug 6 2007 - Updated for Drupal 5.x compatibility.
|
| 42 |
Added cafepressbox.
|
| 43 |
|
| 44 |
******************************************************
|
| 45 |
ADDED:
|
| 46 |
* 5.x module info
|
| 47 |
* cafepressbox option
|
| 48 |
|
| 49 |
*/
|
| 50 |
|
| 51 |
/*****************************
|
| 52 |
* General module hooks.
|
| 53 |
*****************************/
|
| 54 |
|
| 55 |
/** Function to display help information. **/
|
| 56 |
function cafepress_help($section="admin/help#cafepress") {
|
| 57 |
$output = "";
|
| 58 |
|
| 59 |
switch ($section) {
|
| 60 |
case "admin/help#cafepress" :
|
| 61 |
$output = t("The <i>cafepress</i> module is used to display a block with cafepress stuff in it, and allow linking to those items for people to buy them.<br>Simply upload the images of your products from cafepress. Be sure to keep the name of each image the same as it's cafepress item number followed by .jpg");
|
| 62 |
break;
|
| 63 |
case "admin/system/modules/cafepress" :
|
| 64 |
$output = t("Set up the variables that allow the system to work. Make sure that you set the path correctly (check with your sys admin) and that you give a valid cafepress store id, or else it will not work.");
|
| 65 |
break;
|
| 66 |
case "admin/system/modules/cafepress#description" :
|
| 67 |
$output = t("Enables admins to set up a block with cafepress items in it.");
|
| 68 |
break;
|
| 69 |
}//end swith/case
|
| 70 |
|
| 71 |
return $output;
|
| 72 |
}//end function
|
| 73 |
|
| 74 |
|
| 75 |
/** sytem hook. **//*
|
| 76 |
function cafepress_system($field) {
|
| 77 |
return t("cafe press");
|
| 78 |
}//end function
|
| 79 |
*/
|
| 80 |
|
| 81 |
function cafepress_menu($may_cache) {
|
| 82 |
$items = array();
|
| 83 |
|
| 84 |
if ($may_cache) {
|
| 85 |
$items[] = array(
|
| 86 |
'path' => 'admin/settings/cafepress',
|
| 87 |
'title' => t('CafePress'),
|
| 88 |
'description' => t('Configure CafePress behavior.'),
|
| 89 |
'callback' => 'drupal_get_form',
|
| 90 |
'callback arguments' => 'cafepress_admin_settings',
|
| 91 |
'access' => user_access('administer site configuration')
|
| 92 |
);
|
| 93 |
$items[] = array('path' => 'onlinestore',
|
| 94 |
'title' => t('Online store'),
|
| 95 |
'callback' => 'cafepress_all',
|
| 96 |
'access' => user_access('access content'),
|
| 97 |
'type' => MENU_CALLBACK);
|
| 98 |
}
|
| 99 |
else {
|
| 100 |
/*
|
| 101 |
// Not sure what should go here --Logan
|
| 102 |
*/
|
| 103 |
}
|
| 104 |
|
| 105 |
return $items;
|
| 106 |
}
|
| 107 |
|
| 108 |
function cafepress_admin_settings() {
|
| 109 |
$form['cafepress_options'] = array(
|
| 110 |
'#type' => 'fieldset',
|
| 111 |
'#title' => t('CafePress options')
|
| 112 |
);
|
| 113 |
$form['cafepress_options']['cafepress_store_id'] = array(
|
| 114 |
'#type' => 'textfield',
|
| 115 |
'#title' => t("Cafepress store id"),
|
| 116 |
'#default_value' => variable_get('cafepress_store_id', variable_get("cafepress_store_id", "")),
|
| 117 |
'#description' => t('The store id given to you by cafepress when you signed up.'),
|
| 118 |
);
|
| 119 |
$form['cafepress_options']['cafepress_image_dir'] = array(
|
| 120 |
'#type' => 'textfield',
|
| 121 |
'#title' => t("Cafepress images location"),
|
| 122 |
'#default_value' => variable_get("cafepress_image_dir", "images/cafepress"),
|
| 123 |
'#description' => t("The place where you uploaded the product images on the local server."),
|
| 124 |
);
|
| 125 |
$form['cafepress_options']['cafepress_thumbnail_size'] = array(
|
| 126 |
'#type' => 'textfield',
|
| 127 |
'#title' => t("Thumbnail size"),
|
| 128 |
'#default_value' => variable_get("cafepress_thumbnail_size", "150"),
|
| 129 |
'#description' => t("The size to show the items in the block."),
|
| 130 |
);
|
| 131 |
$form['cafepress_options']['cafepress_num_cols'] = array(
|
| 132 |
'#type' => 'textfield',
|
| 133 |
'#title' => t("Display columns"),
|
| 134 |
'#default_value' => variable_get("cafepress_num_cols", "4"),
|
| 135 |
'#description' => t("The number of items to display in each row."),
|
| 136 |
);
|
| 137 |
$form['cafepress_options']['cafepress_store_link'] = array(
|
| 138 |
'#type' => 'select',
|
| 139 |
'#title' => t('Display link to store'),
|
| 140 |
'#options' => drupal_map_assoc(array("No", "Yes")),
|
| 141 |
'#default_value' => variable_get('cafepress_store_link', "Yes"),
|
| 142 |
'#description' => t('Should we display a link back to the cafepress store at the bottom of pages/blocks?'),
|
| 143 |
);
|
| 144 |
$form['cafepress_options']['cafepress_store_make'] = array(
|
| 145 |
'#type' => 'select',
|
| 146 |
'#title' => t('Show make store'),
|
| 147 |
'#options' => drupal_map_assoc(array("No", "Yes")),
|
| 148 |
'#default_value' => variable_get('cafepress_store_make', "No"),
|
| 149 |
'#description' => t('Should we display a referral link to allow users to make new cafepress stores?'),
|
| 150 |
);
|
| 151 |
|
| 152 |
return system_settings_form($form);
|
| 153 |
}
|
| 154 |
|
| 155 |
/** Function to display the block **/
|
| 156 |
function cafepress_block($op = "list", $delta = 0) {
|
| 157 |
if($op == "list") {
|
| 158 |
$result[0]["info"] = t("CafePress block");
|
| 159 |
return $result;
|
| 160 |
} else {
|
| 161 |
if($delta != 0) {
|
| 162 |
return "Unknown block for this module.";
|
| 163 |
} else {
|
| 164 |
$cafepressId = variable_get("cafepress_store_id", "");
|
| 165 |
$image_size = variable_get("cafepress_thumbnail_size", "150");
|
| 166 |
$imageDir = variable_get("cafepress_image_dir", "images/cafepress");
|
| 167 |
$showStore = variable_get("cafepress_store_link", "Yes");
|
| 168 |
$makeStore = variable_get("cafepress_store_make", "No");
|
| 169 |
if($imageDir == "" || $cafepressId == "") {
|
| 170 |
return "Block not properly configured.";
|
| 171 |
}//end if
|
| 172 |
|
| 173 |
mt_srand((double)microtime()*1000000);
|
| 174 |
$a = getProductArray();
|
| 175 |
|
| 176 |
$random = mt_rand(0, sizeof($a) - 1);
|
| 177 |
$image = $a[$random]['image'];
|
| 178 |
$prodid = $a[$random]['id'];
|
| 179 |
if(($prodid) && ($image)) {
|
| 180 |
$content = "<center><a href=\"http://www.cafepress.com/cp/prod.aspx?p=".$cafepressId.".".$prodid."\" target=\"_blank\">";
|
| 181 |
$content .= "<img src=\"".$imageDir."/".$image."\" border=\"0\" width=\"".$image_size."\" height=\"".$image_size."\" alt=\"Support this site by buying our stuff!\"></a><br>";
|
| 182 |
}//end if
|
| 183 |
$content .= "<div class=\"more-link\">".l(t("more"), "onlinestore", array("title" => t("More stuff."))) ."</a></div>";
|
| 184 |
if ($makeStore == "Yes" ) $content .= "<font size=\"-2\"><a href=\"http://www.cafepress.com/cp/info/storeref.aspx?refby=".$cafepressId."\" target=\"_blank\">Sell your stuff</a></font></center>";
|
| 185 |
$block = array();
|
| 186 |
$block['subject'] = "Stuff from our store";
|
| 187 |
$block['content'] = $content;
|
| 188 |
return $block;
|
| 189 |
}//end if/else
|
| 190 |
}//end if/else
|
| 191 |
}//end function
|
| 192 |
|
| 193 |
|
| 194 |
/** function to establish permissions. **/
|
| 195 |
function cafepress_perm() {
|
| 196 |
return array("view cafepress items");
|
| 197 |
}//end function
|
| 198 |
|
| 199 |
|
| 200 |
/** Function to return permissions.**/
|
| 201 |
function cafepress_access($op, $node) {
|
| 202 |
if($op == "view") {
|
| 203 |
return user_access("view cafepress items");
|
| 204 |
}
|
| 205 |
return $node->status;
|
| 206 |
}//end function
|
| 207 |
|
| 208 |
|
| 209 |
/** Page function. **/
|
| 210 |
function cafepress_all() {
|
| 211 |
|
| 212 |
$cafepressId = variable_get("cafepress_store_id", "");
|
| 213 |
$image_size = variable_get("cafepress_thumbnail_size", "150");
|
| 214 |
$imageDir = variable_get("cafepress_image_dir", "images/cafepress");
|
| 215 |
$numberOfCols = variable_get("cafepress_num_cols", "");
|
| 216 |
$showStore = variable_get("cafepress_store_link", "Yes");
|
| 217 |
$makeStore = variable_get("cafepress_store_make", "No");
|
| 218 |
if($imageDir == "" || $cafepressId == "") {
|
| 219 |
return "Block not properly configured.";
|
| 220 |
}//end if
|
| 221 |
|
| 222 |
mt_srand((double)microtime()*1000000);
|
| 223 |
$a = getProductArray();
|
| 224 |
$counter=count($a);
|
| 225 |
|
| 226 |
$content .= ('<div id="node"><span class="submitted">Welcome to our online store.</span><p>Select an item to view more details...<br><br><center><table width="100%"><tr>');
|
| 227 |
$newcounter="0";
|
| 228 |
$cellcounter="0";
|
| 229 |
while ($newcounter < $counter) {
|
| 230 |
$image = $a[$newcounter]['image'];
|
| 231 |
$prodid = $a[$newcounter]['id'];
|
| 232 |
$content .= "<td><center><a href=\"http://www.cafepress.com/cp/prod.aspx?p=".$cafepressId.".".$prodid."\" target=\"_blank\">";
|
| 233 |
$content .= "<img src=\"".$imageDir."/".$image."\" border=\"0\" width=\"".$image_size."\" height=\"".$image_size."\" alt=\"Support this site by buying our stuff!\"></a></td>";
|
| 234 |
$newcounter++;
|
| 235 |
$cellcounter++;
|
| 236 |
if ($cellcounter==$numberOfCols) {
|
| 237 |
$cellcounter="0";
|
| 238 |
$content .= "</tr><tr>";
|
| 239 |
}
|
| 240 |
}
|
| 241 |
$content .= "</tr></table></div>";
|
| 242 |
if ($showStore == "Yes" ) $content .= "<a href=\"http://www.cafepress.com/".$cafepressId."\" target=\"_blank\">See more items...</a><br>";
|
| 243 |
if ($makeStore == "Yes" ) $content .= "<font size=\"-2\"><a href=\"http://www.cafepress.com/cp/info/storeref.aspx?refby=".$cafepressId."\" target=\"_blank\">Sell your stuff</a></font></center>";
|
| 244 |
|
| 245 |
$page_content = $content;
|
| 246 |
|
| 247 |
print theme("page", $page_content);
|
| 248 |
|
| 249 |
}//end function
|
| 250 |
|
| 251 |
/** function to get an array of the products to display **/
|
| 252 |
function getProductArray() {
|
| 253 |
$a = array();
|
| 254 |
if (file_exists(drupal_get_path('module', 'cafepress')."/class.cafepressbox1.php")) {
|
| 255 |
require_once("class.cafepressbox.php");
|
| 256 |
$cp =& new cafePressBox(variable_get("cafepress_store_id", ""));
|
| 257 |
$cp->setCacheDir(variable_get("cafepress_image_dir", "images/cafepress")/*."/cafepressboxcache"*/);
|
| 258 |
$cp->setCacheTime(86400);
|
| 259 |
$cp->setCacheImages(true);
|
| 260 |
$cp->setResizeCachedImages(variable_get("cafepress_thumbnail_size", "150"));
|
| 261 |
$cp->_getProducts();
|
| 262 |
foreach ($cp->products as $id => $product) {
|
| 263 |
$a[] = array('id' => $id, 'image' => $product['image'].".jpg");
|
| 264 |
}
|
| 265 |
} else {
|
| 266 |
$imageDir = variable_get("cafepress_image_dir", "images/cafepress");
|
| 267 |
$imgs = dir($imageDir);
|
| 268 |
while($file = $imgs->read()) {
|
| 269 |
if(eregi(".gif", $file) || eregi(".jpg", $file) || eregi(".jpeg", $file)) {
|
| 270 |
$id = preg_split("[\.|_|v]", $file);
|
| 271 |
$a[] = array('id' => $id[0], 'image' => $file);
|
| 272 |
}//end if
|
| 273 |
}//end while
|
| 274 |
}
|
| 275 |
return $a;
|
| 276 |
}
|
| 277 |
|
| 278 |
/*
|
| 279 |
function displayProducts($cp) {
|
| 280 |
$txt = "";
|
| 281 |
|
| 282 |
$txt .= "var dump products: " . var_dump_pre($cp->products) . "<br>";
|
| 283 |
foreach($cp->products as $product) {
|
| 284 |
|
| 285 |
|
| 286 |
$txt .= "var dump product: " . var_dump_pre($product) . "<br>";
|
| 287 |
$txt .= '<img src="' . $product['image_url'] . '" />' . $product['description'] . '<br>';
|
| 288 |
$txt .= displayProduct($cp, true, $cp->products[0]);
|
| 289 |
}
|
| 290 |
return $txt;
|
| 291 |
}
|
| 292 |
|
| 293 |
function showRandomImage($cp) {
|
| 294 |
|
| 295 |
return displayProduct($cp, true);
|
| 296 |
}
|
| 297 |
|
| 298 |
function showProduct($cp, $itemNumber) {
|
| 299 |
// With a REALLY simple template and showing a specific product:
|
| 300 |
|
| 301 |
// $cp =& new cafePressBox('amnuts');
|
| 302 |
$cp->setTemplate('<div style="text-align:center;">[IMG]<br />[TXT]</div>');
|
| 303 |
return displayProduct($cp, false, $itemNumber);
|
| 304 |
|
| 305 |
// [IMG] is where the product image will be displayed
|
| 306 |
// [TXT] is where the product text will be displayed
|
| 307 |
|
| 308 |
}
|
| 309 |
|
| 310 |
function displayProduct($cp, $link_to_product = false, $id = false) {
|
| 311 |
ob_start();
|
| 312 |
$cp->displayItem($link_to_product, $id);
|
| 313 |
$content = ob_get_contents();
|
| 314 |
ob_end_clean();
|
| 315 |
return $content;
|
| 316 |
}
|
| 317 |
*/
|
| 318 |
|
| 319 |
function var_dump_pre($mixed = null) {
|
| 320 |
$txt = '<pre>';
|
| 321 |
$txt .= var_dump_ret($mixed);
|
| 322 |
$txt .= '</pre>';
|
| 323 |
return $txt;
|
| 324 |
}
|
| 325 |
|
| 326 |
function var_dump_ret($mixed = null) {
|
| 327 |
ob_start();
|
| 328 |
var_dump($mixed);
|
| 329 |
$content = ob_get_contents();
|
| 330 |
ob_end_clean();
|
| 331 |
return $content;
|
| 332 |
}
|
| 333 |
|
| 334 |
?>
|