[[!saveFavorite]]
<form action="[[~[[*id]]]]" method="post">
<input type="hidden" name="favorite_page" value="[[*id]]" />
<input type="submit" value="Save This Page as My Favorite Page" />
</form>
<?php
if (!empty($_POST) && isset($_POST['favorite_page'])) {
if (!$modx->user) {
return; // no user logged in
}
// Get the user's extended fields
$profile = $modx->user->getOne('Profile');
$fields = $profile->get('extended');
$fields['Favorite Page'] = (int) $_POST['favorite_page'];
$profile->set('extended',$fields);
$profile->save();
}
?>
$fields['Favorite Page'] = '';
<?php
if (!$modx->user) {
return; // no user logged in
}
$id = $modx->resource->get('id'); //current id
// Get the user's extended fields
$profile = $modx->user->getOne('Profile');
$fields = $profile->get('extended');
$fav_field = explode(",", $fields['Favorite Page']); // change string to array
$sent_id = (int) $_POST['favorite_page'];
for($i = 0; $i < count($fav_field); ++$i)
if ($fav_field[$i] == $id) { // if found in array
if ($sent_id == $id) {
// if clicked to form button
// foreach($sent_id as $k) unset($fav_field[$k]);// remove sent id from array
foreach (array_keys($fav_field, $sent_id) as $key) unset($fav_field[$key]);
$modx->setPlaceholder('FavPH','Add to Favorites'); // change placeholder text
$fields['Favorite Page'] = implode(",", $fav_field); //put the rest back in field
$profile->set('extended',$fields);
$profile->save();
return;
}
$modx->setPlaceholder('FavPH','Remove from Favorites'); // change placeholder text if already in field but button not clicked
return;
}
//document not yet added to field
if (empty($sent_id)) {
$modx->setPlaceholder('FavPH','Add to Favorites');
return;
}
else array_push($fav_field, $sent_id); //else add at the end of array
$fields['Favorite Page'] = implode(",", $fav_field); //put the rest back in field
$profile->set('extended',$fields);
$profile->save();
$modx->setPlaceholder('FavPH','Remove from Favorites'); [[!saveFavorite]]
<form action="[[~[[*id]]]]" method="post">
<input type="hidden" name="favorite_page" value="[[*id]]" />
<input type="submit" value="[[+FavPH]]" />
</form>$profile = $modx->user->getOne('Profile');
if ($modx->user) {
//... logged in...
}
[[!saveFavorite]]
<form action="[[~[[*id]]]]" method="post">
<input type="hidden" name="favorite_page" value="[[*id]]" />
<input type="submit" name="fav_submit" [[+FavPH]] />
</form><?php
$modx->setPlaceholder('FavPH','class="fav-star" title="Pleas log in" value="Add to favorites" ');
$id = $modx->resource->get('id'); //current id
$sent_id = (int) $_POST['favorite_page'];
if ($modx->user->hasSessionContext($modx->context->get('key'))) {
// after login
// Get the user's extended fields
$profile = $modx->user->getOne('Profile');
$fields = $profile->get('extended');
$fav_field = explode(",", $fields['Favorite Page']); // change string to array
for($i = 0; $i < count($fav_field); ++$i)
if ($fav_field[$i] == $id) { // if found in array
if ($sent_id == $id) {
// if clicked to form button
foreach (array_keys($fav_field, $sent_id) as $key) unset($fav_field[$key]);
$modx->setPlaceholder('FavPH','class="fav-star" title="Add to favorites" value="Add to favorites" '); // change placeholder text
$fields['Favorite Page'] = implode(",", $fav_field); //put the rest back in field
$profile->set('extended',$fields);
$profile->save();
return;
}
$modx->setPlaceholder('FavPH',' class="fav-star-selected" title="Remove from favorites" value="Remove from favorites" '); // change placeholder text if already in field but button not clicked
return;
}
//document not yet added to field
if (empty($sent_id)) {
$modx->setPlaceholder('FavPH',' class="fav-star" title="Add to favorites" value="Add to favorites"');
return;
}
else array_push($fav_field, $sent_id); //else add at the end of array
$fields['Favorite Page'] = implode(",", $fav_field); //put the rest back in field
$profile->set('extended',$fields);
$profile->save();
$modx->setPlaceholder('FavPH',' class="fav-star-selected" title="Remove from favorites" value="Remove from favorites" ');
return;
}
if ($sent_id == $id) { // redirect to login PAGE 57
$url = $modx->makeUrl(57);
$modx->sendRedirect($url);
}
return;