Theory of photoisomerization of impurity molecules in solids