Adding and removing javascript in c5 header

Permalink 1 user found helpful
Hi All
I have been trying to use this code that I have found in the forums
to turn off javascript when c5 is in EditMode


<?php
global $c;
if  (!$c->isEditMode()) {
  echo "EditMode";
} else {
  echo "NOT EditMode";
}
?>


As you can see by the code I have added, it does not work.
Am I missing somthing ?

The code will say in my header "EditMode" when it is on
and "NOT EditMode" when it is off


Any help with this appreciated

regards
Albert

 
TheRealSean replied on at Permalink Reply
TheRealSean
use instead of global $c;

$c = Page::getCurrentPage();

Also you have
if(!$c->isEditMode()){
//Not edit mode notice the ! 
echo "Not Edit Mode";
}else{
echo "Edit Mode";
}
//instead just use 
if($c->isEditMode()){
//do stuff when in edit mode
}else{
//stuff do do when NOT in edit mode
}



Javascript is core to the functioning of Concrete so you wont be able to turn off javascript during edit mode and still have the page work.

You can use the code to prevent custom css from being loaded in though. I assume this is what you mean?
awddesign replied on at Permalink Reply
Hi TheRealSean

thank you for the reply.
I tryed it
<?php
$c = Page::getCurrentPage();
if($c->isEditMode){
 echo "EditMode";
} else {
  echo "NOT EditMode";
}
?>

It still does not work.
When the EditMode is on I would like to turn off my javascript's that are in the Header while I am editing the page.

My javascript's that is not part of the core stuff

regards
Albert
TheRealSean replied on at Permalink Reply
TheRealSean
Hmm that should work, can you post an example or the site link?

you might have caught my post before the edit my initial post missed out the () at the end of isEditMode it is a function so should be
$c->isEditMode()
awddesign replied on at Permalink Reply
Hi TheRealSean

thank you for the reply.
I tryed it again with the new code still the same

<?php
$c = Page::getCurrentPage();
if($c->isEditMode()){
 echo "EditMode";
} else {
  echo "NOT EditMode";
}
?>


regards
Albert
TheRealSean replied on at Permalink Reply
TheRealSean
Im stumped then this code works?, can you share a link or your code there must be something on your page that is preventing it from working.

If you do not want to post the site link here pm me and Ill take a look
awddesign replied on at Permalink Reply
Hi TheRealSean

the template that I am testing it on is from here

http://www.concrete5.org/marketplace/themes/yosemite/...

All I have done so fare is to add the code to the header.php file

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!--
Design by Free CSS Templateshttp://www.freecsstemplates.org...
Released for free under a Creative Commons Attribution 2.5 License
Name       : Yosemite  
Description: A two-column, fixed-width design with dark color scheme.
Version    : 1.0
Released   : 20091106
Modified for Concrete5 by Jordan Lev -http://jordanlev.com
-->
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<base href="http://ftp3.dns-systems.net/~awddemo/concrete5/">
<?php  Loader::element('header_required'); ?>


regards
Albert
TheRealSean replied on at Permalink Reply
TheRealSean
Ok try this one instead, to have it displayed when logged in not just in edit mode.

$u = new User();
$g = Group::getByName('Administrators');
$p = new Permissions($c);
if($u->isSuperUser()||$u->inGroup($g)){
//do stuff if logged in but may not be able to edit the page

//you could also check to see if they are allowed to edit,
if($p->canWrite()){
//logged in and can edit the page
}
}

I have sort of mixed the content from here
http://www.concrete5.org/documentation/how-tos/developers/administr...
and also here
http://www.concrete5.org/documentation/developers/permissions/conte...


Hopefully that should help you out now,

Regards
awddesign replied on at Permalink Best Answer Reply
Hi TheRealSean

Thank you for the code. Eureka it works.

So what this code does is, when you login to C5 to
edit your site it can turn off code in the C5 site.
It's great if you have code conflicting with C5 when you are in
Edit mode.


<?php
$u = new User();
$g = Group::getByName('Administrators');
$p = new Permissions($c);
if($u->isSuperUser()||$u->inGroup($g)){
 echo "EditMode";
} else {
  echo "NOT EditMode";
}
?>


you can also do it this way too

<?php
$u = new User();
$g = Group::getByName('Administrators');
$p = new Permissions($c);
if($u->isSuperUser()||$u->inGroup($g)){
?>
 "EditMode"
<?php
} else {
?>
 "NOT EditMode"
<?php
}
?>


This code works only when you click "Edit Page" when you are in C5 and loged in

<?php
$c = Page::getCurrentPage();
if($c->isEditMode()){
 echo "EditMode";
} else {
  echo "NOT EditMode";
}
?>




regards
Albert
MrNiceGaius replied on at Permalink Reply
MrNiceGaius
This is another way to have conditional code for when the edit tool bar is visible:

<?php
      $c = Page::getCurrentPage();
      $cp = new Permissions($c);
      if (is_object($cp) && ($cp->canWrite() || $cp->canAddSubContent() || $cp->canAdminPage())) { ?>
<!-- DO STUFF WHEN EDIT BAR IS VISIBLE -->
      <?php }   ?>


I'm not sure how this compares to checking if the user is in the administrators group so I thought I should add it.

Cheers
jebidiam replied on at Permalink Reply
jebidiam
This was really helpful, thanks! Just using canWrite() was good enough for my purposes.